Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the skeleton of the GPS <code>Service</code> I used in my tracking application, it has been tested, and works fine I can guarantee that. I hope it will help you out.</p> <pre><code>import java.util.Timer; import java.util.TimerTask; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.preference.PreferenceManager; import android.util.Log; public class TrackingService extends Service { private static final String TAG = TrackingService.class.getSimpleName(); private static final long TIME_BETWEEN_UPDATES = 1000L; private static final long MINIMUM_DISTANCE_CHANGE = 0L; private WakeLock mWakeLock; private LocationManager mLocationManager; private final Timer mTimer = new Timer(); private Handler mHandler = new Handler(); private LocationListener mLocationListenerGps = new LocationListener() { public void onLocationChanged(Location location) { // Your code here } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; private void registerLocationListener() { if (mLocationManager == null) { Log.e(TAG, "TrackingService: Do not have any location manager."); return; } Log.d(TAG, "Preparing to register location listener w/ TrackingService..."); try { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE, mLocationListenerGps); Log.d(TAG, "...location listener now registered w/ TrackingService @ " + TIME_BETWEEN_UPDATES); } catch (RuntimeException e) { Log.e(TAG, "Could not register location listener: " + e.getMessage(), e); } } private void unregisterLocationListener() { if (mLocationManager == null) { Log.e(TAG, "TrackingService: Do not have any location manager."); return; } mLocationManager.removeUpdates(mLocationListenerGps); Log.d(TAG, "Location listener now unregistered w/ TrackingService."); } private TimerTask mCheckLocationListenerTask = new TimerTask() { @Override public void run() { mHandler.post(new Runnable() { public void run() { Log.d(TAG, "Re-registering location listener with TrackingService."); unregisterLocationListener(); registerLocationListener(); } }); } }; @Override public void onCreate() { super.onCreate(); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); registerLocationListener(); mTimer.schedule(mCheckLocationListenerTask, 1000 * 60 * 5, 1000 * 60); acquireWakeLock(); Log.d(TAG, "Service started..."); } @Override public void onStart(Intent intent, int startId) { handleStartCommand(intent, startId); } @Override public int onStartCommand(Intent intent, int flags, int startId) { handleStartCommand(intent, startId); return START_STICKY; } private void handleStartCommand(Intent intent, int startId) { Notification notification = new Notification(R.drawable.ic_launcher, getText(R.string.trackingservice_notification_rolling_text), System.currentTimeMillis()); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(this, getText(R.string.trackingservice_notification_ticker_title), getText(R.string.trackingservice_notification_ticker_text), contentIntent); startForeground(1, notification); } @Override public void onDestroy() { stopForeground(true); mTimer.cancel(); mTimer.purge(); mHandler.removeCallbacksAndMessages(null); unregisterLocationListener(); releaseWakeLock(); super.onDestroy(); Log.d(TAG, "Service stopped..."); } private void acquireWakeLock() { try { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); if (pm == null) { Log.e(TAG, "TrackRecordingService: Power manager not found!"); return; } if (mWakeLock == null) { mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); if (mWakeLock == null) { Log.e(TAG, "TrackRecordingService: Could not create wake lock (null)."); return; } } if (!mWakeLock.isHeld()) { mWakeLock.acquire(); if (!mWakeLock.isHeld()) { Log.e(TAG, "TrackRecordingService: Could not acquire wake lock."); } } } catch (RuntimeException e) { Log.e(TAG, "TrackRecordingService: Caught unexpected exception: " + e.getMessage(), e); } } /** * Releases the wake lock if it's currently held. */ private void releaseWakeLock() { if (mWakeLock != null &amp;&amp; mWakeLock.isHeld()) { mWakeLock.release(); mWakeLock = null; } } } </code></pre> <p>And in your AndroidManifest.xml file you need</p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt; &lt;uses-permission android:name="android.permission.WAKE_LOCK"/&gt; </code></pre> <p>Just fill out the GPS location listener, Register the service in your <code>AndroidManifest.xml</code>, start the service from your activity, and enjoy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload