您的位置:首页 > 移动开发 > Android开发

android中gps的打开关闭

2014-12-10 10:05 295 查看
亲测一下方法有用,4.2以后都可以用

private void toggleGPS() {
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings",
"com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
}
catch (CanceledException e) {
e.printStackTrace();
}
}判断是否打开
private void isGPSEnable() {
/* 用Setting.System来读取也可以,只是这是更旧的用法
String str = Settings.System.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
*/
String str = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.v("GPS", str);
if (str != null) {
return str.contains("gps");
}
else{
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: