【发布时间】: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