How to? Listen for Location Setting being turned ON (Android App)

后端 未结 3 1451
借酒劲吻你
借酒劲吻你 2021-01-02 02:30

So I\'ve spent the past few weeks working on my Android App and looking into the best way of implementing what I need to do, but still can\'t quite get it right.. Any/all he

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-02 03:35

    Since you don't need to actually get a Location, the best implementation for your needs would be a BroadcastReceiver.

    This is the best option because you wouldn't need to have a Service running at all times (resulting in extra batter drain), and you would be able to start your Activity from the BroadcastReceiver.

    With the intent filter and BroadcastReceiver, your app will be started by the OS whenever the Location setting has changed (enabled or disabled), and in the case that it is enabled, you can start your Activity from the BroadcastReceiver.

    First add the intent filter, which will be captured when the OS sends out the implicit Intent that the setting has changed.

    
        
            
            
        
    
    

    Note that for this to work on Android Oreo and above, you'll need to register the broadcast receiver at runtime, see here: https://developer.android.com/guide/components/broadcasts#context-registered-receivers

    Then, in LocationProviderChangedReceiver.java, your implementation would be something like this:

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.location.LocationManager;
    import android.util.Log;
    import android.widget.Toast;
    
    public class LocationProviderChangedReceiver extends BroadcastReceiver {
        private final static String TAG = "LocationProviderChanged";
    
        boolean isGpsEnabled;
        boolean isNetworkEnabled;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
            {
                Log.i(TAG, "Location Providers changed");
    
                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
                //Start your Activity if location was enabled:
                if (isGpsEnabled || isNetworkEnabled) {
                      Intent i = new Intent(context, YourActivity.class);
                      context.startActivity(i);
                }
            }
        }
    }
    

    Edit

    Updated solution with Kotlin and registering receiver at runtime for Android 9.

    The BroadcastReceiver class in Kotlin:

    class LocationProviderChangedReceiver : BroadcastReceiver() {
    
        internal var isGpsEnabled: Boolean = false
        internal var isNetworkEnabled: Boolean = false
    
        override fun onReceive(context: Context, intent: Intent) {
            intent.action?.let { act ->
                if (act.matches("android.location.PROVIDERS_CHANGED".toRegex())) {
                    val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
                    isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
                    isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
    
                    Log.i(TAG, "Location Providers changed, is GPS Enabled: " + isGpsEnabled)
    
                    //Start your Activity if location was enabled:
                    if (isGpsEnabled || isNetworkEnabled) {
                        val i = Intent(context, YourActivity::class.java)
                        context.startActivity(i)
                    }
                }
            }
        }
    
        companion object {
            private val TAG = "LocationProviderChanged"
        }
    }
    

    Register at runtime, for example in onCreate() of your app's MainActivity:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        val br: BroadcastReceiver = LocationProviderChangedReceiver()
        val filter = IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
        registerReceiver(br, filter)
    }
    

提交回复
热议问题