新しいActivity
を開くと、TextView
をリアルタイムで更新するために新しいAsyncTask
を実行します。 Activity
が終了の場合AsyncTask
がキャンセルされます。このActivity
を2回開いて閉じると、すべて正常に動作します。しかし、3回目とそれ以外の間に、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:アクティビティがあるとき、私はこのAsyncTaskを閉じる必要がありますので、
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);
}
:私もfinishメソッドをオーバーライドし
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");
}
. . . .
}
終わり。
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();
}
}
}
}
がすべての答えをありがとう、しかし、以下のコードを置きますそれは私がここで解決策を見つけたようです:[リンク](http://stackoverflow.com/a/16851515/7392319)。それは今のところ働きます。 – sQanns