-java.lang.RuntimeExceptionのようなエラーが発生しました:Looper.prepare()を呼び出していないスレッド内でハンドラを作成できません非UIクラスから警告ダイアログを表示するにはどうすればよいですか? Looper.prepare()を呼び出していないスレッド内でハンドラを作成できません
私は多くの活動をしているceratingアプリケーションです。ユーザーが特定の時間(15分)応答しなかった場合、15分後に、ユーザーに「まだそこにいますか?」という質問を表示するダイアログボックスが表示されます。ユーザーが「いいえ」を押すとアプリケーションが終了するはずです。 私は Application idle time
の助けを借りてこれを実行しようとしましたが、私は私の各アクティビティからControlApplication.javaを I直接呼び出すタッチ()は作成されませんでした。 私のアプリケーション用にグローバルな別のJavaファイルからWaiterクラスのコンストラクタを呼び出しました。 ここに私のコード
Waiter.java
public class Waiter extends Thread
{
private static final String TAG=Waiter.class.getName();
private long lastUsed;
private long period;
private boolean stop;
private Context killContext;
public Waiter(long period, Context context)
{
this.period=period;
stop=false;
killContext = context;
}
public void run()
{
long idle=0;
this.touch();
do
{
idle=System.currentTimeMillis()-lastUsed;
Log.d(TAG, "Application is idle for -------------------------------------->"+idle +" ms");
Log.d(TAG, "lastUsed is -------------------------------------->"+lastUsed +" ms");
Log.d(TAG, "currentTimeMillis is -------------------------------------->"+System.currentTimeMillis() +" ms");
try
{
Thread.sleep(5000); //check every 50 seconds
}
catch (InterruptedException e)
{
Log.d(TAG, "----------------Waiter interrupted!-----------------------------------");
}
if(idle > period)
{
idle=0;
Log.d(TAG, "----------------inside if-----------------------------------");
AlertDialog.Builder killBuilder = new AlertDialog.Builder(killContext);
killBuilder.setMessage("Are you still there ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
android.os.Process.killProcess(
android.os.Process.myPid());
}
});
Log.d(TAG, "----------------inside if killBuilder.create();;-----------------------------------");
AlertDialog alert = killBuilder.create();
Log.d(TAG, "----------------inside if alert.show();-----------------------------------");
alert.show();
}
}
while(!stop);
Log.d(TAG, "--------------------------------Finishing Waiter thread-----------------------------");
}
public synchronized void touch()
{
lastUsed=System.currentTimeMillis();
}
public synchronized void forceInterrupt()
{
this.interrupt();
}
public synchronized void setPeriod(long period)
{
this.period=period;
}
}
しかしalert.showに()です。私は上記のエラーになっています。助けてください。
どこに書きますか? – shankey
run()内でダイアログを表示する場所(UIを更新)。 –
私はしようとするが、どのように活動名を取得するか? – shankey