Android - BroadcastReceiver unregisterReceiver 问题(未注册)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12421449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:37:11  来源:igfitidea点击:

Android - BroadcastReceiver unregisterReceiver issue (not registered)

androidbroadcastreceiver

提问by Marc Ortiz

I'm having some problems unregistering a BroadcastReceiver. I'm first registering it but then when I want to unregister it by using unregisterReceiver();command gives me tons of errors. The error says that I've not registered my BroadcastReceiver.

我在注销BroadcastReceiver. 我首先注册它,但是当我想通过使用unregisterReceiver();命令取消注册它时,会出现大量错误。错误说我没有注册我的BroadcastReceiver.

Code:

代码:

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);
        }
    }
}

回答by JunR

First of all,

首先,

Use thisto work with the object of the class Reciever

使用来处理类 Reciever的对象

remove all robjects, don't call constructors in the extending class.

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

Then:

然后:

Define

定义

boolean isRegistered = false;

In your register method:

在您的注册方法中:

c.registerReceiver(this);
isRegistered = true;

In your unregister method:

在您的注销方法中:

if (isRegistered) {
    c.unregisterReceiver(this);
    isRegistered = false;
}

Then in your activity use instance ofthe class Reciver.

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

Hope, it was helpful!

希望,很有帮助!

回答by jfmg

The best solution is to put the unregisterReceivermethod into a try catch block.

最好的解决方案是将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.
}

Unfortunately, there is no specific exception thrown when unregisterReceiverfails, so you got to use Exception. Also, there is unfortunately no method like isReceiverRegistered(BroadcastReceiver receiver), which in my opinion would be an enhancement to the Android API.

不幸的是,unregisterReceiver失败时没有抛出特定的异常,因此您必须使用Exception. 此外,不幸的是没有像 那样的方法isReceiverRegistered(BroadcastReceiver receiver),在我看来这将是对 Android API 的增强。

Besides, it won't hurt to take a look at ReceiverLifecycle

此外,看看ReceiverLifecycle不会有什么坏处

回答by nandeesh

You cannot unregister a new instance of broadcast Receiver. You will need to use the same instance of BroadcastReciever which has been registered.

您不能取消注册广播接收器的新实例。您将需要使用已注册的相同 BroadcastReciever 实例。

So use

所以用

 c.registerReceiver(this, apps);

and

 c.unregisterReceiver(this);

回答by user936414

In unregister method, you are creating a new BroadcastReceiver instance and unregistering that. Instead make the instance in register method global and call

在 unregister 方法中,您正在创建一个新的 BroadcastReceiver 实例并取消注册。而是将 register 方法中的实例设为全局并调用

if (r != null) {
        c.unregisterReceiver(r);
        r = null;
 }

in unregister method.

在注销方法中。