Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:46:39+00:00 2026-05-24T09:46:39+00:00

I trying to execute the following code… but the AlertDialog is creating problems! Without

  • 0

I trying to execute the following code… but the AlertDialog is creating problems! Without that the code works fine. I want a dialogbox to pop up and ask the user to enable GPS when its turned off!

public class Testing extends Service {
    private Location loc;
    private Dialog dialog;
    String DATA;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public void onCreate()
    {       
            super.onCreate();
    }
    @Override
    public void onStart(Intent intent, int startId) 
    {
        LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        if (!LM.isProviderEnabled(LocationManager.GPS_PROVIDER)){ 
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your GPS is disabled! Would you like to enable it?")
                    .setCancelable(false).setPositiveButton("Enable GPS",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    Intent gpsOptionsIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                    startActivity(gpsOptionsIntent);
                                }
                            });
            builder.setNegativeButton("Do nothing",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
        LocationListener LL = new MyLocationListener();
        LM.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, LL);
        LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, LL);
       /*//Runnable to determine when the first GPS fix was received.
        Runnable showWaitDialog = new Runnable() {
            @Override
            public void run() {
            while (loc == null) {
            // Wait for first GPS Fix (do nothing until loc != null)
            }
            // After receiving first GPS Fix dismiss the Progress Dialog
            dialog.dismiss();
            }
            };
            // Create a Dialog to let the User know that we're waiting for a GPS Fix
            dialog = ProgressDialog.show(getApplicationContext(), "Please wait...","Retrieving GPS data ...", true);
            Thread t = new Thread(showWaitDialog);
            t.start();*/
        LM.addNmeaListener(new NmeaListener() {
        public void onNmeaReceived(long timestamp, String nmea) 
        { 
                    DATA = nmea;
        }});
    }

But I am getting this error! The ProgressDialog code within the comment lines also throws the same error!

08-04 04:36:09.664: ERROR/AndroidRuntime(6301): FATAL EXCEPTION: main
08-04 04:36:09.664: ERROR/AndroidRuntime(6301): java.lang.RuntimeException: Unable to start service Firstdroid.Gps.Testing@462a35d0 with Intent { cmp=Firstdroid.Gps/.Testing }: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.ActivityThread.access$3600(ActivityThread.java:135)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.os.Looper.loop(Looper.java:144)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.ActivityThread.main(ActivityThread.java:4937)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at java.lang.reflect.Method.invoke(Method.java:521)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at dalvik.system.NativeStart.main(Native Method)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.view.ViewRoot.setView(ViewRoot.java:513)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.Dialog.show(Dialog.java:241)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at Firstdroid.Gps.UrbanExplorer.onStart(UrbanExplorer.java:84)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.Service.onStartCommand(Service.java:420)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267)
08-04 04:36:09.664: ERROR/AndroidRuntime(6301):     ... 10 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:46:39+00:00Added an answer on May 24, 2026 at 9:46 am

    It is not possible to show a dialog from a Service (as opposed to Activity).

    Excerpt from Dialog documentation found here

    A dialog is always created and displayed as a part of an Activity. You should normally create dialogs from within your Activity‘s onCreateDialog(int) callback method.

    In fact, showing a dialog from a Service violates the Android UI guidelines. The guidelines recommend using notifications instead.

    If you really need to, you can launch an Activity that looks and behaves like a Dialog, which is described here, but still ill-advised as you may be interrupting the user while he/she is in the middle of another activity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to execute the following code from a firefox extension but it
I'm trying to execute the following PHP code: $path_hierarchy = // function that returns
I'm trying to execute the following code but I'm getting an error saying expression:string
I'm trying to execute the following code but it is not working p =
When I am trying to execute following code to email the contact form details,
I'm trying the following code to execute a search and it's not working. On
I'm trying to execute the following line: exit | sqlplus username/password@sid @test.sql Works great
Hi im trying to execute the following PHP code, however im receiving an error.
I'm trying to execute the following query but I receive a runtime error stating
I'm trying to execute a query in PostgreSQL using the following code. It's written

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.