简体   繁体   中英

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() . 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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