[英]Android - BroadcastReceiver unregisterReceiver issue (not registered)
I'm having some problems unregistering a BroadcastReceiver
. 我在取消注册
BroadcastReceiver
遇到了一些问题。 I'm first registering it but then when I want to unregister it by using unregisterReceiver();
我首先注册它,但是当我想通过使用
unregisterReceiver();
取消注册它时unregisterReceiver();
command gives me tons of errors. 命令给了我很多错误。 The error says that I've not registered my
BroadcastReceiver
. 错误说我没有注册我的
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 使用它来处理类Reciever的对象
remove all r objects, 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 of the class Reciver . 然后在您的活动中使用类Reciver的 实例 。
Hope, it was helpful! 希望,这很有帮助!
The best solution is to put the unregisterReceiver
method 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 unregisterReceiver
fails, so you got to use Exception
. 不幸的是,当
unregisterReceiver
失败时,没有抛出特定的异常,所以你必须使用Exception
。 Also, there is unfortunately no method like isReceiverRegistered(BroadcastReceiver receiver)
, which in my opinion would be an enhancement to the Android API. 此外,遗憾的是没有像
isReceiverRegistered(BroadcastReceiver receiver)
这样的方法,在我看来这将是对Android API的增强。
Besides, it won't hurt to take a look at ReceiverLifecycle 此外,看看ReceiverLifecycle也不会有什么坏处
You cannot unregister a new instance of broadcast Receiver. 您无法取消注册广播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);
In unregister method, you are creating a new BroadcastReceiver instance and unregistering that. 在取消注册方法中,您正在创建新的BroadcastReceiver实例并取消注册该实例。 Instead make the instance in register method global and call
而是使register方法中的实例全局并调用
if (r != null) {
c.unregisterReceiver(r);
r = null;
}
in unregister method. 在取消注册方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.