4

I want to show AlertDialog inside Timer in Service class and i am using following code :

timer.scheduleAtFixedRate( new TimerTask() 
{
    private Handler updateUI = new Handler()
    {
        public void dispatchMessage(android.os.Message msg)
        {
            super.dispatchMessage(msg);
            try {                       
                fun();
            } catch (Exception e) {e.printStackTrace(); }    
        }
    };
    public void run() 
    { 
        try {
            updateUI.sendEmptyMessage(0);                   
        }catch (Exception e) {e.printStackTrace(); }
    }
}, 0,60000);

public void fun()
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);                      
    dlgAlert.setMessage("");
    dlgAlert.setTitle("");              
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create();
    dlgAlert.show();
}

and I am getting following error:

03-14 13:14:36.879: WARN/WindowManager(60): Attempted to add window with non-application token WindowToken{43f606b0 token=null}.  Aborting.
03-14 13:14:36.879: WARN/System.err(817): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
03-14 13:14:36.889: WARN/System.err(817):     at android.view.ViewRoot.setView(ViewRoot.java:509)
03-14 13:14:36.889: WARN/System.err(817):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
03-14 13:14:36.889: WARN/System.err(817):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-14 13:14:36.899: WARN/System.err(817):     at android.app.Dialog.show(Dialog.java:241)
03-14 13:14:36.899: WARN/System.err(817):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
03-14 13:14:36.899: WARN/System.err(817):     at com.mobilelocalite.pkg.GPSServiceCellId.comparefromDb(GPSServiceCellId.java:373)
03-14 13:14:36.909: WARN/System.err(817):     at com.mobilelocalite.pkg.GPSServiceCellId$1$1.dispatchMessage(GPSServiceCellId.java:133)
03-14 13:14:36.909: WARN/System.err(817):     at android.os.Looper.loop(Looper.java:123)
03-14 13:14:36.909: WARN/System.err(817):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-14 13:14:36.909: WARN/System.err(817):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 13:14:36.909: WARN/System.err(817):     at java.lang.reflect.Method.invoke(Method.java:521)
03-14 13:14:36.909: WARN/System.err(817):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-14 13:14:36.920: WARN/System.err(817):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-14 13:14:36.920: WARN/System.err(817):     at dalvik.system.NativeStart.main(Native Method)

Thanks in Advance.

Jon Bright
  • 13,388
  • 3
  • 31
  • 46
Monali
  • 1,966
  • 12
  • 39
  • 59

3 Answers3

1

Take a look at this question: Alert dialog from Android service

EDIT:

The problem is that you are trying to pop up a dialog without having any reference to a window. You need to send the message to an Activity and let the Activity handle the dialog pop up.

Using

new AlertDialog.Builder(this);

inside a Service will give you an error similar to the one you got. Using the same code inside an Activity will not.

So one alternative is to send a broadcast to the Activity you are contacting, letting the Activity listen for this broadcast. And when the broadcast is received by the Activity, a Dialog is shown.

Community
  • 1
  • 1
Eric Nordvik
  • 14,656
  • 8
  • 42
  • 50
0
 Attempted to add window with non-application token WindowToken{43f606b0 token=null}.  Aborting.

you are getting this because you are trying to add a window in service which doesnt have a render thread to draw anything.Whenever you are trying to add a dialog it first fetches a token of the parent window form the system which is the the "context" of the dialog.You are passing "this" in the context,which in your case is the service.Which is not proper.

You should try with ApplicationContext or with context of some Activity.

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47
0

Best way will be create a class which would inherit from DialogFragment (android.support.v4.app.DialogFragment). See android Doc

You can call the DialogFragment in this way :

 DialogFragment fragment;
 FragmentManager fm = activity.getSupportFragmentManager();
            final FragmentTransaction transaction = fm.beginTransaction();

            fragment.show(transaction, "dialog");
            if (addToBackStack) {
                transaction.addToBackStack(null);
            } else {
                clearBackStack(activity);

Hope it helps

Fanny BATCHO
  • 113
  • 9