在Android操作系统中,“设置” - >“位置服务”,有一个名为“访问我的位置”,它可以用来切换按钮禁用&从应用程序启用位置信息访问。聆听位置接入禁用/启用设置
目前,我正在开发一个位置服务应用程序。我想知道,我如何在Android项目中听这个设置?当用户禁用或启用“访问我的位置”时,是否有任何广播接收器可以立即使用?
如果没有任何广播接收器,我怎样才能在我的Android项目中听到这种变化?
在Android操作系统中,“设置” - >“位置服务”,有一个名为“访问我的位置”,它可以用来切换按钮禁用&从应用程序启用位置信息访问。聆听位置接入禁用/启用设置
目前,我正在开发一个位置服务应用程序。我想知道,我如何在Android项目中听这个设置?当用户禁用或启用“访问我的位置”时,是否有任何广播接收器可以立即使用?
如果没有任何广播接收器,我怎样才能在我的Android项目中听到这种变化?
您可以使用下面的代码来检查位置服务是否启用或不
LocationManager lm = null;
boolean gps_enabled,network_enabled;
if(lm==null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try{
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}catch(Exception ex){}
try{
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}catch(Exception ex){}
if(!gps_enabled && !network_enabled){
dialog = new AlertDialog.Builder(context);
dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
context.startActivity(myIntent);
//get gps
}
});
dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
// TODO Auto-generated method stub
}
});
dialog.show();
}
嗨,我的问题不是关于如何检查值,而是关于如何听这意味着如何在代码中获取用户已更改此位置访问权限的通知并获取更改后的值。 – Mellon
@GrIsHu这是不正确的。你有没有尝试下面的usman解决方案? –
这个工作对我来说:
添加接收到清单文件:
<receiver
android:name="com.eegeo.location.LocationProviderChangedReceiver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</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);
}
}
虽然这个接收器由于显而易见的原因被多次调用,但这会告诉你状态。
仅当应用程序需要位于后台时才应使用清单广播接收器。否则它应该使用运行时广播接收器 –
这样你可以做得更简单。在你的广播接收器的的onReceive()方法:
ContentResolver contentResolver = getContext().getContentResolver();
// Find out what the settings say about which providers are enabled
int mode = Settings.Secure.getInt(
contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
if (mode == Settings.Secure.LOCATION_MODE_OFF) {
// Location is turned OFF!
} else {
// Location is turned ON!
}
〜感谢这段代码:LocationManagerTest.java
@ GrlsHu,不,我不想这样做。我的问题的关键点是如何听这个位置访问设置。 – Mellon
http://stackoverflow.com/a/20673766/1994950和http://stackoverflow.com/a/23756293/1994950将帮助 – Kushal