3 回答
TA贡献1796条经验 获得超4个赞
尝试使用此代码。它适用于所有版本。
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(ctx.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"));
this.ctx.sendBroadcast(poke);
}
}
// automatic turn off the gps
public void turnGPSOff()
{
String provider = Settings.Secure.getString(ctx.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"));
this.ctx.sendBroadcast(poke);
}
}
TA贡献1830条经验 获得超3个赞
尝试使用以下代码关闭gps Intent intent = new Intent(“ android.location.GPS_ENABLED_CHANGE”); intent.putExtra(“ enabled”,false); sendBroadcast(intent);
TA贡献1843条经验 获得超7个赞
我收到“ java.lang.SecurityException:权限被拒绝:不允许发送广播android.location.GPS_ENABLED_CHANGE”。应用程序崩溃了
- 3 回答
- 0 关注
- 484 浏览
添加回答
举报