Android 如何自动启用GPS

Android 如何自动启用GPS,android,Android,我正在使用此代码将获得安全异常 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();

我正在使用此代码将获得安全异常

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

private void turnGPSOff(){
    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(provider.contains("gps")){ //if gps is enabled
    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);
}
}

如何解决自动启用GPS的问题。

为了安全起见,Google开发者阻止了这两种方法,这两种方法以前都可以正常工作。 因此,您无法通过编程启用GPS

现在没有办法/解决办法以编程方式启用Gps

如果gps未启用,您可以要求用户打开gps

if (!check_Gps(Your_Activity_Name.this)) {

   open_Gps_Activity(Your_Activity_Name.this);


 }

  public void open_Gps_Activity(Context context) {
    final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}
首先检查gps是否启用

public boolean check_Gps(Context context) {
    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    return statusOfGPS;
}
如果未启用,则打开gps活动

if (!check_Gps(Your_Activity_Name.this)) {

   open_Gps_Activity(Your_Activity_Name.this);


 }

  public void open_Gps_Activity(Context context) {
    final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

您还可以向用户敬酒,告诉用户如果不打开gps,您的应用程序将无法正常工作。

感谢您宝贵的回答。那么,还有其他方法可以打开/关闭GPS吗?第4点是您的答案:)谢谢您宝贵的回答。那么有没有其他方法可以打开/关闭GPS?对不起,伙计,没有其他方法了