如果手機 GPS 設定沒有開啟,可以透過程式碼來開啟嗎?

 根據筆者明查暗訪的結果,似乎有人說可以,也有人說不行,但是把網路上講的程式碼都試過一遍,至少在筆者的 Acer liquid e1 以及 Asus nexus 7 是不行的

網路上資料參考

http://stackoverflow.com/questions/4721449/how-can-i-enable-the-gps-programmatically-on-android

http://stackoverflow.com/questions/15426144/turning-on-and-off-gps-programmatically-in-android-4-0-and-above

http://www.cnblogs.com/wii/archive/2012/03/18/2404947.html

如果使用

Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);

或是類似的

Settings.Secure.putString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, LocationManager.GPS_PROVIDER);

兩者都會需要開啟 android.permission.WRITE_SECURE_SETTINGS,不過如果你有詳讀上述網頁的話,你會發現一般的 app 應該是無法取得這樣的權限的,即便你在 AndroidManifest.xml 指定也是一樣。

至於另外一種寫法

Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);

筆者測試也是不行,不知道是手機得 root 過,還是筆者的語法是錯誤的,筆者在 XE5 中是這樣寫的

poke := TJIntent.Create;
poke.setClassName(StringToJString('com.android.settings'), StringToJString('com.android.settings.widget.SettingsAppWidgetProvider'));
poke.addCategory(TJIntent.JavaClass.CATEGORY_ALTERNATIVE);
poke.setData(TJnet_Uri.JavaClass.parse(StringToJString('3')));
SharedActivity.sendBroadcast(poke);

所以筆者認為,app 應該是無法直接開啟 GPS 設定,只能在偵測到沒有開啟 GPS 服務時,透過程式控制自動切換到「定位服務」那頁,提示使用者開啟

如果筆者有錯的話,歡迎不吝指教...

 

to be continued...

arrow
arrow
    文章標籤
    補遺 xe5 android app gps
    全站熱搜

    縹緲 發表在 痞客邦 留言(1) 人氣()