当前位置:操作系统 > 安卓/Android >>

如何在android4.0中开启GPS

问题描述:

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

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

解决方案:

试试下面的代码:

[java]
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); 
    } 

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);
    }
}

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,