IntentRecieverLeakedException,是否缺少调用unregisterReceiver()?在机器人 [英] IntentRecieverLeakedException, Are you missing a call to unregisterReceiver() ? in android
问题描述
我想从以下短信例如: <一href="http://mobiforge.com/developing/story/sms-messaging-android">http://mobiforge.com/developing/story/sms-messaging-android 但如果我尝试发送邮件,我发现了以下情况例外。
例外: 12月2日至7日:38:15.447:ERROR / ActivityThread(839):活动com.micro.MyTest渗漏IntentReceiver com.micro.MyTest$1@435a0c70 最初这里注册。是否缺少调用 unregisterReceiver()? 12月2日至7日:38:15.447:ERROR / ActivityThread(839):android.app.IntentReceiverLeaked:活动com.test.SendSMS已泄露 IntentReceiver com.test.SendSMS $ -1 @ 435a0c70这原是 在这里注册。是否缺少调用unregisterReceiver()? 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread $ PackageInfo $ ReceiverDispatcher(ActivityThread.java:707)。 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread $ PackageInfo.getReceiverDispatcher(ActivityThread.java:535) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:748) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ApplicationContext.registerReceiver(ApplicationContext.java:735) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ApplicationContext.registerReceiver(ApplicationContext.java:729) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.content.ContextWrapper.registerReceiver(ContextWrapper.java:278) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在com.micro.MyTest.sendSMS(SendSMS的.java:98) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在com.micro.MyTest.onCreate(SendSMS的.java:42) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread.access $ 1800(ActivityThread.java:112) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1692) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.os.Handler.dispatchMessage(Handler.java:99) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.os.Looper.loop(Looper.java:123) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在android.app.ActivityThread.main(ActivityThread.java:3948) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在java.lang.reflect.Method.invokeNative(本机方法) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在java.lang.reflect.Method.invoke(Method.java:521) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 12月2日至7日:38:15.447:ERROR / ActivityThread(839):在dalvik.system.NativeStart.main(本机方法) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):活动com.micro.MyTest渗漏IntentReceiver com.test.MyTest$2@435a13b8 最初这里注册。是否缺少调用 unregisterReceiver()? 12月2日至7日:38:15.496:ERROR / ActivityThread(839):android.app.IntentReceiverLeaked:活动com.test.SendSMS已泄露 IntentReceiver com.test.SendSMS $ -2 @ 435a13b8这原是 在这里注册。是否缺少调用unregisterReceiver()? 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread $ PackageInfo $ ReceiverDispatcher(ActivityThread.java:707)。 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread $ PackageInfo.getReceiverDispatcher(ActivityThread.java:535) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:748) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ApplicationContext.registerReceiver(ApplicationContext.java:735) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ApplicationContext.registerReceiver(ApplicationContext.java:729) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.content.ContextWrapper.registerReceiver(ContextWrapper.java:278) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在com.micro.MyTest.sendSMS(SendSMS的.java:129) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在com.micro.MyTest.onCreate(SendSMS的.java:42) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread.access $ 1800(ActivityThread.java:112) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1692) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.os.Handler.dispatchMessage(Handler.java:99) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.os.Looper.loop(Looper.java:123) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在android.app.ActivityThread.main(ActivityThread.java:3948) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在java.lang.reflect.Method.invokeNative(本机方法) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在java.lang.reflect.Method.invoke(Method.java:521) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 12月2日至7日:38:15.496:ERROR / ActivityThread(839):在dalvik.system.NativeStart.main(本机方法)
它发送短信方式
:
publicvoid sendSMS(字符串phoneNumber的,字符串消息){
字符串SENT =SMS_SENT;
字符串DELIVERED =SMS_DELIVERED;
PendingIntent sentPI = PendingIntent.getBroadcast(此,0,新意图(SENT),0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,新的意向(送到),0);
//当短信已发送,以下行(第98行)抛出异常
registerReceiver(
新的BroadcastReceiver(){
公共无效的onReceive(背景为arg0,意图ARG1){
开关(的getResult code()){
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS发送,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),一般故障,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),无服务,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),空的PDU,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),无线电关,Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(发送));
//当SMS已交付
registerReceiver(
新的BroadcastReceiver(){
公共无效的onReceive(背景为arg0,意图ARG1){
开关(的getResult code()){
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS递送,Toast.LENGTH_SHORT).show();
打破;
案例Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),短信未交付,Toast.LENGTH_SHORT).show();
打破;
}
}
},新的IntentFilter(交付));
SmsManager的短信= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
}
在您的活动类,声明这些全局变量:
私人BroadcastReceiver的sendBroadcastReceiver;
私人BroadcastReceiver的deliveryBroadcastReceiver;
字符串SENT =SMS_SENT;
字符串DELIVERED =SMS_DELIVERED;
覆盖您的活动的onCreate并注册您的接收器:
@覆盖
保护无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
sendBroadcastReceiver =新的BroadcastReceiver()
{
公共无效的onReceive(背景为arg0,意图ARG1)
{
开关(的getResult code())
{
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS发送,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),一般故障,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(),无服务,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),空的PDU,Toast.LENGTH_SHORT).show();
打破;
案例SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),无线电关,Toast.LENGTH_SHORT).show();
打破;
}
}
};
deliveryBroadcastReceiver =新的BroadcastReceiver()
{
公共无效的onReceive(背景为arg0,意图ARG1)
{
开关(的getResult code())
{
案例Activity.RESULT_OK:
Toast.makeText(getBaseContext(),SMS递送,Toast.LENGTH_SHORT).show();
打破;
案例Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(),短信未交付,Toast.LENGTH_SHORT).show();
打破;
}
}
};
registerReceiver(deliveryBroadcastReceiver,新的IntentFilter(交付));
registerReceiver(sendBroadcastReceiver,新的IntentFilter(发送));
}
接下来,您发送短信的方法只剩下6线code:
公共无效sendSMS(字符串phoneNumber的,字符串消息)
{
字符串SENT =SMS_SENT;
字符串DELIVERED =SMS_DELIVERED;
PendingIntent sentPI = PendingIntent.getBroadcast(此,0,新意图(SENT),0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,新的意向(送到),0);
SmsManager的短信= SmsManager.getDefault();
sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);
}
最后,注销您的接收器上停止你的活动:
@覆盖
保护无效的onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}
I'm trying the following sms example from: http://mobiforge.com/developing/story/sms-messaging-android but I'm getting the following exception if I try to send messages.
Exception: 02-07 12:38:15.447: ERROR/ActivityThread(839): Activity com.micro.MyTest has leaked IntentReceiver com.micro.MyTest$1@435a0c70 that was originally registered here. Are you missing a call to unregisterReceiver()? 02-07 12:38:15.447: ERROR/ActivityThread(839): android.app.IntentReceiverLeaked: Activity com.test.SendSMS has leaked IntentReceiver com.test.SendSMS $1@435a0c70 that was originally registered here. Are you missing a call to unregisterReceiver()? 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.(ActivityThread.java:707) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:535) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:748) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:735) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:729) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:278) 02-07 12:38:15.447: ERROR/ActivityThread(839): at com.micro.MyTest.sendSMS(SendSMS .java:98) 02-07 12:38:15.447: ERROR/ActivityThread(839): at com.micro.MyTest.onCreate(SendSMS .java:42) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread.access$1800(ActivityThread.java:112) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.os.Handler.dispatchMessage(Handler.java:99) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.os.Looper.loop(Looper.java:123) 02-07 12:38:15.447: ERROR/ActivityThread(839): at android.app.ActivityThread.main(ActivityThread.java:3948) 02-07 12:38:15.447: ERROR/ActivityThread(839): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 12:38:15.447: ERROR/ActivityThread(839): at java.lang.reflect.Method.invoke(Method.java:521) 02-07 12:38:15.447: ERROR/ActivityThread(839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 02-07 12:38:15.447: ERROR/ActivityThread(839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 02-07 12:38:15.447: ERROR/ActivityThread(839): at dalvik.system.NativeStart.main(Native Method) 02-07 12:38:15.496: ERROR/ActivityThread(839): Activity com.micro.MyTest has leaked IntentReceiver com.test.MyTest$2@435a13b8 that was originally registered here. Are you missing a call to unregisterReceiver()? 02-07 12:38:15.496: ERROR/ActivityThread(839): android.app.IntentReceiverLeaked: Activity com.test.SendSMS has leaked IntentReceiver com.test.SendSMS $2@435a13b8 that was originally registered here. Are you missing a call to unregisterReceiver()? 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.(ActivityThread.java:707) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:535) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ApplicationContext.registerReceiverInternal(ApplicationContext.java:748) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:735) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ApplicationContext.registerReceiver(ApplicationContext.java:729) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:278) 02-07 12:38:15.496: ERROR/ActivityThread(839): at com.micro.MyTest.sendSMS(SendSMS .java:129) 02-07 12:38:15.496: ERROR/ActivityThread(839): at com.micro.MyTest.onCreate(SendSMS .java:42) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread.access$1800(ActivityThread.java:112) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.os.Handler.dispatchMessage(Handler.java:99) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.os.Looper.loop(Looper.java:123) 02-07 12:38:15.496: ERROR/ActivityThread(839): at android.app.ActivityThread.main(ActivityThread.java:3948) 02-07 12:38:15.496: ERROR/ActivityThread(839): at java.lang.reflect.Method.invokeNative(Native Method) 02-07 12:38:15.496: ERROR/ActivityThread(839): at java.lang.reflect.Method.invoke(Method.java:521) 02-07 12:38:15.496: ERROR/ActivityThread(839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 02-07 12:38:15.496: ERROR/ActivityThread(839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 02-07 12:38:15.496: ERROR/ActivityThread(839): at dalvik.system.NativeStart.main(Native Method)
The method which sends sms
:
publicvoid sendSMS(String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
// When the SMS has been sent, the following line (line 98) throws Exception
registerReceiver(
new BroadcastReceiver(){
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() {
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));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
in your activity class, declare these global variables:
private BroadcastReceiver sendBroadcastReceiver;
private BroadcastReceiver deliveryBroadcastReceiver;
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
override onCreate of your activity and register your receivers:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
sendBroadcastReceiver = new BroadcastReceiver()
{
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;
}
}
};
deliveryBroadcastReceiver = new BroadcastReceiver()
{
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;
}
}
};
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));
}
next, your send sms method is left with only 6 lines of code:
public void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
lastly, unregister your receivers on stop of your activity:
@Override
protected void onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}
这篇关于IntentRecieverLeakedException,是否缺少调用unregisterReceiver()?在机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!