Android设备GPS以编程方式开/关

Shr*_*khe 11 gps android android-intent

我正在使用以下代码开启/关闭GPS.

//Enable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
context.sendBroadcast(intent);
//Disable GPS
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
context.sendBroadcast(intent);
Run Code Online (Sandbox Code Playgroud)

我可以通过编程方式在Android设备上开启/关闭GPS.我正在使用上面的代码.但它并不适用于所有设备.

Luc*_*fer 26

根据我的个人经验,我正在回答这个问题,

  • 您在问题中显示的黑客代码已从Android 4.4版停止运行.您将从Kitkat版本开始触发此例外java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE

  • 第一个答案的代码将不再起作用,它只在通知栏中显示动画GPS图标.

  • 出于安全目的,Google开发人员阻止了以前正常工作的两种方法.

  • 因此,结论是您无法以编程方式启动或关闭GPS.