我如何检测onReceive方法何时完成调用?

时间:2018-07-16 10:32:30

标签: android sms

我想在收到所有交货报告后停止搅拌器。我已经尝试过

if(DELIVERED.isEmpty()){

        stopPulse();
        showSuccessImage();

    }

,但不确定是否可以正常工作。当所有短信发送完毕后,有人可以帮助我阻止您吗? TIA。

private void MultipleSMS(final String phoneNumber, final String message) {


    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:
                    ContentValues values = new ContentValues();
                    for (int i = 0; i < MobNumber.size(); i++) {
                        values.put("address", MobNumber.get(i).toString());
                        values.put("body", message);
                        if(i == MobNumber.lastIndexOf(phoneNumber)){

                            stopPulse();
                        }
                    }
                    getContentResolver().insert(
                            Uri.parse("content://sms/sent"), values);

                    isSucess = true;


                    //stopPulse();

                    //showSuccessImage();


                    Toast.makeText(getBaseContext(), "SMS sent",
                            Toast.LENGTH_SHORT).show();


                    //callUpdateApi(msisdn);

                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                    showFailedImage();

                    Log.d("generic", "generic fail");
                    stopPulse();
                    Toast.makeText(getBaseContext(), "Generic failure",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:

                    if (pulsator.isStarted()) {

                        pulsator.stop();
                    }

                    showFailedImage();
                    Log.d("no service", "service fail");
                    stopPulse();
                    Toast.makeText(getBaseContext(), "No service",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:

                    showFailedImage();
                    Log.d("null PDU", "api Fail");
                    stopPulse();
                    Toast.makeText(getBaseContext(), "Null PDU",
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    showFailedImage();
                    Log.d("radio off", "api Fail");
                    stopPulse();
                    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:

                    showFailedImage();
                    Toast.makeText(getBaseContext(), "SMS not delivered",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(DELIVERED));


    if(DELIVERED.isEmpty()){

        stopPulse();
        showSuccessImage();

    }




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

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);
        }
    }


}

0 个答案:

没有答案