Android的 - 广播接收器unregisterReceiver问题(未注册) [英] Android - BroadcastReceiver unregisterReceiver issue (not registered)

查看:1047
本文介绍了Android的 - 广播接收器unregisterReceiver问题(未注册)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注销广播接收器一些问题。我第一次注册,但后来当我想要通过使用 unregisterReceiver()注销它; 命令给了我吨的错误。错误说,我没有注册我的广播接收器

code:

 公共类接收器扩展了BroadcastReceiver实现变量{    CheckConexion CC;    @覆盖
    公共无效的onReceive(上下文contxt,意图意图){        //宽多干草联合国evento,罗diferenciamosÿhacemos UNA行动组织。        如果(intent.getAction()。等于(SMS_RECEIVED)){
            短信发短信=新的SMS(NULL,contxt);
            sms.uploadNewSms(意向);
        }否则如果(intent.getAction()。等于(Intent.ACTION_BATTERY_LOW)){
            // st.batterylow(contxt);
        }否则如果(intent.getAction()。等于(Intent.ACTION_POWER_CONNECTED)){
            // st.power(1,contxt);
        }否则如果(intent.getAction()。等于(Intent.ACTION_POWER_DISCONNECTED)){
            // st.power(0,contxt);
        }否则如果(intent.getAction()。等于(Intent.ACTION_PACKAGE_ADDED)
                || intent.getAction()。等于(Intent.ACTION_PACKAGE_CHANGED)
                || intent.getAction()。等于(Intent.ACTION_PACKAGE_REMOVED)){
            数据库DB =新的数据库(contxt);
            如果(db.open()。preferences(4)){
                乌里数据= intent.getData();
                新ListApps(contxt).import_app(意向,contxt,数据,
                        intent.getAction());
            }
            db.close();
        }否则如果(intent.getAction()。等于(
                ConnectivityManager.CONNECTIVITY_ACTION)){
            CC =新CheckConexion(contxt);
            如果(cc.isOnline()){                / *数据库DB =新的数据库(contxt);
                db.open();
                如果(db.move()== 1){
                    新UploadOffline(contxt);
                }
                db.close(); * /            }
        }    }    公共无效寄存器(上下文C){
        接收机R =新的接收器();
        IntentFilter的I =新的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的应用=新的IntentFilter();
        apps.addAction(Intent.ACTION_PACKAGE_ADDED);
        apps.addAction(Intent.ACTION_PACKAGE_CHANGED);
        apps.addAction(Intent.ACTION_PACKAGE_REMOVED);
        apps.addDataScheme(包);
        c.registerReceiver(R,应用);
    }    公共无效注销(上下文C){
        广播接收器R =新的接收器();
        如果(R!= NULL){
            c.unregisterReceiver(R);
        }
    }
}


解决方案

首先,

使用这个类的对象工作Reciever

删除所有的研究的对象,不叫在扩展类的构造函数。

然后:

定义

 布尔isRegistered = FALSE;

在您的注册方式:

  c.registerReceiver(本);
isRegistered = TRUE;

在你的取消注册方法:

 如果(isRegistered){
    c.unregisterReceiver(本);
    isRegistered = FALSE;
}

然后在你的活动使用类的实例 Reciver

希望,那是有帮助的!

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.

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

解决方案

First of all,

Use this to work with the object of the class Reciever

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

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 of the class Reciver.

Hope, it was helpful!

这篇关于Android的 - 广播接收器unregisterReceiver问题(未注册)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆