amanda45
amanda45

Reputation: 595

Android Service doesn't Stop running

I create a service and put it in the foreground with a notification. In the service I have a timer that just prints "SERVICE STILL RUNNING" to let me know the service is still alive. When I close the app, the OnDestroy() of the service is called but the timmer keeps printing "SERVICE STILL RUNNING" why is that? I put the service in the foreground by calling showNotification(). If I don't call showNotification() and close the app the service gets destroyed and the timmer stops printing "SERVICE STILL RUNNING". How can I have the service in the foreground and kill it correctly when the app is closed. The memory monitor continues to show the memory usage even after the app is closed. If I dont put the service in the foreground and close the app, the memory monitor stops.This problem only happens and android 6.0.

MainActivity:

public class MainActivity extends Activity {

    Intent service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        service = new Intent(MainActivity.this, MyService.class);

        // starting Client service
        service.setAction("START");
        startService(service);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        service.setAction("STOP");
        stopService(service);
    }
}

Service:

public class MyService extends Service {

    Timer timer;




    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
       //return super.onStartCommand(intent, flags, startId);
        if(intent !=null)
        {
            if(intent.getAction().equals("START"))
            {

               showNotification();
            } else if(intent.getAction().equals("STOP"))
            {
                stopForeground(true);
                stopSelf();

            }
        }else
        {
            //stopForeground(true);
            //stopSelf();

        }




        return START_STICKY;
    }


    @Override
    public void onCreate() {
        super.onCreate();

        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run()
            {


                Log.d("","SERVICE STILL RUNNING");

            }
        },1*1000,5*1000);
    }

    private void showNotification() {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction("new");
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);


        Bitmap icon = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("Title")
                .setTicker("Test")
                .setContentText("testing")
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
                .setContentIntent(pendingIntent)
                .setOngoing(true).build();

        startForeground(101, notification);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("","SERVICE HAS BEEN DESTROYED!!!");
    }
}

Upvotes: 1

Views: 3679

Answers (2)

Use inside your service code:

@Override
public void onDestroy() {
    stopForeground(true);
    super.onDestroy();
    System.exit(0); //We terminate the app!
}

Upvotes: 0

Luke Villanueva
Luke Villanueva

Reputation: 2070

Try adding this on you Service's onDestroy

@Override
public void onDestroy() {
    super.onDestroy();
    timer.cancel();
    timer = null;
    stopForeground(true);//Add this. Since stopping a service in started in foreground is different from normal services.
    Log.d("","SERVICE HAS BEEN DESTROYED!!!");
}

EDIT

On your Activity's onDestroy method

@Override
protected void onDestroy() {
    super.onDestroy();
    service.setAction("STOP");
    service.executeStopForeground();
    stopService(service);
}

Then add this method on your service:

public void executeStopForeground()
{
    stopForeground(true);
}

Upvotes: 1

Related Questions