2012-08-09 74 views
0

我正在开发APP发送短信号码,如果短信失败的Asynctask执行和检查时间间隔重新发送短信,但是,我所有的工作浪费,因为我不明白Asynctask 这里是我的码。短信重发+异步任务

异步类

 class checkList extends AsyncTask<String, Void, Void> { 
     public Void doInBackground(String... p) { 
      while (true) { 
       already_running_async=true; 
       sms.sendTextMessage(phone_nr, null, "SmS Sending", sentPI, null); 

       try { 
       Thread.sleep(10000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       if (isCancelled()) break; 

      } 
     return null; 
     } 
     }; 

短信发送代码和错误处理

 bol=false; 
    already_running_async=false; 
    check=false; 

sms = SmsManager.getDefault(); 
     if(!phone_nr.equals("")) 
      sms.sendTextMessage(phone_nr, null,"Sms body" , sentPI, null); 
     sendBroadcastReceiver = new BroadcastReceiver(){ 
     @Override 
      public void onReceive(Context context, Intent intent) { 
       switch (getResultCode()) 
       { 
        case Activity.RESULT_OK: 
         //sms sent if bol true cancel the Asynctask :) 
        if(bol){ 
        new checkList().cancel(true);} 
        break; 
        case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
         if(already_running_async==false){ 
         new checkList().execute(); 
         bol=true;} 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         if(already_running_async==false){ 
         new checkList().execute(); 
         bol=true;} 
         break; 
        case SmsManager.RESULT_ERROR_NULL_PDU: 
         if(already_running_async==false){ 
         new checkList().execute(); 
         bol=true;} 
         break; 
        case SmsManager.RESULT_ERROR_RADIO_OFF: 
         if(already_running_async==false){ 
         new checkList().execute(); 
         bol=true;} 
         break; 
       } 

      } 
     }; 
     registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT)); 

问题: 任务启动,但任务仍然活着,并在10秒我多次发送短信也取消了取消,但asyntask在取得成功后没有取消短信发送

请帮助我还没有睡,因为这个问题对夜:(

问候

+0

请说明你的实际问题是什么。文字没有发送吗?该任务是否不启动?是什么东西引发错误? – thegrinner 2012-08-09 12:13:55

+0

你好,任务启动,但任务仍然活着,并在10秒后重复发送短信我也放了取消,但asyntask不成功取消发送成功后短信 – Fawad 2012-08-09 12:15:59

回答

0

此情况下,创建一个新的的AsyncTask被立即取消。

case Activity.RESULT_OK: 
    //sms sent if bol true cancel the Asynctask :) 
    if(bol){ 
     new checkList().cancel(true); 
    } 
    break; 

要取消的AsyncTask您需要将其存储在一个变量,像

checkList smsTask = new checkList(); 
smsTask.execute(); 

// Other app logic 

// Replacing the internal portion of that case is the new cancel code 
if (bol) { 
    smsTask.cancel(true); 
} 
+0

意味着只有逻辑是创建一个异步任务的对象?其余代码是完美的重新发送/重试失败的短信?请你也可以口头检查和模拟我的代码thanx – Fawad 2012-08-09 12:34:14

+0

问题不在于任务创建时,而在于不存储对其的引用。此外,您可能需要考虑将检查发送成功或失败的逻辑移入AsyncTask本身。然后,任务可以确定是否需要继续尝试发送,如果您想暂停尝试,则只需从外部取消即可。另外,通过检查短信状态,您的主线程将不会被阻止。我不确定如果其余的代码是完美的,而没有实际构建和编译所有东西,但它*看起来*确定。 – thegrinner 2012-08-09 13:10:36

+0

我已经创建了AsyncTask的对象,但是它说async任务的最终对象是正常的,并且以这种方式Asynctask smstask根本不由smstask.execute()开始; – Fawad 2012-08-09 13:13:21