如何从单独的广播接收器 class 完成 Android activity

How to finish an Android activity from separate Broadcast receiver class

我有一个 activity class MediaPlayerDemo_Audio 和一个 broadcast receiver class ConnectivityChangeReceiver 扩展 BroadcastReceiver.

我想在网络连接丢失时完成 activity 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 将在后台 运行 并且它将继续检查网络连接,此 class 将从该接收器调用。

<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 class 不能扩展 Activity 以及 BroadcastReceiver class,反之亦然。

根据示例,它不需要同时扩展 类。只需在 activity 中创建广播接收器,您需要做的一件事是在 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 */
            }
        }
    }
}
}

你只需要将你的 BroadcastReceiver Class 放在你的 activity 里面作为内部 Class。并在 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 

        }
    }       
}}



}