Android - BroadcastReceiver unregisterReceiver问题(未注册)

Mar*_*tiz 5 android broadcastreceiver

我有一些问题取消注册BroadcastReceiver.我是第一次注册它,但是当我想通过使用unregisterReceiver();命令取消注册它给我带来了大量的错误.错误说我没有注册我的BroadcastReceiver.

码:

public class Receiver extends BroadcastReceiver implements Variables {

    CheckConexion cc;

    @Override
    public void onReceive(Context contxt, Intent intent) {

        // Cuando hay un evento, lo diferenciamos y hacemos una acción.

        if (intent.getAction().equals(SMS_RECEIVED)) {
            Sms sms = new Sms(null, contxt);
            sms.uploadNewSms(intent);
        } else if (intent.getAction().equals(Intent.ACTION_BATTERY_LOW)) {
            // st.batterylow(contxt);
        } else if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)) {
            // st.power(1, contxt);
        } else if (intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED)) {
            // st.power(0, contxt);
        } else if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)
                || intent.getAction().equals(Intent.ACTION_PACKAGE_CHANGED)
                || intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
            Database db = new Database(contxt);
            if (db.open().Preferences(4)) {
                Uri data = intent.getData();
                new ListApps(contxt).import_app(intent, contxt, data,
                        intent.getAction());
            }
            db.close();
        } else if (intent.getAction().equals(
                ConnectivityManager.CONNECTIVITY_ACTION)) {
            cc = new CheckConexion(contxt);
            if (cc.isOnline()) {

                /*Database db = new Database(contxt);
                db.open();
                if (db.move() == 1) {
                    new UploadOffline(contxt);
                }
                db.close();*/

            }
        }

    }

    public void register(Context c) {
        Receiver r = new Receiver();
        IntentFilter i = new IntentFilter();
        i.addAction(SMS_RECEIVED);
        i.addAction(Intent.ACTION_BATTERY_LOW);
        i.addAction(Intent.ACTION_POWER_CONNECTED);
        i.addAction(Intent.ACTION_POWER_DISCONNECTED);
        i.addAction(Intent.ACTION_CALL_BUTTON);
        i.addAction(Intent.ACTION_CAMERA_BUTTON);
        i.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        c.registerReceiver(r, i);
        IntentFilter apps = new IntentFilter();
        apps.addAction(Intent.ACTION_PACKAGE_ADDED);
        apps.addAction(Intent.ACTION_PACKAGE_CHANGED);
        apps.addAction(Intent.ACTION_PACKAGE_REMOVED);
        apps.addDataScheme("package");
        c.registerReceiver(r, apps);
    }

    public void unregister(Context c) {
        BroadcastReceiver r = new Receiver();
        if (r != null) {
            c.unregisterReceiver(r);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 7

首先,

使用来处理类Reciever的对象

删除所有r对象,不要在扩展类中调用构造函数.

然后:

限定

boolean isRegistered = false;
Run Code Online (Sandbox Code Playgroud)

在您的注册方法中:

c.registerReceiver(this);
isRegistered = true;
Run Code Online (Sandbox Code Playgroud)

在取消注册方法中:

if (isRegistered) {
    c.unregisterReceiver(this);
    isRegistered = false;
}
Run Code Online (Sandbox Code Playgroud)

然后在您的活动中使用Reciver的实例.

希望,这很有帮助!


jfm*_*fmg 6

最好的解决方案是将该unregisterReceiver方法放入try catch块中。

try {
    this.unregisterReceiver(myReceiver);
}
catch (final Exception exception) {
    // The receiver was not registered.
    // There is nothing to do in that case.
    // Everything is fine.
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,unregisterReceiver失败时不会抛出特定的异常,因此您必须使用Exception。另外,不幸的是,没有类似的方法isReceiverRegistered(BroadcastReceiver receiver),我认为这是对Android API的增强。

此外,看看ReceiverLifecycle也无害

  • 最好只捕获`IllegalArgumentException`。 (2认同)