Android - BroadcastReceiver unregisterReceiver 问题(未注册)

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

最佳答案

首先,

使用 this 来处理 class Reciever 的对象

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

然后:

定义

boolean isRegistered = false;

在你的注册方法中:

c.registerReceiver(this);
isRegistered = true;

在您的注销方法中:

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

然后在您的 Activity 中使用Reciver

希望对您有所帮助!

关于Android - BroadcastReceiver unregisterReceiver 问题(未注册),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12421449/

相关文章:

Android:获取短信发送时间

android - 无法注册 BroadcastReceiver

android - 如果 Activity 已关闭,则处理事件电源按钮按下

android - 当我的应用程序处于后台时在 Android 上捕获硬件按钮

安卓 : How to populate a ListView with alphabet indexer and fastscroll as in contacts app. ?

android - Eclipse Android 项目中的 JavaDoc

android - RecyclerView 中的预取文本布局,无法解析方法 setTextFuture

java - 在android中的BroadcastReceiver类的onReceive方法中访问ArrayList项目

java - 安慰。将带有 "link"的文本写入类/行

android - 类和回收器 View 错误 : can't instantiate class