如何在android4.0中開啓GPS

原問題來自於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);
    }
}


 

發佈了95 篇原創文章 · 獲贊 125 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章