网上打开与关闭gps几种方案测试,其中方案3可行
? ? ? ?對于網(wǎng)上提到的幾種開啟或者關閉GPS方案,分別進行了測試,開發(fā)環(huán)境android 7.0,時間202007;
其中方案1和2,成功;方案3,成功;
方案1:(失敗)
// 這段代碼可以實現(xiàn)GPS開關狀態(tài)的切換-經(jīng)過測試不行;20200722 // Intent GPSIntent = new Intent(); // GPSIntent.setClassName("com.android.settings", // "com.android.settings.widget.SettingsAppWidgetProvider"); // GPSIntent.addCategory("android.intent.category.ALTERNATIVE"); // GPSIntent.setData(Uri.parse("custom:3")); // try { // PendingIntent.getBroadcast(MainActivity.this, 0, GPSIntent, 0).send(); // } catch (PendingIntent.CanceledException e) { // e.printStackTrace(); // } // try { // Thread.sleep(2000); // } catch (InterruptedException e) { // e.printStackTrace(); // }方案2:(失敗)
Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);在權限添加這里卡住,提示需要在系統(tǒng)-app目錄下;
<uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />方案3:(可以)
?
//開啟GPS
public static final String GPS_ON = "settings put secure location_providers_allowed +gps";
?? ?
//關閉GPS
public static final String GPS_OFF = "settings put secure location_providers_allowed -gps";
?
//查看GPS狀態(tài)
public static final String QUERY_GPS_STATE = "settings get secure location_providers_allowed";
?
參考:https://blog.csdn.net/u013512708/article/details/103087761?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-6?
private void toggleGPS(String cmd){//Runtime對象Runtime runtime = Runtime.getRuntime();OutputStream localOutputStream=null;DataOutputStream localDataOutputStream=null;try {Process localProcess = runtime.exec("su");localOutputStream = localProcess.getOutputStream();localDataOutputStream = new DataOutputStream(localOutputStream);localDataOutputStream.writeBytes(cmd);localDataOutputStream.flush();Log.e(TAG,"執(zhí)行命令"+cmd);} catch (IOException e) {//MyLog.e(TAG+"strLine:"+e.getMessage());e.printStackTrace();}finally {if(localDataOutputStream!= null){try {localDataOutputStream.close();} catch (IOException e) {e.printStackTrace();}}if(localOutputStream!= null){try {localOutputStream.close();} catch (IOException e) {e.printStackTrace();}}} }總結
以上是生活随笔為你收集整理的网上打开与关闭gps几种方案测试,其中方案3可行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [css] 对比下px、em、rem有
- 下一篇: [vue] vue实例挂载的过程是什么?