如何在Android上以編程方式啟用或禁用GPS?我知道在Android上用程序打開/關(guān)閉GPS的問題有 一直. 討論 許多 時(shí)代,答案總是一樣的:“出于安全/隱私的原因,您不能轉(zhuǎn)發(fā)到位置首選項(xiàng)屏幕,讓用戶啟用/禁用它。”我知道,不過我最近買了塔斯克在市場上,以及你可以用它完成的許多其他事情中,你可以在進(jìn)入預(yù)先確定的應(yīng)用程序時(shí)設(shè)置自動(dòng)啟用gps的規(guī)則,并在退出時(shí)禁用它(請(qǐng)參閱這里關(guān)于如何做到這一點(diǎn)的教程,它只是起作用了!)這個(gè)應(yīng)用程序不能用固件簽名鍵簽名,因?yàn)樗梢栽诤芏郃ndroid版本和不同的設(shè)備上工作,你甚至不需要扎根。我想在我的應(yīng)用程序中這樣做。當(dāng)然,我不想破壞用戶的隱私,所以我首先問用戶是否想用典型的“記住我的決定”復(fù)選框自動(dòng)打開它,如果他回答是,啟用它。有沒有人知道塔斯克是如何做到這一點(diǎn)的?
4 回答

手掌心
TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
/system/aps
, 并且它們在清單中具有以下權(quán)限:
<uses-permission android:name="android.permission.WRITE_SETTINGS"/><uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
電碼
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) {}}
- 4 回答
- 0 關(guān)注
- 1114 瀏覽
添加回答
舉報(bào)
0/150
提交
取消