我得到了这个错误的东西Leaked Intent Receiver that was originally registered here,但应用程序仍然启动和运行,即使有这个错误的东西,是什么导致了这个错误?这对应用程序有影响吗?
这就是我想要做的:
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 ..
发布于 2015-05-26 03:49:52
onCreate中注册接收器,则在其onDestroy方法中注销它。onResume中注册接收器,则在其onPause方法中注销它。onStart中注册接收器,则在其onStop方法中注销它。发布于 2014-12-05 00:27:15
这个问题很可能是因为您从中调用sendSMS的Activity已经退出,但这是您将接收器注册为匿名内部类的上下文。您需要取出这些接收器,或者保留对它们的引用并在您的onPause()中注销它们。
发布于 2014-12-05 00:33:14
由于接收方是在活动内部注册的,而不是在清单中注册的,因此需要取消注册onPause或onStop
public void onStop() {
super.onStop();
if (myReceiver != null) {
unregisterReceiver(myReceiver);
}我忘记提到应该在内部类中创建BroadcastReceiver,比如
private class myReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//Add your code here
}
}希望能有所帮助。
https://stackoverflow.com/questions/27298700
复制相似问题