Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GPS on or off state

Is there any way to check if the Android GPS is on or off without any permission, just the state I don't need to toggle it.

Thanks

like image 263
zsniperx Avatar asked Nov 29 '10 03:11

zsniperx


2 Answers

If you want to check the condition of GPS that whether it is ON or OFF, then this solution will help you

final LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
  Toast.makeText(context, "GPS is disable!", Toast.LENGTH_LONG).show(); 
else
 Toast.makeText(context, "GPS is Enable!", Toast.LENGTH_LONG).show();
like image 170
Pir Fahim Shah Avatar answered Oct 17 '22 20:10

Pir Fahim Shah


steps -

  1. Create services running in backgroud
  2. You require following permission in Manifest file too -

    android.permission.ACCESS_FINE_LOCATION
    
  3. write code

     final LocationManager manager = (LocationManager)context.getSystemService    (Context.LOCATION_SERVICE );
    
    if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
    
    //what you want to do
     else
    
       //what you want to do
    
  4. Or simply you can check

    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
    boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
  5. run your services continuous to monitor connection

  6. call your services from Activity
like image 42
Rakesh Avatar answered Oct 17 '22 21:10

Rakesh