SmsSentReceiver始终在putExtra()中传递相同的值

时间:2017-10-08 07:29:10

标签: android android-intent broadcastreceiver sms

目前我正在构建Android短信网关。到目前为止一切正常。问题是当我在数据库列表循环上发送短信发送短信时。当我收到SmsSentReceiver Broadcast时,它总是具有相同的ID。

我必须提一下,我在Manifest.xml上有适当的权限SEND,RECEIVE,PHONE_STATE,INTERNET

这是我的SendSMS方法:

public String sendSMS(String phoneNumber, String message, Long RowId) {

    Log.e(TAG,"SENDING ROW ID: "+RowId);

    String SENT = "SMS_SENT";
    Intent intentSent = new Intent(SENT);
    intentSent.putExtra("RowId", RowId);
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, intentSent, 0);

    /*String DELIVERED = "SMS_DELIVERED";
    Intent intentDelivered = new Intent(DELIVERED);
    intentDelivered.putExtra("RowId", RowId);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, intentDelivered, 0);*/

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, null); //deliveredPI

    return "SMS:";

}

我的Manifest.xml上有这个

<service android:name=".services.Smsd"></service>

    <receiver android:name=".services.SmsSentReceiver">
        <intent-filter>
            <action android:name="SMS_SENT" />
        </intent-filter>
    </receiver>

我的SmsSentReceiver.java

public class SmsSentReceiver extends BroadcastReceiver {

private static final String TAG = "SmsSentReceiver";

@Override
public void onReceive(Context context, Intent arg1) {


    Log.e(TAG, "++++++++++++++++++++++++++ SmsSentReceiver "+ arg1.getExtras().getLong("RowId") +" +++++++++++++++++++++++++++++++++++++++++");


    if (getResultCode() == Activity.RESULT_OK) {
        try {
            Log.e(TAG, "Sent ...");
            Outbox outbox = SugarRecord.findById(Outbox.class,arg1.getExtras().getLong("RowId"));
            Log.e(TAG, "RESULT_OK");
            Log.e(TAG, String.format("Phone:%s \nBody:%s \nSmsId:%s \nExtra:%s \nLocalExtra:%s \nStatus:%s",
                    outbox.getPhone(),
                    outbox.getBody(),
                    outbox.getSmsId(),
                    arg1.getExtras().getLong("RowId"),
                    outbox.getId(),
                    outbox.getStatus()));

            outbox.Status = "1"; // 1 =success
            outbox.SendingDate = new Date();
            outbox.save();

            // Delete SMS
            context.getContentResolver().delete(Uri.parse("content://sms/inbox"), outbox.getBody(), new String[]{"body"});


        } catch (Exception e) {

            Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            Log.e(TAG, "SmsSentReceiver ERROR: " + e.toString());
            Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

        }


    } else {
        try {

            Log.e(TAG, "Not Sent ...");
            Outbox outbox = SugarRecord.findById(Outbox.class,arg1.getExtras().getLong("RowId"));
            Log.e(TAG, "RESULT_CANCELED");
            Log.e(TAG, String.format("Phone:%s \nBody:%s \nSmsId:%s \nExtra:%s \nLocalExtra:%s \nStatus:%s",
                    outbox.getPhone(),
                    outbox.getBody(),
                    outbox.getSmsId(),
                    arg1.getExtras().getLong("RowId"),
                    outbox.getId(),
                    outbox.getStatus()));

            outbox.Status = "2";
            outbox.SendingDate = new Date();
            outbox.Retries = outbox.getRetries() + 1;
            outbox.save();

            // Delete SMS
            context.getContentResolver().delete(Uri.parse("content://sms/inbox"), outbox.getBody(), new String[]{"body"});

        } catch (Exception e) {

            Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
            Log.e(TAG, "SmsSentReceiver ERROR: " + e.toString());
            Log.e(TAG, "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

        }

*****溶液***** 添加RowId.intValue() 添加PendingIntent.FLAG_ONE_SHOT

设置唯一的请求代码,然后运行一次。 https://developer.android.com/reference/android/app/PendingIntent.html#FLAG_ONE_SHOT

PendingIntent sentPI = PendingIntent.getBroadcast(this, RowId.intValue(), intentSent, PendingIntent.FLAG_ONE_SHOT);

0 个答案:

没有答案