简体   繁体   English

用户启用位置服务后如何获得通知

[英]How to get notify when user has enabled Location services

I have an application that sends push. 我有一个发送推送的应用程序。 Push sends request to enable Location services. 推送发送请求以启用位置服务。 As soon as I receive push, I have called a notification, and on notification action 收到推送后,我会立即打电话通知,并通知我

Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(viewIntent);

But how to log, if the user has enabled location settings or not. 但是,如果用户未启用位置设置,如何记录。

Push is received in broadcastReceiver , So we dont have startActivityForResult() . 推送是在broadcastReceiver接收的,因此我们没有startActivityForResult() Is there any other way to do? 还有其他方法吗?

I hope this code will help you : 我希望这段代码对您有帮助:

 private boolean isLocationEnabled()
  {
   LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
   return service.isProviderEnabled(LocationManager.GPS_PROVIDER)||service.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

Add this permissions to manifest file : 将此权限添加到清单文件:

<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" />

Try this. 尝试这个。

    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);
    }

Also add this permission to menifest file. 还要将此权限添加到清单文件中。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM