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;
}
}
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;
}
}
相关文章推荐
- android使用代码实现gps,gprs,wifi打开与关闭
- android 5.1 打开与关闭GPS
- Android中如何打开或关闭GPS
- Android 在代码中设置打开关闭GPS不必用户操作
- Android打开或者关闭GPS
- Android 利用程序实现GPS的打开或关闭
- Android中如何通过程序打开或关闭GPS
- android 打开或关闭GPS
- android 默认打开位置服务,默认关闭GPS设置
- Android中如何通过程序打开或关闭GPS
- Android实现GPS的打开与关闭
- Android 如何打开和关闭GPS设备
- Android中如何通过程序打开或关闭GPS
- android 中对 gps 默认关闭+语言默认中文的设置+时区设置+修改系统名字+修改系统默认时间
- 9.1、Android中得到新打开Activity 关闭后返回的数据
- Android GPRS的自动打开与关闭。
- android 中对 gps 默认关闭+语言默认中文的设置+时区设置+修改系统名字+修改系统默认时间+修改屏幕休眠时间
- 【Android】代码开启/关闭GPS
- android 打开/关闭 移动网络
- 【Android】代码开启/关闭GPS .