How to display an AlertDialog inside a timer in a service class?

I want to show AlertDialog inside a timer in the Service class, and I use the 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 get the 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 at Advance.

+3
source share
2 answers

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

EDIT:

The problem is that you are trying to open a dialog box without a link to the window. You need to send a message to "Activity" and let the action handler open a dialog box.

Using

new AlertDialog.Builder(this);

inside the service you will get an error similar to the one you received. Using the same code inside an Activity will not.

, - , , Activity . Activity, .

+1
 Attempted to add window with non-application token WindowToken{43f606b0 token=null}.  Aborting.

, , , -. , , , "" . "this" , . .

ApplicationContext - Activity.

0

All Articles