以编程方式打开/关闭 Android 设备 GPS [英] Android device GPS on/off programmatically
问题描述
我正在使用以下代码打开/关闭 GPS.
I am using following code for GPS on/off.
//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);
我需要以编程方式在 android 设备上打开/关闭 GPS.我正在使用上面的代码.但它并不适用于所有设备.
Programmatically I need to on/off GPS on android device. I am using above code for that. But It doesn't work on all the devices.
推荐答案
根据我的个人经验,我正在回答这个问题,
From my personal experience, I am answering this,
您在问题中显示的黑客代码已从 Android 4.4 版停止运行.您将从 Kitkat 版本开始触发此异常
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.location.GPS_ENABLED_CHANGE
第一个 answer 的代码将不再起作用,它只会在通知中显示动画 GPS 图标酒吧.
The First answer's code will not work any more, it only display animated GPS icon in notification bar.
出于安全目的,Google 开发人员在这两种之前都可以正常工作的方法上设置了块.
For The security purpose Google developer has block above both methods which were previously working fine.
因此结论是您无法以编程方式打开或关闭 GPS.
Hence conclusion is that You can not programmatically start GPS On or Off.
这篇关于以编程方式打开/关闭 Android 设备 GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!