2012-12-17 23 views
0

我有一個類中調用下載該擴展的AsyncTask。 的OnPreExecute方法執行以下操作:奇怪的行爲的AsyncTask

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    this.dialog.setTitle("Check updates..."); 

    this.dialog.show(); 

} 

列出的對話框instantied在類的構造函數,並具有以下特點酒色:

dialog = new ProgressDialog(activity); 
dialog.setCancelable(false); 

在doInBackground方法,我會做很多的網絡操作,我會每次我可以從期望的url下載圖像時調用進度更新方法:

protected void onProgressUpdate(String... values) 
// TODO Auto-generated method stub 
    super.onProgressUpdate(values); 



    //call the onprogress update 
    publishProgress("1000"); 

    //do a lot of stuff with the network 


} 

In t所以基本上

 protected void onProgressUpdate(String... values) { 
     // TODO Auto-generated method stub 
     super.onProgressUpdate(values); 

     if(values[0].equals("1000")){ 

      dialog.dismiss(); 

        progress_brand.show(); 

      progress_brand.setProgress(progress_brand.getProgress()+1); 

      if(progress_brand.getProgress() == progress_brand.getMax()){ 

       progress_brand.dismiss(); 

      } 

     } 
} 

:他onprogressupdate我會解僱創建的第一個對話,我會告訴另外一個在的AsyncTask開始我展示了標題爲「檢查更新」對話框...然後我會尋找在doInBackground方法這些更新,如果我會找到一些,我將使用發佈進度駁回「老對話」,並創建一個新的與ProgressDialog.STYLE_HORIZONTAL。最後一個對話框每次我都會從網上下載。

所以這裏的問題。如果我用eclipse運行應用程序,然後在下載過程中,我會暫停應用程序一切工作正常。如果我第二次重新輸入應用程序,我可以看到下載繼續完美,我可以看到第二個進度欄繼續按預期進行更新。

但是,如果我做一個簽名的apk - >通過APK安裝應用程序 - >啓動應用程序 - >把它放在暫停下載過程 - >重新進入應用程序,那麼第一個對話框再次顯示,下載無法正常進行。 我從logcat中看到,如果我將從eclipse運行應用程序,onpreexecute方法僅被調用一次,即使我將退出並重新輸入應用程序。 但是,如果我會通過APK的onpreexecute方法被調用每次我會退出,然後再安裝應用程序重新啓動應用程序。

這是爲什麼發生?我試圖清理項目和其他基本操作,看看問題是否是該apk的創建,但沒有結果。

+0

您可能想要顯示(或解釋)從哪裏開始下載任務。 – Luksprog

+0

請記住,每當您重新進入應用程序時,都會創建該活動,因此onResume方法將被調用,您在哪裏放置了asyncktask.start方法? – vsm

+0

我從項目的主要活動(oncreate方法)開始下載任務:if(isOnline()){ \t \t final下載d = new Download(this,this.getApplicationContext()); \t \t d.execute(「」); \t \t} – jiraya85

回答

0

不,你不要在你的AnyTask使用ProgressDialog

嘗試這個(例如)

public class Updated extends Activity { 
    /** 
    * ProgressDialog which is shown 
    */ 
    private ProgressDialog progessDialog_g; 
    private boolean downloadUses = false; 


    /** 
    * Instance of the BroadcastReceiver 
    */ 
    private BroadcastReceiver receiver_g; 
    private IntentFilter iFilter; 
    protected ServiceConnection mServerConn = new ServiceConnection() { 
     public void onServiceConnected(ComponentName name, IBinder binder) { 

     } 

     public void onServiceDisconnected(ComponentName name) { 

     } 

    }; 
    private Intent sI; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.progress); 

     progessDialog_g = new ProgressDialog(this); 

     // Reads and sets the settings for the ProgressDialog 

     // Create the IntentFilter for the different broadcast messages 
     iFilter = new IntentFilter(
       ProgressService.PROGRESS_DIALOG_BROADCAST_FINISH); 
     // Creates the BroadcastReceiver 
     receiver_g = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 

       if (ProgressService.PROGRESS_DIALOG_BROADCAST_FINISH 
         .equals(intent.getAction())) { 
        // Finishs the ProgressDialog 
        progessDialog_g.cancel(); 
        Finish(); 

       } 
      } 
     }; 




    } 



    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    protected void onResume() { 

     sI = new Intent(this, ProgressService.class); 
     this.bindService(sI, mServerConn, Context.BIND_AUTO_CREATE); 
     this.startService(sI); 
     // Registers the BroadcastReceiver 
     registerReceiver(receiver_g, iFilter); 
     if (downloadUses) { 
      downloadUses = false; 
      Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 
     } else { 

      progessDialog_g.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
      progessDialog_g.setMessage("you messege"); 
      progessDialog_g.show(); 

      new DownloadJSONTask(this, sI) 
        .execute(Initialize.server_url+"/changes/update/1"); 

     } 

     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     this.stopService(new Intent(this, ProgressService.class)); 
     this.unbindService(mServerConn); 
     unregisterReceiver(receiver_g); 
     super.onPause(); 
    } 

    private void Finish() { 

     Intent in = new Intent(this, RegionsActivity.class); 
     startActivity(in); 
     downloadUses = true; 
    } 

} 

} 

public class ProgressService extends IntentService { 

    public static final String PROGRESS_DIALOG_BROADCAST_FINISH = "Dialog.Progress.MyKey.Finish"; 

    public ProgressService() { 
     super("ExampleProgressService"); 
    } 

    /** 
    * Send the finish message. 
    */ 
    private void closeProgressActivity() { 
     Intent intent = new Intent(PROGRESS_DIALOG_BROADCAST_FINISH); 

     sendBroadcast(intent); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     // extractVariablesFromIntentAndPrepare(intent); 

     String action = intent.getStringExtra("Action"); 

     if ("0".equals(action)) { 

      closeProgressActivity(); 

     } 

    } 

} 

,並在您AnyTask

sI.putExtra("Action", "0"); 
     context.startService(sI); 

在你身邊anifest

<service android:name=".Intent.ProgressService" /> 
+0

對不起,能否請您提供有關此解決方案的更多詳細信息?我無法理解它 – jiraya85