Search code examples
androidbroadcastreceiver

how to send Broadcast to BroadcastReceiver when location of device changes, in Android


In my android application, I want to sendBroadcast to BroadcastReceiver when the location of the device changes, even when my application is not running. I am new to android, please tell how can I do that?


Solution

  • Try this

        LocationManager manager=    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     manager.requestLocationUpdates("gps", 10, 10, new LocationListener() {
     @Override
     public void onLocationChanged(Location location) {
       sendBroadcast(rec); 
     }
    
     @Override
     public void onStatusChanged(String provider, int status, Bundle extras) {
    
     }
    
     @Override
     public void onProviderEnabled(String provider) {
    
     }
    
     @Override
     public void onProviderDisabled(String provider) {
    
     }
    });
    

    In order to broadcast in background you have to make use of AsyncTask or Service