简体   繁体   中英

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?

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

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