我正在开发一个应用程序,我在后台点击按钮发送短信。代码如下:
点击
@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);
}
}
现在需要该应用的是:
我在屏幕上还有一个按钮作为停止按钮。 我想在点击该停止按钮时停止异步任务,并想要调用新活动。
请指导我现在应该如何处理您的宝贵建议。
答案 0 :(得分:0)
您是否一次发送一条短信或更多短信?我的意思是实际情况是什么?
如果您要发送一条短信,那么您将无法停止/取消此AsyncTask,因为只要您点击它,它就会立即执行doInBackground()。
如果您要从doInBackground()
发送多条短信,那么您可以在isCancelled()
内查看doInBackground()
,但只有当您点击按钮上的AsyncTask asyncTask.cancel(true);
时,它才会成立