我有一个发送推送的应用程序。推送发送请求以启用位置服务。 我收到推送后,立即拨打通知,并通知行动
Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(viewIntent);
但是如果用户启用了位置设置,如何记录。
在broadcastReceiver
收到推送,因此我们没有startActivityForResult()
。还有其他办法吗?
答案 0 :(得分:0)
我希望此代码能为您提供帮助:
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" />
答案 1 :(得分:0)
试试这个。
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>