SMSの送信にこのコードを使用しています。
private void sendSMS(String phoneNumber, String message) {
System.out.println("SMS: " + message);
System.out.println("to " + phoneNumber);
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---
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));
// SmsManager sms = SmsManager.getDefault();
// sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts = sm.divideMessage(message);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
sm.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
deliveryIntents);
}
}
このエラーが発生する場合もあれば、エラーがまったく発生せずに正常に再生される場合もあります。
android.app.IntentReceiverLeaked: Activity com.example.senddemo.MainActivity has leaked IntentReceiver com.example.senddemo.MainActivity$6@40548d50 that was originally registered here. Are you missing a call to unregisterReceiver()?
12-23 15:23:32.800 E/ActivityThread(27024): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:756)
12-23 15:23:32.800 E/ActivityThread(27024): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:551)
12-23 15:23:32.800 E/ActivityThread(27024): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:836)
12-23 15:23:32.800 E/ActivityThread(27024): at android.app.ContextImpl.registerReceiver(ContextImpl.java:823)
12-23 15:23:32.800 E/ActivityThread(27024): at android.app.ContextImpl.registerReceiver(ContextImpl.java:817)
12-23 15:23:32.800 E/ActivityThread(27024): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
12-23 15:23:32.800 E/ActivityThread(27024): at com.example.senddemo.MainActivity.sendSMS(MainActivity.java:331)
12-23 15:23:32.800 E/ActivityThread(27024): at com.example.senddemo.MainActivity.access$0(MainActivity.java:290)
12-23 15:23:32.800 E/ActivityThread(27024): at com.example.senddemo.MainActivity$2.onClick(MainActivity.java:120)
12-23 15:23:32.800 E/ActivityThread(27024): at android.view.View.performClick(View.java:2552)
12-23 15:23:32.800 E/ActivityThread(27024): at android.view.View$PerformClick.run(View.java:9229)
12-23 15:23:32.800 E/ActivityThread(27024): at android.os.Handler.handleCallback(Handler.java:587)
12-23 15:23:32.800 E/ActivityThread(27024): at android.os.Handler.dispatchMessage(Handler.java:92)
12-23 15:23:32.800 E/ActivityThread(27024): at android.os.Looper.loop(Looper.java:138)
12-23 15:23:32.800 E/ActivityThread(27024): at android.app.ActivityThread.main(ActivityThread.java:3701)
12-23 15:23:32.800 E/ActivityThread(27024): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 15:23:32.800 E/ActivityThread(27024): at java.lang.reflect.Method.invoke(Method.java:507)
12-23 15:23:32.800 E/ActivityThread(27024): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-23 15:23:32.800 E/ActivityThread(27024): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
12-23 15:23:32.800 E/ActivityThread(27024): at dalvik.system.NativeStart.main(Native Method)
12-23 15:23:32.810 E/ActivityThread(27024): Activity com.example.senddemo.MainActivity has leaked IntentReceiver com.example.senddemo.MainActivity$5@405486e0 that was originally registered here. Are you missing a call to unregisterReceiver()?
xmlファイルまたはOnDestry関数に何かを追加する必要がありますか?私は放送受信機に慣れていないからです。なぜそれが時々再生され、他のいくつかは再生されないのですか?