Background service with location receiver in android

I create a background service that will start in its own process. This should allow me to listen to whether the location of the device has changed. I should be able to change criteria, such as moving distance before notifying the user interface.

How can i do the same? I have little knowledge of services and LocationListener implementations. Any tutorials on the net will be appreciated.

I got one backlink from, but I didn't understand this.

+69
android android-location android-service
Jan 23 '13 at 11:05
source share
4 answers

First you need to create a Service . In this Service create a class that extends LocationListener . To do this, use the following snippet of Service code:

 public class LocationService extends Service { public static final String BROADCAST_ACTION = "Hello World"; private static final int TWO_MINUTES = 1000 * 60 * 2; public LocationManager locationManager; public MyLocationListener listener; public Location previousBestLocation = null; Intent intent; int counter = 0; @Override public void onCreate() { super.onCreate(); intent = new Intent(BROADCAST_ACTION); } @Override public void onStart(Intent intent, int startId) { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); listener = new MyLocationListener(); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, (LocationListener) listener); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener); } @Override public IBinder onBind(Intent intent) { return null; } protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it been more than two minutes since the current location, use the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers are the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); } @Override public void onDestroy() { // handler.removeCallbacks(sendUpdatesToUI); super.onDestroy(); Log.v("STOP_SERVICE", "DONE"); locationManager.removeUpdates(listener); } public static Thread performOnBackgroundThread(final Runnable runnable) { final Thread t = new Thread() { @Override public void run() { try { runnable.run(); } finally { } } }; t.start(); return t; } public class MyLocationListener implements LocationListener { public void onLocationChanged(final Location loc) { Log.i("*****", "Location changed"); if(isBetterLocation(loc, previousBestLocation)) { loc.getLatitude(); loc.getLongitude(); intent.putExtra("Latitude", loc.getLatitude()); intent.putExtra("Longitude", loc.getLongitude()); intent.putExtra("Provider", loc.getProvider()); sendBroadcast(intent); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderDisabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show(); } public void onProviderEnabled(String provider) { Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); } } 

Add this Service anywhere in your project, the way you want! :)

+108
Jan 23 '13 at 11:11
source share

I know that I am posting this answer a little late, but I felt that it was worth using the Google Plugin Location Provider service to get the current location.

Key features of this api:

1. Simple API: Allows you to select the level of accuracy, as well as energy consumption.

2. Available immediately: Gives your applications instant access to the best, most recent location.

3. Energy Efficiency:. He chooses the most efficient way to get a location with less power.

4.Versatility: Meets a wide range of needs: from the use of the foreground, which require a high-precision location to use the background, which requires periodic location updates with little impact on power.

It is also flexible when updating in location. If you want the current location to be only when your application starts, you can use the getLastLocation(GoogleApiClient) method getLastLocation(GoogleApiClient) .

If you want to constantly update your location, you can use requestLocationUpdates(GoogleApiClient,LocationRequest, LocationListener)

You can find a very good fuse location blog here and google doc for fuse location can also be found here .

Update

According to the developer docs, starting with Android O, they added new restrictions on the background layout.

If your application runs in the background, the location service calculates a new location for your application only a few times per hour. This is true even if your application requests a more frequent Update location. However, if your application runs in the foreground, there is no change in the sampling rate compared to Android 7.1.1 (API level 25).

+11
May 04 '15 at 11:43
source share

Background location service. It will be restarted even after killing the application.

MainActivity.java

 public class MainActivity extends AppCompatActivity { AlarmManager alarmManager; Button stop; PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (alarmManager == null) { alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceive.class); pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, pendingIntent); } } } 

BookingTrackingService.java

 public class BookingTrackingService extends Service implements LocationListener { private static final String TAG = "BookingTrackingService"; private Context context; boolean isGPSEnable = false; boolean isNetworkEnable = false; double latitude, longitude; LocationManager locationManager; Location location; private Handler mHandler = new Handler(); private Timer mTimer = null; long notify_interval = 30000; public double track_lat = 0.0; public double track_lng = 0.0; public static String str_receiver = "servicetutorial.service.receiver"; Intent intent; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mTimer = new Timer(); mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval); intent = new Intent(str_receiver); } @Override public int onStartCommand(Intent intent, int flags, int startId) { this.context = this; return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.e(TAG, "onDestroy <<"); if (mTimer != null) { mTimer.cancel(); } } private void trackLocation() { Log.e(TAG, "trackLocation"); String TAG_TRACK_LOCATION = "trackLocation"; Map<String, String> params = new HashMap<>(); params.put("latitude", "" + track_lat); params.put("longitude", "" + track_lng); Log.e(TAG, "param_track_location >> " + params.toString()); stopSelf(); mTimer.cancel(); } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } /******************************/ private void fn_getlocation() { locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE); isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnable && !isNetworkEnable) { Log.e(TAG, "CAN'T GET LOCATION"); stopSelf(); } else { if (isNetworkEnable) { location = null; locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { Log.e(TAG, "isNetworkEnable latitude" + location.getLatitude() + "\nlongitude" + location.getLongitude() + ""); latitude = location.getLatitude(); longitude = location.getLongitude(); track_lat = latitude; track_lng = longitude; // fn_update(location); } } } if (isGPSEnable) { location = null; locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { Log.e(TAG, "isGPSEnable latitude" + location.getLatitude() + "\nlongitude" + location.getLongitude() + ""); latitude = location.getLatitude(); longitude = location.getLongitude(); track_lat = latitude; track_lng = longitude; // fn_update(location); } } } Log.e(TAG, "START SERVICE"); trackLocation(); } } private class TimerTaskToGetLocation extends TimerTask { @Override public void run() { mHandler.post(new Runnable() { @Override public void run() { fn_getlocation(); } }); } } // private void fn_update(Location location) { // // intent.putExtra("latutide", location.getLatitude() + ""); // intent.putExtra("longitude", location.getLongitude() + ""); // sendBroadcast(intent); // } } 

AlarmReceive.java (BroadcastReceiver)

 public class AlarmReceive extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.e("Service_call_" , "You are in AlarmReceive class."); Intent background = new Intent(context, BookingTrackingService.class); // Intent background = new Intent(context, GoogleService.class); Log.e("AlarmReceive ","testing called broadcast called"); context.startService(background); } } 

AndroidManifest.xml

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <service android:name=".ServiceAndBroadcast.BookingTrackingService" android:enabled="true" /> <receiver android:name=".ServiceAndBroadcast.AlarmReceive" android:exported="false"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 
+6
Jun 19 '17 at 12:00
source share

It’s very simple that you don’t need to create an extends LocationListener class 1- Variable

 private LocationManager mLocationManager; private LocationListener mLocationListener; private static double currentLat =0; private static double currentLon =0; 

2- onStartService ()

 @Override public void onStartService() { addListenerLocation(); } 

3- addListenerLocation () method

  private void addListenerLocation() { mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { currentLat = location.getLatitude(); currentLon = location.getLongitude(); Toast.makeText(getBaseContext(),currentLat+"-"+currentLon, Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { Location lastKnownLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if(lastKnownLocation!=null){ currentLat = lastKnownLocation.getLatitude(); currentLon = lastKnownLocation.getLongitude(); } } @Override public void onProviderDisabled(String provider) { } }; mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 500, 10, mLocationListener); } 

4- onDestroy ()

 @Override public void onDestroy() { super.onDestroy(); mLocationManager.removeUpdates(mLocationListener); } 
+2
Sep 06 '16 at 8:09
source share



All Articles