状況:
ボタンがクリックされるたびにSMSメッセージを送信するService
(も使用してみました)があります。メソッドが一度だけ呼び出されるとActivity
99.9%確信しています。これは、アプリが強制的に停止されるまで、画面上の滞在中に数秒からほぼ反抗的に作成されます。が一定期間画面に表示されたままになり、SMSが正常に送信されます。フェードインとフェードアウトが少しわかります。sendSMS(...)
Toasts
BroadcastReceiver
"SMS Sent"
Toast
私は何か間違ったことをしていますか?
ボーナス:
ステータスのあるが応答を受け取らBroadcastReceiver
ないのはなぜですか?"SMS delivered"
これが私がずっと前にチュートリアルからつかんだ非常に一般的なコードです:
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
sendSMS("5555555","hello world");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
sendSMS("5555555","hello world");
return Service.START_STICKY;
}
// ---sends an SMS message to another device---
private void sendSMS(final String phoneNumber, final String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this.getApplicationContext(), 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this.getApplicationContext(), 0,
new Intent(DELIVERED), 0);
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "SMS error: Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "SMS error: No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "SMS error: Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "SMS error: Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
/編集:この質問のコメントで読むことができるように問題を修正しました