如何从单独的广播接收器类完成Android活动

时间:2015-12-08 11:30:36

标签: android android-activity broadcastreceiver activity-finish

我有一个活动班MediaPlayerDemo_Audio和一个broadcast receiverConnectivityChangeReceiver,其中BroadcastReceiver延伸。

我想在网络连接丢失时完成MediaPlayerDemo_Audio活动。

有什么想法吗? 感谢。

ConnectivityChangeReceiver.java

public class ConnectivityChangeReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE );
    /* Check wi-fi network availability */
    NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToWifi = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
    /* Check mobile network availability */
    NetworkInfo activeMobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToMobileData = activeMobileInfo != null && activeMobileInfo.isConnectedOrConnecting();

    if(isConnectedToWifi){
        //Toast.makeText(context, "Connected to wifi!", Toast.LENGTH_LONG).show();
    }else{
        if(isConnectedToMobileData){
            //Toast.makeText(context, "Connected to Mobile data!", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(context, "No internet connection!", Toast.LENGTH_LONG).show();
            /* Here I want to finish that activity */
        }
    }       
}}

的AndroidManifest.xml Class ConnectivityChangeReceiver将在后台运行,它将继续检查网络连接,这个类将从这个接收器调用。

<receiver android:name=".ConnectivityChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

我提到了answer,但它无能为力。因为一个class cannot extend multiple classes。我的意思是,MediaPlayerDemo_Audio类无法扩展Activity以及BroadcastReceiver类,反之亦然。

2 个答案:

答案 0 :(得分:1)

您只需将活动中的BroadcastReceiver类作为内部类。并在onResume上注册Receiver并在onPause内取消注册。

让我们说

public class MediaPlayerDemo_Audio extends Activity
{

//  your activity code
.....
//  


// here is your broadcast receiver 
public class ConnectivityChangeReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE );
    /* Check wi-fi network availability */
    NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToWifi = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
    /* Check mobile network availability */
    NetworkInfo activeMobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    boolean isConnectedToMobileData = activeMobileInfo != null && activeMobileInfo.isConnectedOrConnecting();

    if(isConnectedToWifi){
        //Toast.makeText(context, "Connected to wifi!", Toast.LENGTH_LONG).show();
    }else{
        if(isConnectedToMobileData){
            //Toast.makeText(context, "Connected to Mobile data!", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(context, "No internet connection!", Toast.LENGTH_LONG).show();
            /* Here I want to finish that activity */

//finish activity here 

        }
    }       
}}



}

答案 1 :(得分:0)

根据示例,它不需要扩展这两个类。只需在您的活动中创建广播接收器,您需要做的一件事就是在onResume()中注册接收器并在onPause()上取消注册。

以下是示例: -

public class MyPreferenceActivity extends Activity {

//Your stuff inside the activity

@Override
protected void onResume() {
    super.onResume();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
    registerReceiver(receiver, intentFilter);
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
}

ConnectivityChangeReceiver receiver = new ConnectivityChangeReceiver();

public class ConnectivityChangeReceiver extends BroadcastReceiver {

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
/* Check wi-fi network availability */
        NetworkInfo activeWifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnectedToWifi = activeWifiInfo != null && activeWifiInfo.isConnectedOrConnecting();
/* Check mobile network availability */
        NetworkInfo activeMobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        boolean isConnectedToMobileData = activeMobileInfo != null && activeMobileInfo.isConnectedOrConnecting();

        if (isConnectedToWifi) {
            //Toast.makeText(context, "Connected to wifi!", Toast.LENGTH_LONG).show();
        } else {
            if (isConnectedToMobileData) {
                //Toast.makeText(context, "Connected to Mobile data!", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "No internet connection!", Toast.LENGTH_LONG).show();
        /* Here I want to finish that activity */
            }
        }
    }
}
}