public void startDelayed() { Class serviceProviderClass = null; AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); try { Intent serviceIntent = new Intent(this, ServiceProvider.getClass(config.getServiceProvider())); serviceIntent.addFlags(Intent.FLAG_FROM_BACKGROUND); serviceIntent.putExtra("config", config.toParcel().marshall()); PendingIntent pintent = PendingIntent.getService(this, 0, serviceIntent, 0); alarmManager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 1000, pintent); } catch (ClassNotFoundException e) { Log.e(TAG, "Service restart failed"); } }
// @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onTaskRemoved(Intent rootIntent) { Log.d(TAG, "Task has been removed"); unregisterActionReceiver(); if (config.getStopOnTerminate()) { stopSelf(); } super.onTaskRemoved(rootIntent); }
@Override public boolean stopService(Intent intent) { Log.i(TAG, "- Received stop: " + intent); cleanUp(); if (config.isDebugging()) { Toast.makeText(this, "Background location tracking stopped", Toast.LENGTH_SHORT).show(); } return super.stopService(intent); // not needed??? }
protected void handleLocation(Location location) { final LocationProxy bgLocation = LocationProxy.fromAndroidLocation(location); bgLocation.setServiceProvider(config.getServiceProvider()); if (config.isDebugging()) { bgLocation.setDebug(true); persistLocation(bgLocation); } Log.d(TAG, "Broadcasting update message: " + bgLocation.toString()); try { String locStr = bgLocation.toJSONObject().toString(); Intent intent = new Intent(Constant.ACTION_FILTER); intent.putExtra(Constant.ACTION, Constant.ACTION_LOCATION_UPDATE); intent.putExtra(Constant.DATA, locStr); this.sendOrderedBroadcast( intent, null, new BroadcastReceiver() { // @SuppressLint("NewApi") @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "Final Result Receiver"); Bundle results = getResultExtras(true); if (results.getString(Constant.LOCATION_SENT_INDICATOR) == null) { Log.w(TAG, "Main activity seems to be killed"); if (config.getStopOnTerminate() == false) { bgLocation.setDebug(false); persistLocation(bgLocation); Log.d(TAG, "Persisting location. Reason: Main activity was killed."); } } } }, null, Activity.RESULT_OK, null, null); } catch (JSONException e) { Log.w(TAG, "Failed to broadcast location"); } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "Received start id " + startId + ": " + intent); if (intent != null) { // config = Config.fromByteArray(intent.getByteArrayExtra("config")); config = (Config) intent.getParcelableExtra("config"); Log.i(TAG, "Config: " + config.toString()); // Build a Notification required for running service in foreground. NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentTitle(config.getNotificationTitle()); builder.setContentText(config.getNotificationText()); builder.setSmallIcon(android.R.drawable.ic_menu_mylocation); if (config.getNotificationIcon() != null) { builder.setSmallIcon(getPluginResource(config.getSmallNotificationIcon())); builder.setLargeIcon( BitmapFactory.decodeResource( getApplication().getResources(), getPluginResource(config.getLargeNotificationIcon()))); } if (config.getNotificationIconColor() != null) { builder.setColor(this.parseNotificationIconColor(config.getNotificationIconColor())); } setClickEvent(builder); Notification notification = builder.build(); notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR; startForeground(startId, notification); } // We want this service to continue running until it is explicitly stopped return START_REDELIVER_INTENT; }