【问题标题】:Android: Stop async task on button clickAndroid:单击按钮时停止异步任务
【发布时间】:2014-07-14 09:29:05
【问题描述】:

我正在开发一个应用程序,通过单击按钮在后台发送短信。代码如下:

点击时

@Override
public void onClick(View v) {
    if(v.getId() == R.id.button_sendsms){
        onSendButtonClick();
    }
}

发送短信方法

public void onSendButtonClick(){ 
    // Send SMS in background to the number entered by user for registration 
    sendSMSInBackground = new SendSMSInBackground();
    sendSMSInBackground.execute();  
}

后台类

private class SendSMSInBackground extends AsyncTask<Void, Void, Void> {
    SeekBar seekBarMessageProgress;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        seekBarMessageProgress = (SeekBar) findViewById(R.id.seekbaar_message_progress_susl_two);
        seekBarMessageProgress.setProgress(progress);
    }

@Override
protected Void doInBackground(Void... arg0) {
    sendSMS("98********", "SMS");
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    /*
     * My task
     */
    try{
        if(sentReceiver == null){}
        else{
            unregisterReceiver(sentReceiver);
        }
        if(deliverReceiver == null){}
        else{
            unregisterReceiver(deliverReceiver);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    // ** My Send SMS code**

    private void sendSMS(String phoneNumber, String randomNumber) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        Intent sentIntent = new Intent(SENT);
        Intent deliveredUntent = new Intent(DELIVERED);

        PendingIntent sentPI = PendingIntent.getBroadcast(SignUpScreenActivity.this, 0,sentIntent, 0);        
        PendingIntent deliveredPI = PendingIntent.getBroadcast(SignUpScreenActivity.this, 0,deliveredUntent, 0);

        //---when the SMS has been sent---
        sentReceiver = 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();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Utility.smsFlag = 2;
                        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;
                }
            }
        };

        //---when the SMS has been delivered---
        deliverReceiver = 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;                      
                }
            }
        }; 

        try{
            registerReceiver(sentReceiver, new IntentFilter(SENT));
            registerReceiver(deliverReceiver, new IntentFilter(DELIVERED));
        } catch (Exception e) {
            e.printStackTrace();
        }

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

现在应用的需求是:

我在屏幕上多了一个按钮作为停止按钮。 我想在单击该停止按钮时停止异步任务并想调用新活动。

请指导我现在应该如何处理您的宝贵建议。

【问题讨论】:

  • 您是一次发送一条短信还是多条短信?
  • 一条短信,只有一次来自我的应用程序。

标签: android android-activity android-asynctask


【解决方案1】:

您是一次发送一条短信还是多条短信?我的意思是实际情况如何?

如果您发送一条短信,那么您将无法停止/取消此 AsyncTask,因为一旦您单击,它将立即执行 doInBackground()。

如果您从doInBackground() 发送多条短信,那么您可以在doInBackground() 中检查isCancelled(),但只有当您在按钮单击时为您的AsyncTask 调用asyncTask.cancel(true); 时才会如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多