0

このアプリケーションには AppWidget があり、ユーザーがクリックすると SMS が送信されます。ウィジェットをクリックして SMS を送信すると、問題なく動作します。ここで、SMS の送信が成功したか失敗したかを知りたいと思います。どうすればいいですか?

SMS送信時のコードはこちら

String sms2 = id + " " + name + " " + cn.getName() + " " + message
                                    + " " + lati + " " + longi 
                                    + " https://maps.google.com/?q=" + lati + "," + longi + " -alertoapp";
                            String cp = cn.getPhoneNumber();
                            PendingIntent piSent=PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
                            PendingIntent piDelivered=PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
                            SmsManager sms = SmsManager.getDefault();
                            sms.sendTextMessage(cp, null, sms2, piSent, piDelivered);

マニフェスト

<receiver android:name=".Widget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <!-- Broadcast Receiver that will also process our self created action -->
            <action android:name="de.thesmile.android.widget.buttons.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget_provider" />
    </receiver>

SMSが送信されているかどうかにかかわらず、これは乾杯で機能するため、以下のこのメソッドを使用したいのですが、問題はBroadCastReceiverクラスでregisterReceiverメソッドが利用できないことです。

 switch (getResultCode()) {
            case Activity.RESULT_OK:
                clear1();
                clear2();
                clear3();
                clear4();
                Toast.makeText(getBaseContext(), "SMS has been 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;
            default:
                Toast.makeText(getApplicationContext(), "Coordinates is null, Try again", Toast.LENGTH_LONG).show();
                break;
            }

        }
    };
    smsDeliveredReceiver=new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            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(smsSentReceiver, new IntentFilter("SMS_SENT"));
    registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));

何か提案はありますか?

4

1 に答える 1

1

私と同じ問題に遭遇した人には、この助けを借りてこれを修正しまし

だからここにあります

smsSentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent arg1) {
        String msg = "";
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                msg = "SMS has been sent";
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                msg = "Generic Failure";
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                msg = "No Service";
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                msg = "Null PDU";
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                msg = "Radio Off";
                break;
            default:
                msg = "Coordinates is null, Try again";
                break;
        }
        Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }
};

smsDeliveredReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context ctx, Intent arg1) {
        String msg = "";
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                msg = "SMS Delivered";
                break;
            case Activity.RESULT_CANCELED:
                msg = "SMS not delivered";
                break;
        }
        Toast.makeText(ctx.getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }
};

ctx.getApplicationContext()
    .registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
ctx.getApplicationContext()
    .registerReceiver(smsDeliveredReceiver, new IntentFilter("SMS_DELIVERED"));

結論: したがって、BroadcastReceiver クラス内にレシーバーを登録します。

それ以外のregisterReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

使用するcontext.getApplicationContext().registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));

于 2014-01-31T09:00:28.050 に答える