问题描述
在Android OS中,在"设置"->"位置服务"中,有一个名为"访问我的位置",可用于禁用&启用来自应用的位置信息访问权限.
In Android OS, in "Settings" --> "Location services", there is a toggle button named "Access to my location" which can be used to disable & enable location info access from apps.
当前,我正在开发位置服务应用程序.我想知道,如何在我的Android项目中收听此设置?当用户禁用或启用"访问我的位置"时,我可以立即使用任何广播接收器吗?
Currently, I am developing a location service application. I am wondering, how can I listen to this setting in my Android project? Is there any broadcast receiver I can use to know right away when user disable or enable "Access to my location" ?
如果没有任何广播接收器,我该如何在Android项目中收听此更改?
If there isn't any broadcast receiver for it, how can I listen to this change in my Android project?
推荐答案
这对我有用:
将接收者添加到清单文件中:
Add receiver to the Manifest file:
<receiver
android:name="com.eegeo.location.LocationProviderChangedReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>
检查接收器中的两个位置提供者:
Check both location providers in receiver:
public class LocationProviderChangedReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
boolean anyLocationProv = false;
LocationManager locationManager = (LocationManager) MyMainActivity.context.getSystemService(Context.LOCATION_SERVICE);
anyLocationProv |= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
anyLocationProv |= locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
Log.i("", "Location service status" + anyLocationProv);
}
}
尽管由于明显的原因多次调用了此接收器,但这会告诉您状态.
Though this receiver is called more than once due to obvious reasons, but this will tell you the status.
这篇关于在设置中收听/禁用位置信息访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!