如何在android4.0中开启GPS

问答小助手 2013-04-15 04:02:33
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/2046

问题描述:

我的应用中要开启GPS,一直到android2.3.6版本都运行正常。但是到了android4.0版本以后,就不行了。

不知道为什么?在高版本里应该怎么设置呢?

解决方案:

试试下面的代码:
public void turnGPSOn()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.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"));
this.ctx.sendBroadcast(poke);
}
}
...全文
10186 149 打赏 收藏 转发到动态 举报
写回复
用AI写文章
149 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanjiji 2015-07-20
  • 打赏
  • 举报
回复
4.4这个代码不行了啊,大神们有没有其他的解决方案啊
qq_29416919 2015-06-30
  • 打赏
  • 举报
回复
谢谢分享!kkkkkkkkkkkkkkkkkkk
THE-L 2015-06-08
  • 打赏
  • 举报
回复
有屁用 !!!!!!!!!!!!
  • 打赏
  • 举报
回复
CTX是神马来的,报错啊CTX
奋青斗 2014-07-21
  • 打赏
  • 举报
回复
不得行哦,需要哪些权限哦
奋青斗 2014-07-19
  • 打赏
  • 举报
回复
这代码 测试没有哦
echoJiang 2014-06-27
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/4721449/how-can-i-enable-the-gps-programmatically-on-android
private void turnGpsOn (Context context) {
    beforeEnable = Settings.Secure.getString (context.getContentResolver(),
                                              Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    String newSet = String.format ("%s,%s",
                                   beforeEnable,
                                   LocationManager.GPS_PROVIDER);
    try {
        Settings.Secure.putString (context.getContentResolver(),
                                   Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                                   newSet); 
    } catch(Exception e) {}
}


private void turnGpsOff (Context context) {
    if (null == beforeEnable) {
        String str = Settings.Secure.getString (context.getContentResolver(),
                                                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        if (null == str) {
            str = "";
        } else {                
            String[] list = str.split (",");
            str = "";
            int j = 0;
            for (int i = 0; i < list.length; i++) {
                if (!list[i].equals (LocationManager.GPS_PROVIDER)) {
                    if (j > 0) {
                        str += ",";
                    }
                    str += list[i];
                    j++;
                }
            }
            beforeEnable = str;
        }
    }
    try {
        Settings.Secure.putString (context.getContentResolver(),
                                   Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                                   beforeEnable);
    } catch(Exception e) {}
}
snbumq 2014-05-21
  • 打赏
  • 举报
回复
怎么强制打开GPS
GOTO_2000 2014-05-21
  • 打赏
  • 举报
回复
要想打开GPS,估计你得需要系统权限才能开启。 在androidmenifest文件中 声明 shared userid = system,在android.mk中 声明是platform,这样有系统级别的apk才能够开启关闭 GPS。
hushoubo 2014-04-23
  • 打赏
  • 举报
回复
刚开始学,表示看不懂
kands2011 2014-04-23
  • 打赏
  • 举报
回复
不错,真是值得学习
yiyudemei 2014-02-26
  • 打赏
  • 举报
回复
为什么我还是打不开啊
LengYueFengCai 2013-11-25
  • 打赏
  • 举报
回复
由于android 版本的升级性能也在优化,像一些耗时的http连接等需要重启一个线程去执行,这是4.0后版本升级的,把你的gps打开程序放在单独线程里就ok了
EasyVeclin 2013-11-25
  • 打赏
  • 举报
回复
EasyVeclin 2013-11-25
  • 打赏
  • 举报
回复
limengwe 2013-11-23
  • 打赏
  • 举报
回复
ok good
asdfggrr 2013-11-23
  • 打赏
  • 举报
回复
目前还看不懂
x174232 2013-10-22
  • 打赏
  • 举报
回复
是不是需要什么权限
aninna 2013-10-09
  • 打赏
  • 举报
回复
引用 127 楼 luckychechen 的回复:
我的也是
引用 125 楼 sja119 的回复:
试了一下,GPS并没有开启成功,只是一直在搜索GPS中.......
我的也是
aninna 2013-10-09
  • 打赏
  • 举报
回复
引用 126 楼 luckychechen 的回复:
我的也是
引用 124 楼 z303729470 的回复:
不错,学习了.
我的也是
加载更多回复(85)

80,348

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧