Можно ли использовать BroadcastReceiver внутри AsyncTask - PullRequest
0 голосов
/ 01 декабря 2018

Я пытаюсь отправить форму SMS внутри AsyncTask.Мне нужно получить отправленный и доставленный результат, поэтому я должен использовать BroadcastReceiver.Проблема в том, что я не могу отправить resultCode из braodcaseReceiever обратно в вызываемую деятельность или службу (фактически, вызывающая сторона является службой).

Я пытался использовать onPostExecute, но не удача,поэтому мой вопрос, где я должен использовать приемник вещания?

Код моего AsnycTask выглядит следующим образом:

private class SmsTask extends AsyncTask<String, Void, Integer> {
    private Context mContext;
    private Integer sentStatus = 100;

    public SmsTask(Context conctext) {
        mContext = conctext;
    }

    protected boolean sendSMS(String number) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        // Set pending intents to broadcast
        PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(SENT), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0, new Intent(DELIVERED), 0);

        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                sentStatus = getResultCode();
                switch (sentStatus)
                {
                    case Activity.RESULT_OK:
                        Log.i("IMMM", "SMS sent");
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Log.i("IMMM", "Generic failure");
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Log.i("IMMM", "No service");
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Log.i("IMMM", "Generic failure");
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Log.i("IMMM", "Radio off");
                        break;
                }
            }
        }, new IntentFilter(SENT));

        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Log.i("IMMM", "SMS delivered");
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.i("IMMM", "SMS not delivered");
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));


        String destinationAddress = number;
        String smsMessage  = String.format("This is test sent to %s", number);
        String scAddress = null;

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(destinationAddress, scAddress, smsMessage, sentPI, deliveredPI);
        return true;
    }

    @Override
    protected Integer doInBackground(String... params) {
        sendSMS(params[0]);
        return null;
    }

    @Override
    protected void onPostExecute(Integer result) {
        //here obviously is not the right place, it's being called way before we get status of sent message.
        //it's always showing the default value 100 define up.
        Log.i("IMMM", "Sent Status: "+ sentStatus.toString());
    }

    @Override
    protected void onPreExecute() {}
}
...