2

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.

Community
  • 1
  • 1
shankey
  • 333
  • 2
  • 7
  • 18

5 Answers5

1

The problem is that you are trying Updating the UI from a non-UI thread, you should update the UI from the UI thread only else you have to use a Handler or runOnUiThread().

Try this,

killContext.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                alert.show();
            }
        });
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

AlertDialog can't be shown in any context, except current activity. So you have to be sure that killContext extends Activity class and it is on screen write now.

Jin35
  • 8,602
  • 3
  • 32
  • 52
0

I have also observed that if a process is put on non-ui thread and it raises alert without runOnUiThread, it causes some kind of hang and android ui thread goes in ANR... Anybody has this same observation ?

0
  • best way to do it is AsyncTask .

  • short way is call RunOnUiThread( new runnable) and write alert dialog related thing inside runnable .

  • alternative way Handler class implementation

Shailendra Singh Rajawat
  • 8,172
  • 3
  • 35
  • 40
  • I would like to go with short way but where should I write RunOnUiThread( new runnable) exactly ? I am extremely new to this so please explain it in detail. – shankey Dec 16 '11 at 13:21
  • to being new is not unfair, but ask for help even if you can do it by refer docs and study actually is . – Shailendra Singh Rajawat Dec 16 '11 at 13:28
0

You should store an Activity instead of a Context in the killContext field, then you can call runOnUiThread:

killActivity.runOnUiThread(new Runnable(){public void run(){alert.show();}})
cheng81
  • 2,434
  • 2
  • 21
  • 18