Android GPS on or off state

梦想的初衷 提交于 2019-12-21 04:37:19

问题


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


回答1:


No. Checking GPS Status on Android requires

android.permission.ACCESS_FINE_LOCATION

The code to check the status of GPS


 LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
 boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);




回答2:


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



回答3:


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();


来源:https://stackoverflow.com/questions/4300572/android-gps-on-or-off-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!