0

状況:

ボタンがクリックされるたびにSMSメッセージを送信するService(も使用してみました)があります。メソッドが一度だけ呼び出されるとActivity99.9%確信しています。これは、アプリが強制的に停止されるまで、画面上の滞在中に数秒からほぼ反抗的に作成されます。が一定期間画面に表示されたままになり、SMSが正常に送信されます。フェードインとフェードアウトが少しわかります。sendSMS(...)ToastsBroadcastReceiver"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);
}

/編集:この質問のコメントで読むことができるように問題を修正しました

4

3 に答える 3

0

このアプローチを使用して、トーストを。未満で表示できますToast.LENGTH_SHORT

于 2012-09-07T09:01:11.563 に答える
0

2回呼び出していsendSMS()た、犯人は私のサービスの非推奨onStart()のメソッドでした

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

この答えは私が両方の方法を維持するのに役立ちました

于 2012-09-07T09:31:12.323 に答える
-3

これを使用して表示します

Toast.makeText(context、message、1000).show();

ここで、1000はミリ秒単位の期間です。

于 2012-09-07T09:01:35.803 に答える