Android device GPS on/off programmatically

后端 未结 5 1149
深忆病人
深忆病人 2020-11-27 07:40

I am using following code for GPS on/off.

//Enable GPS
Intent intent = new Intent(\"android.location.GPS_ENABLED_CHANGE\");
intent.putExtra(\"enabled\", true         


        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 08:27

    This code works on Rooted Phone

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
    
    
            String[] cmds = {"cd /system/bin" ,"settings put secure location_providers_allowed +gps"};
            try {
                Process p = Runtime.getRuntime().exec("su");
                DataOutputStream os = new DataOutputStream(p.getOutputStream());
                for (String tmpCmd : cmds) {
                    os.writeBytes(tmpCmd + "\n");
                }
                os.writeBytes("exit\n");
                os.flush();
            }
            catch (IOException e){
                e.printStackTrace();
            }
    
        }
    }
    

提交回复
热议问题