AsyncTaskでトーストを高めるためにどのように、私が使用ルーパーにプロンプ​​トが表示されています

StackOverflow https://stackoverflow.com/questions/2837676

質問

私は、バックグラウンドでAsyncTaskで完了したタスクを持っています。いくつかの時点で私は何かが完了したことをトーストを発行する必要があります。

私が試したと私は失敗したので、 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

私はそれを行うことができますどのように?

役に立ちましたか?

解決

onPostExecute - UIスレッド上で実行 若しくは  publishProgress();あなたのdoinbackground内と

protected void onProgressUpdate(Integer... progress) {
}

http://developer.android.com/reference/android/os/ AsyncTask.htmlする

他のヒント

することができますトーストdoInBackground内部

表示されますが、トーストにしたい、このコードを追加します。

runOnUiThread(new Runnable() {
public void run() {

    Toast.makeText(<your class name>.this, "Cool Ha?", Toast.LENGTH_SHORT).show();
    }
});
あなたはトーストを使用したい場合は、

onProgressUpdate()

:あなたは、このメソッドを使用する必要があります
protected Integer doInBackground(Void...Params) {
   int check_point = 1;
   publishProgress(check_point);
   return check_point;
}

protected void onProgressUpdate(Integer integers) {
  if(integers == 1) {
    Toast.makeText(classname.this, "Text", 0).show(); 
}
あなたがバックグラウンドスレッドからトーストを表示したい場合は、

あなたはdoInBackgroundからrunOnUiThreadを呼び出す必要があります。私は別の方法があります信じていません。

編集:私はそのバックを取ります。私はあなたがトーストを示し、doInBackgroundからpublishProgressに呼び出しを行うために、UIスレッド上で動作するonProgressUpdateを、実装することができると思います。

あなたはdoInBackgroundでトーストを表示したい場合は、

、あなたはAsyncTaskのOnPostExecute方法でそれを使用することができます。

protected void onPostExecute(String file_url) {    
   Toast.makeText(getApplicationContext(),"Your Message", Toast.LENGTH_LONG).show();

   pDialog.dismiss();//dismiss the progress dialouge
}
scroll top