以编程方式打开/关闭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,
-
您在问题中显示的hack代码已从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屋!