gpt4 book ai didi

android - IntentRecieverLeakedException,您是否错过了对 unregisterReceiver() 的调用?在安卓中

转载 作者:IT老高 更新时间:2023-10-28 22:05:47 25 4
gpt4 key购买 nike

我正在尝试以下短信示例: http://mobiforge.com/developing/story/sms-messaging-android但如果我尝试发送消息,我会收到以下异常。

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.<init>(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.<init>(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)

发送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);

}

最佳答案

在您的 Activity 类中,声明这些全局变量:

private BroadcastReceiver sendBroadcastReceiver;
private BroadcastReceiver deliveryBroadcastReceiver;
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

覆盖您的 Activity 的 onCreate 并注册您的接收器:

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

接下来,你的发送短信方法只剩下 6 行代码了:

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

最后,在您的 Activity 停止时取消注册您的接收器:

@Override
protected void onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}

关于android - IntentRecieverLeakedException,您是否错过了对 unregisterReceiver() 的调用?在安卓中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9078390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com