如何自动启用GPS

时间:2015-04-08 05:23:51

标签: android android-intent permissions gps location

转到设置 - >位置服务 - >检查访问我的位置,我想以编程方式执行此操作autoEnable,我尝试了下面的代码,但没有工作

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");      
intent.putExtra("enabled", true);
sendBroadcast(intent);
private void turnGPSOn(){     
 String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);      if(!provider.contains("gps")){ 
    //if gps is disabled         
    final Intent poke = new Intent();           
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");          poke.addCategory(Intent.CATEGORY_ALTERNATIVE);         poke.setData(Uri.parse("3"));          
    sendBroadcast(poke);     

          } 
}

1 个答案:

答案 0 :(得分:0)

考虑API版本 //启用


 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
sendBroadcast(intent);

//残疾人


Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);

在Android清单中设置权限


<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

More Details Please Visit