/**
* 打开GPS
*@date 2014-2-18 上午10:01:21
*@author luman
*/
public void turnGPSOn(Context ctx){
Log.e(TAG," turnGPSOn ");
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
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"));
ctx.sendBroadcast(poke);
}
}
/**
* 关闭GPS
*@date 2014-2-18 上午10:01:21
*@author luman
*/
public void turnGPSOff(Context ctx){
Log.e(TAG," turnGPSOff ");
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
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("1"));
ctx.sendBroadcast(poke);
}
}
* 打开GPS
*@date 2014-2-18 上午10:01:21
*@author luman
*/
public void turnGPSOn(Context ctx){
Log.e(TAG," turnGPSOn ");
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
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"));
ctx.sendBroadcast(poke);
}
}
/**
* 关闭GPS
*@date 2014-2-18 上午10:01:21
*@author luman
*/
public void turnGPSOff(Context ctx){
Log.e(TAG," turnGPSOff ");
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
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("1"));
ctx.sendBroadcast(poke);
}
}
Android GPS 控制
本文介绍了一种在Android设备上通过发送特定Intent来控制GPS开关的方法。该方法利用了广播接收者和系统的设置权限,能够有效地开启或关闭GPS定位功能。
904

被折叠的 条评论
为什么被折叠?



