是否可以注销没有扩展类的扩展类的BroadcastReceiver?

时间:2019-03-19 06:38:11

标签: android broadcastreceiver android-broadcastreceiver

我正在使用BroadcastReceiver来检查扩展类的网络状态,而不是初始化BroadcastReceiver。那么,可以在任何地方注销它吗?

如果是,那么如何做,如果不是,那么将是这样做的替代解决方案。 我看到了一个解决方案here,但这不是问题的实际答案。

  

这是我正在使用BroadcastReceiver的班级。

 public class CheckNetworkState extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        /*check WIFI state*/
        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        /*check network state*/
        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable() || mobile.isAvailable()) {
            //Toast.makeText(context, "connection established", Toast.LENGTH_SHORT).show();
        } else {
            /*shows dialogue*/
            Intent alertDialogueIntent = new Intent(context, DialogueUtils.class);
            alertDialogueIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(alertDialogueIntent);
        }
    }
}
  

这是我的Manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.hardware.camera.autofocus" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Utils.DialogueUtils" />
    <receiver
        android:name=".Utils.CheckNetworkState"
        android:enabled="true">
        <intent-filter>
            <action
                android:name="android.net.conn.CONNECTIVITY_CHANGE"
                tools:ignore="BatteryLife" />
        </intent-filter>
    </receiver>

    <activity android:name=".UserAuth.ChangePassword"></activity>
    <activity android:name=".QRScanner.CustomScanner" />
    <activity android:name=".WaterMetersList.WaterMeters" />
    <activity android:name=".UserAuth.Login" />
    <activity
        android:name=".ControlCharts.Charts"
        android:label="@string/title_activity_bottom_navigation" />
    <activity android:name=".Splash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

1 个答案:

答案 0 :(得分:0)

在您的应用程序类中,如下创建静态接收器:

public class YourApplication extends Application {

    public static BroadcastReceiver receiver;

    public void onCreate() {
        super.onCreate();
        receiver = new CheckNetworkState(); 
    }
}

之后,您可以将接收器注册为:

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

this.registerReceiver(YourApplication.receiver, filter);

之后,您可以将其注销为:

getContext().unregisterReceiver(YourApplication.receiver);