2017-01-10 85 views
0

當我打開新的Activity我運行新的AsyncTask實時更新TextView。當Activity完成時AsyncTask被取消。當我打開並關閉這個Activity兩次時,一切運行正確。但第三次和其他 - doInBackground方法永遠不會啓動,儘管任務狀態是「正在運行」。代碼看起來像這樣。爲什麼AsyncTask只能正常工作2次?

打開活動:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
      final String selectedFromList = (list.getItemAtPosition(position).toString()); 

      Context context = getApplicationContext(); 
      Intent intent = new Intent(context, Main2Activity.class); 
      intent.putExtra("NetworkText", txts.getText()); 
      intent.putExtra("ChooseCompTitle", new String(selectedFromList)); 
      startActivity(intent); 
      overridePendingTransition(R.anim.right_in, R.anim.left_out); 
     } 
    }); 

「的onCreate」 開始執行的AsyncTask:

ConnectionDataPack supportDesktopData; 
UpdateTextView taskUpdateText; 

protected void onCreate(Bundle savedInstanceState) { 
    . . . . 

    final TextView txts = (TextView) findViewById(R.id.textView2); 
    final ScrollView scroll = (ScrollView) findViewById(R.id.ScrollView01); 
    final TextView textContent = (TextView) findViewById(R.id.textView4); 

    if(getIntent().getExtras().getString("NetworkText") != null){ 
     txts.setText(getIntent().getExtras().getString("NetworkText")); 
    } 

    if(getIntent().getExtras().getString("ChooseCompTitle") != null){ 
     supportDesktopData = findFitCompPack(getIntent().getExtras().getString("NetworkText"), MainActivity.connectComputer); 
    } 
    else finish(); 

    taskUpdateText = new UpdateTextView(supportDesktopData, textContent, scroll); 
    taskUpdateText.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

    if(taskUpdateText.getStatus() == AsyncTask.Status.PENDING){ 
     Log.i("LogsX", "AsyncTask has not started yet."); 
    } 
    if(taskUpdateText.getStatus() == AsyncTask.Status.RUNNING){ 
     Log.i("LogsX", "AsyncTask is currently doing work."); 
    } 
    if(taskUpdateText.getStatus() == AsyncTask.Status.FINISHED){ 
     Log.i("LogsX", "AsyncTask is done"); 
    } 
    . . . . 

} 

我也重寫完成方法:

public void finish() { 
    super.finish(); 
    if(!taskUpdateText.isCancelled()) { 
     Log.i("LogX", "End AsyncTask!"); 
     taskUpdateText.cancel(true); 
    } 
    else Log.i("LogX", "Asynctask is already cancell!"); 
    overridePendingTransition(R.anim.left_in, R.anim.right_out); 
} 

,因爲當活動是我必須關閉這個的AsyncTask完。

的AsyncTask是這樣的:

class UpdateTextView extends AsyncTask<Void, Void, Void> 
{ 
    int lengthTextBuffor = 0; 

    ConnectionDataPack compDataPack; 
    TextView textUpdate; 
    ScrollView scrool; 

    public UpdateTextView(ConnectionDataPack compDataPack, TextView textUpdate, ScrollView scrool) 
    { 
     this.compDataPack = compDataPack; 
     this.textUpdate = textUpdate; 
     this.scrool = scrool; 
    } 

    protected void onProgressUpdate(Void... params) { 
      textUpdate.setText(compDataPack.dataBackupCharacter.toString()); 
      scrool.fullScroll(View.FOCUS_DOWN); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     Log.i("LogX", "doInBackground Starts!"); 
     boolean startLoop = true; 
     while(true) 
     { 
       synchronized (compDataPack.lock){ 
       if(!startLoop && lengthTextBuffor == compDataPack.dataBackupCharacter.length()) { 
        try { 
         compDataPack.lock.wait(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
       startLoop = false; 
       publishProgress(); 
      } 
     } 
    } 
} 
+0

感謝您的所有答案,但看來我在這裏找到了解決辦法:[link](http://stackoverflow.com/a/16851515/7392319)。它現在工作。 – sQanns

回答

0

問題與這些可能是你不asyntask被垃圾回收或它仍然在後臺運行。

嘗試使用下面的代碼

@override 
public void onBackPressed() { 
if(taskUpdateText!=null) { 
      taskUpdateText.cancel(true); 
      taskUpdateText = null; 
     } 

} 
0

試試下面代碼..

if (taskUpdateText != null && taskUpdateText.getStatus() == AsyncTask.Status.PENDING) { 
      taskUpdateText.execute(); 
     } else if (taskUpdateText == null || taskUpdateText.getStatus() == AsyncTask.Status.FINISHED) { 
      taskUpdateText = new UpdateTextView(); 
      taskUpdateText.execute(); 
     } 

和的onDestroy()把下面的代碼

@Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     if (taskUpdateText != null && taskUpdateText.getStatus() == AsyncTask.Status.RUNNING) { 
      taskUpdateText.cancel(true); 
     } 
    }