打开和关闭GPS编程方式在安卓4.0及以上?
问题描述:
您好,我建立一个防盗应用程序,并通过短信查找我的手机,它完美的作品,直到2.3。 但在4.0我不能打开或关闭GPS编程有通过code对GPS切换其他任何可能的方式。
Hi there i am creating an anti-theft application and, locating my phone through sms and it works perfectly until 2.3. But in 4.0 i cant turn on or off gps programmatically is there any other possible way to switch on gps through code.
答
尝试使用这种code。它为我工作的所有版本。
Try using this code. It worked for me on all the versions.
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);
}
}