android以编程方式在android 4.0及更高版本中打开和关闭GPS



我想在一个活动中打开和关闭gps定位,尝试其他所有主题,但这不起作用这不是重复的主题,我看其他的

示例:

清单:

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.Manifest.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.Manifest.permission.ACCESS_COARSE_LOCATION" />

活动:

public void turnGPSOn()
 {
 Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
 intent.putExtra("enabled", true);
 this.ctx.sendBroadcast(intent);
String provider = Settings.Secure.getString(activityname.this.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")); 
    activityname.this.sendBroadcast(poke);

}
 }
 // automatic turn off the gps
  public void turnGPSOff()
  {
String provider = Settings.Secure.getString(activityname.this.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")); 
    activityname.this.sendBroadcast(poke);
  }
 }

当我尝试我的应用程序停止(崩溃)时

来源:在android 4.0及以上版本中以编程方式打开和关闭GPS?

我不想显示对话框(选择开放本地化)我需要以编程方式(隐藏)

请帮帮我。谢谢。

由于明显的隐私原因,这是不可能的,除非通过root。你试图利用的安全漏洞已经关闭了很长一段时间。

最新更新