3 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試使用此代碼。它適用于所有版本。
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貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
嘗試使用以下代碼關(guān)閉gps Intent intent = new Intent(“ android.location.GPS_ENABLED_CHANGE”); intent.putExtra(“ enabled”,false); sendBroadcast(intent);

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊
我收到“ java.lang.SecurityException:權(quán)限被拒絕:不允許發(fā)送廣播android.location.GPS_ENABLED_CHANGE”。應(yīng)用程序崩潰了
- 3 回答
- 0 關(guān)注
- 511 瀏覽
添加回答
舉報(bào)