• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need help with AlertDialog. .show() doesn't show it!

SOLVED BELOW

Below is the snippet of code that isn't working how I think it should. What I want it to do is detect if GPS is currently on. If not, then throw up an AlertDialog letting the user know I want it enabled, and then open the screen for them so they can turn it on. It's detecting the GPS status correctly, but the dialog box never appears causing an infinite loop. What else do I need to do aside from show()ing the AlertDialong?

PHP:
public void toggleGPS()
    {
    	LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    	
		while(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER )) { 
	    	AlertDialog.Builder alt_bld = new AlertDialog.Builder(AssetTracking.this);
	    	alt_bld.setMessage("Please enable GPS.")
	    	.setCancelable(false)
	    	.setPositiveButton("OK", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int id) {
		    		Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS ); 
					startActivity(myIntent);
		    	}
	    	});
	    	AlertDialog alert = alt_bld.create();

	    	alert.show();
		}
		
    	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, (LocationListener) this);
    }
 
try using this way instead
Code:
AlertDialog.Builder(AssetTracking.this[FONT=monospace][/FONT][COLOR=#000000][COLOR=#0000BB][/COLOR][/COLOR]).setMessage("Please enable GPS.").setCancelable(false) 
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                            startActivity(myIntent); 

                            }
                        }).create().show();

I didn't try compiling it , but I think it should work
 
Eclipse had a fit about the syntax so I changed it to the below, but still no dialog box:

PHP:
while(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER )) { 
			
			AlertDialog.Builder adb = new AlertDialog.Builder(AssetTracking.this);
			adb
			.setMessage("Please enable GPS.").setCancelable(false) 
            .setPositiveButton("OK",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                        startActivity(myIntent); 

                        }
                    }).create().show();
		}
 
SOLUTION:

As it turns out the best way to do what I wanted was with the use of onStart():

PHP:
@Override
    public void onStart(){
    	super.onStart();
    	toggleGPS();
    }

protected Dialog onCreateDialog(int n){
    	Dialog d = null;
    	switch(n){
    		case ENABLE_GPS:
    			d= new AlertDialog.Builder(AssetTracking.this)
    				.setMessage("Please enable GPS.")
    				.setCancelable(false)
    				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    		           public void onClick(DialogInterface dialog, int id) {
    		        	   Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS );
                           startActivity(myIntent); 
    		           }
    				})
    				.create();
    			break;
    		default:
    			d= null;
    			break;
    	}
    	return d;
    }
    
    public void toggleGPS(){
    	LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    	
		if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER )) { 
			showDialog(ENABLE_GPS);
		}
    }
 
Back
Top Bottom