0

I have an activity with two buttons (ON and OFF): when I click the ON button a service starts. When I click the OFF botton the service stops. Now, my service does not have problems apart from when I kill the app from "Recent Apps" because in this circumstance the service restarts. I don't want that it restarts but I want that it continues working. The service is a START_STICKY.
This is my "service" code:

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    myServiceReceiver = new MyServiceReceiver();
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    // Display a notification about us starting.  We put an icon in the status bar.
    showNotification();
}

public class LocalBinder extends Binder {
    SensorService getService() {
        return SensorService.this;
    }
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return mBinder;
}

// This is the object that receives interactions from clients.  See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    if (!running){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(MY_ACTION_FROMACTIVITY);
        registerReceiver(myServiceReceiver, intentFilter);
        running = true;

        sensorManager=(SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),   SensorManager.SENSOR_DELAY_NORMAL);
    }
    //return super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

@Override
public void onDestroy() {
    Log.v(TAG,"destroy");
    // TODO Auto-generated method stub
    sensorManager.unregisterListener(this);
    mNM.cancel(NOTIFICATION);
    this.unregisterReceiver(myServiceReceiver);
    super.onDestroy();
}


private void showNotification() {
    CharSequence text = getText(R.string.activity);
    Notification notification = new Notification(R.drawable.lifestyle, text, System.currentTimeMillis());

    SharedPreferences flagNot = getSharedPreferences("flagNotification", 0);
    final SharedPreferences.Editor editor = flagNot.edit();
    editor.putBoolean("flagNotification",true);
    editor.commit();

    Intent notificationIntent = new Intent(this, ActivityTab.class);
    // The PendingIntent to launch our activity if the user selects this notification
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.app_name),text, contentIntent);
    notification.flags |= Notification.FLAG_NO_CLEAR;
    // Send the notification.
    mNM.notify(NOTIFICATION, notification);
}


public void onAccuracyChanged(Sensor sensor,int accuracy){

}

public void onSensorChanged(SensorEvent event){
    ......
}

public class MyServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        int hostCmd = arg1.getIntExtra(CMD, 0);
        if(hostCmd == CMD_STOP){
            running = false;

            stopSelf();
        }
    }
}       

}

Can you help me,please?
Many thanks.

Grancein
  • 642
  • 10
  • 19
  • we are developers. we can't interfere in OS operation. When your app kills, OS must stop all background services automatically so it can free memory. you can continue the service till the app will forcefully stop. – Ranjit Jul 29 '14 at 14:42
  • I know it. To explain better what I want, you take as example the countdown of google clock. If it starts and I kill the app from Recent Apps, the countdown doesn't restart from the beginning but it continues. – Grancein Jul 29 '14 at 14:55
  • There are two different levels to this; one is prevent the service from being killed, an entirely different one is allowing a *replacement* service to pick up where the killed one left off, in a way that is transparent to the user. – Chris Stratton Jul 29 '14 at 19:00

1 Answers1

3

Implement startForeground in your service and send it a persistent notification.

private void startForeground() {
    int ID = 1234;
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);

    Notification.Builder builder = new Notification.Builder(getBaseContext());
    builder.setContentIntent(pendIntent);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setTicker("CUSTOM MESSAGE");
    builder.setWhen(System.currentTimeMillis());
    builder.setAutoCancel(false);
    builder.setContentTitle("Test Service");
    builder.setContentText("CUSTOM MESSAGE");

    Notification notification = builder.build();

    startForeground(ID, notification);
}
Jason John
  • 934
  • 2
  • 12
  • 23