[英]How to get notify when user has enabled Location services
我有一个发送推送的应用程序。 推送发送请求以启用位置服务。 收到推送后,我会立即打电话通知,并通知我
Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(viewIntent);
但是,如果用户未启用位置设置,如何记录。
推送是在broadcastReceiver
接收的,因此我们没有startActivityForResult()
。 还有其他方法吗?
我希望这段代码对您有帮助:
private boolean isLocationEnabled()
{
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
return service.isProviderEnabled(LocationManager.GPS_PROVIDER)||service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
将此权限添加到清单文件:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
尝试这个。
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){
///////////// do you dialog show code here
////////////call the intent to open location service setting
Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
context.startActivity(myIntent);
}
还要将此权限添加到清单文件中。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.