1

Android will kill some service when memory is not enough.

Like this:

app stopped

I know I can use foreground service to prohibit android to kill my service

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            Notification notification = new Notification(R.mipmap.ic_launcher,"this is service", System.currentTimeMillis());

            Intent intent = new Intent(this, MainActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent , 0);
            notification.setLatestEventInfo(this, "myapp", "myservice", contentIntent);
            notification.flags =Notification.FLAG_AUTO_CANCEL;
            startForeground(123,notification);
        }
        catch(Exception e)
        {
            stopSelf();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

But this will display a notification on screen

I would rather kill service than display notification, but I also don't want to display stopped message.

I found some app, it can display no message when android kills it.

e.g. Screen Dimmer

How can I prohibit android to display app stopped message?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
CL So
  • 3,647
  • 10
  • 51
  • 95

2 Answers2

3

Check this: https://stackoverflow.com/a/32229266/2965799

According to that I have used the following code to handle the exception. I wanted to display another message so I added my own message however if you use his answer there will be no messages.

    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread paramThread, Throwable paramThrowable) {

            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(getActivity(),"Your message", Toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }.start();
            try
            {
                Thread.sleep(4000); // Let the Toast display before app will get shutdown
            }
            catch (InterruptedException e) {    }
            System.exit(2);
        }
    });
Community
  • 1
  • 1
Taher
  • 1,185
  • 3
  • 18
  • 36
2

One way is to implement a UncaughtExceptionHandler with your own custom failure code. The API to install your handler is this:

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh);

The class is documented here. As a very basic example:

import java.lang.Thread.UncaughtExceptionHandler;

public final class CrashHandler implements UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
       android.util.Log.wtf("My app name", "Oops, caught it dying on me!");
    }
}

A full working example is available here.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114