[英]how can i unregister receiver in android?
i got this error thing Leaked Intent Receiver that was originally registered here
, but the apps is still up and running even with that error kind of thing, what causes that error ? 我收到了
Leaked Intent Receiver that was originally registered here
此错误信息Leaked Intent Receiver that was originally registered here
,但是即使出现此类错误,应用程序仍可以启动并运行,是什么导致该错误? and does this affects the apps ? 这会影响应用程序吗?
here's what im trying to do : 这是我想做的事情:
private void sendSMS(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
try {
SmsManager sms = SmsManager.getDefault();
ArrayList<String> mSMSMessage = sms.divideMessage(message);
for (int i = 0; i < mSMSMessage.size(); i++) {
sentPendingIntents.add(i, sentPI);
deliveredPendingIntents.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,
sentPendingIntents, deliveredPendingIntents);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "SMS sending failed...", Toast.LENGTH_SHORT)
.show();
}
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
}
tnx in advance.. 提前发送..
If you have registered the receiver in the onCreate
then unregister it in it's onDestroy
method. 如果您已经在
onCreate
注册了接收器,请在其onDestroy
方法中将其注销。
If you have registered the receiver in the onResume
then unregister it in it's onPause
method. 如果您已经在
onResume
注册了接收器,那么请在其onPause
方法中注销它。
If you have registered the receiver in the onStart
then unregister it in it's onStop
method. 如果您已经在
onStart
注册了接收器,请在其onStop
方法中注销它。
The problem is likely because the Activity
you are calling sendSMS
from has exited, but that is the context in which you have registered your receivers as anonymous inner classes. 该问题可能是因为您正在
sendSMS
调用sendSMS
的Activity
已退出,但这是您已将接收者注册为匿名内部类的上下文。 You'll need to pull those receivers out, or retain references to them and unregister them in your onPause()
. 您需要拉出那些接收者,或者保留对它们的引用,然后在
onPause()
注销它们。
Since the receiver is being registered inside an activity and not on the manifest, you need to unregister it onPause or onStop 由于接收者是在活动中而不是在清单中进行注册,因此您需要在onPause或onStop上取消注册。
public void onStop() {
super.onStop();
if (myReceiver != null) {
unregisterReceiver(myReceiver);
}
I forgot to mention that you should created the BroadcastReceiver inside an inner class such as 我忘了提到您应该在内部类(例如)中创建BroadcastReceiver
private class myReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//Add your code here
}
}
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.