如何使用活动类在AsyncTask中发送SMS?

时间:2013-03-19 14:50:52

标签: android android-asynctask sms

我需要从AsyncTask发送短信。所以我创建了一个名为SMS的活动。 我的AsyncTask我得到了这个构造函数:

public AsyncTask(Context pContext, TextView pTxtWorkProgress, TextView pTxtMsg, ProgressBar pProgressbar_line, SMS pSmsActivity){
        context = pContext;
        txtMsg = pTxtMsg;
        txtWorkProgress = pTxtWorkProgress;
        progressbar_line = pProgressbar_line;
        smsActivity = pSmsActivity;
    }

然后,在我的doInBackground()中,我得到了代码:

if(smsActivity != null)
 smsActivity.sendSMS("9999", "4444");
else
 hasErrorSms = true;

在我的smsActivity中,我得到了方法sendSMS():

public void sendSMS(String phoneNumber, 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:
                    Toast.makeText(getBaseContext(), "SMS sent", Toast.LENGTH_SHORT).show();
                    Util.mSMS_SENT_STATUS = Util.mSMS_SENT_RESULT_OK;
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                    Util.mSMS_SENT_STATUS = Util.mSMS_SENT_RESULT_ERROR_GENERIC_FAILURE;                        
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                    Util.mSMS_SENT_STATUS = Util.mSMS_SENT_RESULT_ERROR_NO_SERVICE;                        
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                    Util.mSMS_SENT_STATUS = Util.mSMS_SENT_RESULT_ERROR_NULL_PDU;                        
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                    Util.mSMS_SENT_STATUS = Util.mSMS_SENT_RESULT_ERROR_RADIO_OFF;                        
                    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:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

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

但它没有用,我得到一个NullPointerException,当我想在SMS课程中获得一个Intent。

这就是我调用AsyncTask的方式:

new AsyncTask(this, txtWorkProgress, txtMsg, progressbar_line, new SMS()).execute(larr_what_have_to_upate_in_device.toString(), lCD_DEVIC);

我知道新短信()无效,有人可以告诉我该怎么做? 感谢。

1 个答案:

答案 0 :(得分:1)

你报告说崩溃是在线发生的:

PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);

我过去曾成功设置过这样的意图:

public static final String SENT = "SMS_SENT";
public static final String DELIVERED = "SMS_DELIVERED";

final String name = "Some-Name";
final PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
    (new Intent(SENT)).putExtra("name", name),
    PendingIntent.FLAG_UPDATE_CURRENT);
final PendingIntent delivedPI = PendingIntent.getBroadcast(this, 0,
    (new Intent(DELIVERED)).putExtra("name", name),
    PendingIntent.FLAG_UPDATE_CURRENT);

因此,请尝试添加额外数据namePendingIntent.FLAG值。