getting error like -java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
I am cerating application which has many activities . If user doesn't respond for a specific time (15 mins) then after 15 mins I wnat to show a dialog box which ask user "Are you still there ?" and if user press "NO" application should close. I tried to do this with the help of Application idle time
but I didn't created ControlApplication.java
I directly called touch() from my each activity.
and called Waiter class constructor from another java file which is global for my application.
here is my code
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;
}
}
but at alert.show(); I am getting above error. please help.