- 浏览: 150096 次
- 性别:
- 来自: 五指山
文章分类
最新评论
-
dengdonglin888:
qq_30221445 写道你好 请问这种数据能解吗 < ...
Simple XML -
qq_30221445:
你好 请问这种数据能解吗 <request> ...
Simple XML -
画个逗号给明天qu:
画个逗号给明天qu 写道
Android上传文件到服务器 -
画个逗号给明天qu:
...
Android上传文件到服务器 -
alranger:
我在jsp页面加上这一段代码后,问题还是存在。
解决Ext在ie9报错:不支持extjs对象的“createContextualFragment属性或方法”
手机应用中最酷的可能就是位置服务相关的了,如何读取GPS信息,在官方文档上有相当详细的说明,后面如果有机会,我也会专门写例子来介绍(教程已完成,请参见:教程:实现Android的不同精度的定位(基于网络和GPS))。但今天,我们先来看下如何以编程的方式来开启或关闭GPS。
官方的API中,android.provider.Settings.Secure类有2个静态方法:
public static final void setLocationProviderEnabled (ContentResolver cr, String provider, boolean enabled)
和
public static final boolean isLocationProviderEnabled (ContentResolver cr, String provider)
不过遗憾的是,这2个方法都注明了从API Level 8(即Android 2.2)才开始提供,那么在2.2之前又该如何编程实现GPS的开关呢?
山重水复疑无路
首先,我们要知道,Android系统的设置画面中就可以进行GPS的开关,那么它是如何实现的呢?
由于我的机器上的android source是2.3版本的,所以直接启动了一个2.1的模拟器,用adb pull将Settings.apk抓下来,反编译之后,在SecuritySettings类中找到如下代码:
package,com.android.settings.SecuritySettings.java
1
2
3
4
5
6
7
8
CheckBoxPreference localCheckBoxPreference3 = this.mGps;
if (paramPreference == localCheckBoxPreference3)
{
ContentResolver localContentResolver3 = getContentResolver();
boolean bool6 = this.mGps.isChecked();
Settings.Secure.setLocationProviderEnabled(localContentResolver3, "gps", bool6);
continue;
}
可以看到2.1系统中已经存在有Settings.Secure.setLocationProviderEnabled方法了,只是该方法没有开放而已,事实上读过Android源码的人都对/*hide*/很反感吧,看得到,摸不到!
既然Setting画面中的用法,我们不能使用,那么再换1种方法,我们去看一下Settings.Secure.setLocationProviderEnabled的写法,然后直接套用。
这次,我们直接去看Android 2.3的源码,找到Setting.java之后,找到相关的方法,代码如下:
core, android.provider.Setting.java
1
2
3
4
5
6
7
8
9
10
11
12
public static final void setLocationProviderEnabled(ContentResolver cr,
String provider, boolean enabled) {
// to ensure thread safety, we write the provider name with a '+' or '-'
// and let the SettingsProvider handle it rather than reading and modifying
// the list of enabled providers.
if (enabled) {
provider = "+" + provider;
} else {
provider = "-" + provider;
}
putString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, provider);
}
原来这个方法只是1个包装,事实上调用的还是Settings.Secure中的putString方法,我们直接借用过来:
在自己的onClick事件中写上
Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, “network,gps”);
然后执行,WOW,发生了什么,需要android.permission.WRITE_SETTINGS权限?在Manifest文件中加上,再运行,还是出错,不过这次需要的是android.permission.WRITE_SECURE_SETTINGS,再次加上。
满怀希望的再次运行,结果还是一样的问题:
java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS
看来,Google封死了直接调用Settings的路了,事实上我又试着使用反射来直接调用setLocationProviderEnabled方法,结果也是一样的告诉我需要权限。
柳暗花明又一村
难道没有别的办法了吗?那些2.1中可以运行的App Widget是如何做到的呢?
再次检视2.1的Settings.apk中的代码,发现有1个widget包,里面有1个类叫SettingsAppWidgetProvider,这就是用来提供App Widget的类,继续研究这里的代码,发现构造RemoteView的代码中用到如下方法:
1
2
3
4
5
6
7
8
9
private static PendingIntent getLaunchPendingIntent(Context paramContext, int paramInt1, int paramInt2)
{
Intent localIntent1 = new Intent();
Intent localIntent2 = localIntent1.setClass(paramContext, SettingsAppWidgetProvider.class);
Intent localIntent3 = localIntent1.addCategory("android.intent.category.ALTERNATIVE");
Uri localUri = Uri.parse("custom:" + paramInt2);
Intent localIntent4 = localIntent1.setData(localUri);
return PendingIntent.getBroadcast(paramContext, 0, localIntent1, 0);
}
由于这是反编译的结果,略微有点混乱,但还是可以看出思路,目的是通过PendingIntent来扔出1个Intent,接受者是SettingsAppWidgetProvider.class,接受的参数有2个,1个是Category:SettingsAppWidgetProvider.class(正是这个类自身),另1个是Data:Uri.parse(“custom:” + paramInt2),这个paramInt2是Widget中的图标按钮的序号。所有的序号在类的首部都有定义:
1
2
3
4
5
private static final int BUTTON_BLUETOOTH = 4;
private static final int BUTTON_BRIGHTNESS = 1;
private static final int BUTTON_GPS = 3;
private static final int BUTTON_SYNC = 2;
private static final int BUTTON_WIFI = 0;
那么我们只要送出custom:3的Uri给SettingsAppWidgetProvider.class,就应该可以由SettingsAppWidgetProvider类来帮我们调用Settings.Secure.setLocationProviderEnabled方法了。
说到做到,在Activity中添加如下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
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();
}
}
这个方法是1个纯开关,如果当前是开启的,那么就会关闭它,反之亦然。
检查GPS开关状态
那么,如何查看当前的GPS开关状态呢?可以用上面提到的反射方式调用isLocationProviderEnabled,代码片断如下:
1
2
3
4
5
secureClass = cl.loadClass("android.provider.Settings$Secure");
isMethod = secureClass.getMethod("isLocationProviderEnabled",
ContentResolver.class, String.class);
Boolean ret = (Boolean) isMethod.invoke(secureClass, this
.getContentResolver(), "gps");
也可以直接用下面的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}
}
这2种方法的原理都是一样的,方法2其实也就是isLocationProviderEnabled实际调用的代码,只是Google未对读取操作进行权限限制。
总结
如果目标手机是运行Android 2.2的话,那么最好还是使用2.2开放的Settings.Secure类中的2个方法来操作。但如果目标手机运行的版本是2.1或以下的话,那么就只能使用变通的方法来实现了。这1方法在Android官方的Wiki上已经有人提出了,详情请见:Issue 7890。但可能是2.1版本已经古旧不再维护的原因,官方并未进行任何的Fix。
转载自:网络
你多试试几次,我的ok的!
官方的API中,android.provider.Settings.Secure类有2个静态方法:
public static final void setLocationProviderEnabled (ContentResolver cr, String provider, boolean enabled)
和
public static final boolean isLocationProviderEnabled (ContentResolver cr, String provider)
不过遗憾的是,这2个方法都注明了从API Level 8(即Android 2.2)才开始提供,那么在2.2之前又该如何编程实现GPS的开关呢?
山重水复疑无路
首先,我们要知道,Android系统的设置画面中就可以进行GPS的开关,那么它是如何实现的呢?
由于我的机器上的android source是2.3版本的,所以直接启动了一个2.1的模拟器,用adb pull将Settings.apk抓下来,反编译之后,在SecuritySettings类中找到如下代码:
package,com.android.settings.SecuritySettings.java
1
2
3
4
5
6
7
8
CheckBoxPreference localCheckBoxPreference3 = this.mGps;
if (paramPreference == localCheckBoxPreference3)
{
ContentResolver localContentResolver3 = getContentResolver();
boolean bool6 = this.mGps.isChecked();
Settings.Secure.setLocationProviderEnabled(localContentResolver3, "gps", bool6);
continue;
}
可以看到2.1系统中已经存在有Settings.Secure.setLocationProviderEnabled方法了,只是该方法没有开放而已,事实上读过Android源码的人都对/*hide*/很反感吧,看得到,摸不到!
既然Setting画面中的用法,我们不能使用,那么再换1种方法,我们去看一下Settings.Secure.setLocationProviderEnabled的写法,然后直接套用。
这次,我们直接去看Android 2.3的源码,找到Setting.java之后,找到相关的方法,代码如下:
core, android.provider.Setting.java
1
2
3
4
5
6
7
8
9
10
11
12
public static final void setLocationProviderEnabled(ContentResolver cr,
String provider, boolean enabled) {
// to ensure thread safety, we write the provider name with a '+' or '-'
// and let the SettingsProvider handle it rather than reading and modifying
// the list of enabled providers.
if (enabled) {
provider = "+" + provider;
} else {
provider = "-" + provider;
}
putString(cr, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, provider);
}
原来这个方法只是1个包装,事实上调用的还是Settings.Secure中的putString方法,我们直接借用过来:
在自己的onClick事件中写上
Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, “network,gps”);
然后执行,WOW,发生了什么,需要android.permission.WRITE_SETTINGS权限?在Manifest文件中加上,再运行,还是出错,不过这次需要的是android.permission.WRITE_SECURE_SETTINGS,再次加上。
满怀希望的再次运行,结果还是一样的问题:
java.lang.SecurityException: Permission denial: writing to secure settings requires android.permission.WRITE_SECURE_SETTINGS
看来,Google封死了直接调用Settings的路了,事实上我又试着使用反射来直接调用setLocationProviderEnabled方法,结果也是一样的告诉我需要权限。
柳暗花明又一村
难道没有别的办法了吗?那些2.1中可以运行的App Widget是如何做到的呢?
再次检视2.1的Settings.apk中的代码,发现有1个widget包,里面有1个类叫SettingsAppWidgetProvider,这就是用来提供App Widget的类,继续研究这里的代码,发现构造RemoteView的代码中用到如下方法:
1
2
3
4
5
6
7
8
9
private static PendingIntent getLaunchPendingIntent(Context paramContext, int paramInt1, int paramInt2)
{
Intent localIntent1 = new Intent();
Intent localIntent2 = localIntent1.setClass(paramContext, SettingsAppWidgetProvider.class);
Intent localIntent3 = localIntent1.addCategory("android.intent.category.ALTERNATIVE");
Uri localUri = Uri.parse("custom:" + paramInt2);
Intent localIntent4 = localIntent1.setData(localUri);
return PendingIntent.getBroadcast(paramContext, 0, localIntent1, 0);
}
由于这是反编译的结果,略微有点混乱,但还是可以看出思路,目的是通过PendingIntent来扔出1个Intent,接受者是SettingsAppWidgetProvider.class,接受的参数有2个,1个是Category:SettingsAppWidgetProvider.class(正是这个类自身),另1个是Data:Uri.parse(“custom:” + paramInt2),这个paramInt2是Widget中的图标按钮的序号。所有的序号在类的首部都有定义:
1
2
3
4
5
private static final int BUTTON_BLUETOOTH = 4;
private static final int BUTTON_BRIGHTNESS = 1;
private static final int BUTTON_GPS = 3;
private static final int BUTTON_SYNC = 2;
private static final int BUTTON_WIFI = 0;
那么我们只要送出custom:3的Uri给SettingsAppWidgetProvider.class,就应该可以由SettingsAppWidgetProvider类来帮我们调用Settings.Secure.setLocationProviderEnabled方法了。
说到做到,在Activity中添加如下方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
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();
}
}
这个方法是1个纯开关,如果当前是开启的,那么就会关闭它,反之亦然。
检查GPS开关状态
那么,如何查看当前的GPS开关状态呢?可以用上面提到的反射方式调用isLocationProviderEnabled,代码片断如下:
1
2
3
4
5
secureClass = cl.loadClass("android.provider.Settings$Secure");
isMethod = secureClass.getMethod("isLocationProviderEnabled",
ContentResolver.class, String.class);
Boolean ret = (Boolean) isMethod.invoke(secureClass, this
.getContentResolver(), "gps");
也可以直接用下面的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}
}
这2种方法的原理都是一样的,方法2其实也就是isLocationProviderEnabled实际调用的代码,只是Google未对读取操作进行权限限制。
总结
如果目标手机是运行Android 2.2的话,那么最好还是使用2.2开放的Settings.Secure类中的2个方法来操作。但如果目标手机运行的版本是2.1或以下的话,那么就只能使用变通的方法来实现了。这1方法在Android官方的Wiki上已经有人提出了,详情请见:Issue 7890。但可能是2.1版本已经古旧不再维护的原因,官方并未进行任何的Fix。
转载自:网络
评论
2 楼
wtqq520
2012-05-16
qiuyouzhi 写道
验证了,我验证了好像不行的
你多试试几次,我的ok的!
1 楼
qiuyouzhi
2012-05-10
验证了,我验证了好像不行的
发表评论
-
xUtils简介
2014-11-25 10:04 845xUtils 包含了很多实用的android工具。 xU ... -
直接拿来用!最火的Android开源项目
2014-07-25 11:01 701转 http://www.admin10000.com/d ... -
Android APK反编译详解(附图)
2014-03-28 10:56 820http://blog.csdn.net/ithomer/ar ... -
小米人
2014-02-17 17:23 686http://www.xiaomiren.net/ -
android开发之gallery 兑现滚动一张且短距离滑动实现滚动
2013-07-02 15:28 663http://www.myexception.cn/andro ... -
TextView显示插入的图片
2013-07-01 11:29 700http://orgcent.com/android-text ... -
TextView使用SpannableString设置复合文本
2013-07-01 11:29 648http://orgcent.com/android-text ... -
转:::Android TextView文字横向自动滚动(跑马灯)
2013-06-17 11:45 1513TextView实现文字滚动需要以下几个要点: 1.文字长度长 ... -
相片滤镜开源
2013-04-27 15:01 736https://github.com/daizhenjun/I ... -
android图片特效处理之模糊效果
2013-04-27 14:57 829http://blog.csdn.net/sjf0115/ar ... -
android图片处理方法(不断收集中)
2013-04-27 14:57 559http://gundumw100.iteye.com/blo ... -
Android, WindowsPhone7, IOS ,vc2010平台40多套图片滤镜开源
2013-04-27 14:56 665http://www.cnblogs.com/daizhj/a ... -
移动云存储平台
2013-04-25 16:13 906http://bmob.cn 关于Bmob 对于很多 ... -
android ExpandableListView简单应用及listview模拟ExpandableListView
2013-02-28 11:45 693http://blog.csdn.net/jj120522/a ... -
android_App集成支付宝
2013-02-28 11:43 790http://www.cnblogs.com/qianxude ... -
Android Pull Refresh View 插件
2012-12-01 12:43 847Android Pull Refresh View htt ... -
Android-TelephoneManager(转载)
2012-10-09 22:08 1347好文章齐分享。原文地址:http://blog.si ... -
android 开源 listview separato
2012-08-27 22:51 665http://code.google.com/p/androi ... -
fragment开源项目 学习
2012-08-13 12:02 929https://github.com/tisa007/Andr ... -
Fragment学习
2012-08-13 11:53 670http://www.eoeandroid.com/threa ...
相关推荐
【Android】代码开启/关闭GPSDemo 相关文章:... 为了方便看效果,您可以在在onCreate中添加finish();方法,然后打开系统GPS设置页面再运行代码,这样就能看到GPS开关的变化了。
一个简单的android GPS DEMO代码
支持2.1以上的android版本,可以把里面的APK下载到真机上测试,可以做到和系统的GPS开关同步控制,源码是在LINUX下编译的,因为android2.1把修改GPS开关的代码HIDE了,所以在2.1window平台下是编译不了。但是APK直接...
Android GPS定位源代码,带教程视频。
android GPS地图源代码,可以获取GPS位置,可以运行
获得Android手机当前的GPS定位数据,保存到SQLite本地数据库中,个人的简单作品,不足之处请多执教
描述了android的gps结构,主要是hardware 层的
安卓手机开发,GPS获取经纬度的源代码,欢迎下载啊!!
android GPS架构之GPS的开启与关闭
二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** ...
毕业设计的东西 gps 是一个很好的例子
调用底层 不经过用户界面 直接开启GPS模块 测试机型不全 如有问题 欢迎交流
android平台上完成GPS定位功能,完整代码实例,多次测试。
Android的GPS定位源码,可以检测GPS是否打开,定位速度快,可以显示经纬度。 已经打包了可以安装的APK文件
Android GPS示例代码,可用来参考,也可作为最基本的测试用例
android gps开发源码,android gps示例
android GPS地图源代码,可以获取GPS位置,可以运行.zip
android开发的一个GPS获取办法,有用的可以参照
Android gps,gnss2.0补丁。适用于rk等平台。