android设置gps开启
2013-06-13 15:06
330 查看
http://blog.csdn.net/yuanbohx/article/details/8003331
设置部分:
[java] view plaincopy
/**
* 设置GPS开启或关闭,入口参数为true时开启GPS,为false时关闭GPS
* */
public void setGPS(boolean on_off) {
boolean gpsEnabled = android.provider.Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER);
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
if(on_off == true)
{
if(!gpsEnabled) {
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
else {
if(gpsEnabled) {
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
}
以上代码需要添加如下权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
设置部分:
[java] view plaincopy
/**
* 设置GPS开启或关闭,入口参数为true时开启GPS,为false时关闭GPS
* */
public void setGPS(boolean on_off) {
boolean gpsEnabled = android.provider.Settings.Secure.isLocationProviderEnabled( getContentResolver(), LocationManager.GPS_PROVIDER);
Intent gpsIntent = new Intent();
gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
if(on_off == true)
{
if(!gpsEnabled) {
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
else {
if(gpsEnabled) {
gpsIntent.setData(Uri.parse("custom:3"));
try {
PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
} catch (CanceledException e) {
e.printStackTrace();
}
}
}
}
以上代码需要添加如下权限:
<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
相关文章推荐
- android 检查GPS是否开启,GPS设置界面
- android设置gps自动开启
- android中判断gps是否开启,通过dialog跳转至设置
- android 调用设置页面开启gps
- 判断Android的WIFI与GPS状态,并引导用户前去开启GPS与WIFI设置
- android设置gps自动开启
- android设置gps自动开启
- Android 手势密码开启状态设置
- android手动开启GPS
- 设置Android设备在睡眠期间始终保持WLAN开启的代码实现
- Android应用获取通知栏权限是否开启--以及跳转到系统设置界面--解决方案
- Android判断GPS是否开启和强制帮用户打开GPS
- android 中对 gps 默认关闭+语言默认中文的设置+时区设置+修改系统名字+修改系统默认时间+修改屏幕休眠时间
- android的GPS代码分析JNI如何HAL之间如何设置回调函数【转】
- 【Android】代码开启/关闭GPS
- 如何通过代码实现进入到手机的设置页面(比如开启gps设置的页面)
- [Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
- android EditText当设置中开启拼音检查工具报错问题
- Android判断GPS是否开启和强制帮用户打开GPS
- 【转载】如何开启Android的虚拟GPS功能