Android GPS开启或关闭状态

时间:2010-11-29 03:14:01

标签: android gps toggle state android-manifest

有没有办法在没有任何许可的情况下检查Android GPS是打开还是关闭,只是我不需要切换它的状态。

由于

3 个答案:

答案 0 :(得分:30)

没有。在Android上检查GPS状态需要

android.permission.ACCESS_FINE_LOCATION

检查GPS状态的代码


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

答案 1 :(得分:5)

步骤 -

  1. 创建在backgroud中运行的服务
  2. 您在Manifest文件中也需要以下权限 -

    android.permission.ACCESS_FINE_LOCATION
    
  3. 编写代码

     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. 或者只是你可以检查

    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
    boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
  5. 连续运行您的服务以监控连接

  6. 从活动中调用您的服务

答案 2 :(得分:2)

如果您想检查GPS的状态是开启还是关闭,那么此解决方案将帮助您

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