如何防止Android显示应用程序停止消息

如何防止Android显示应用程序停止消息,android,Android,当内存不足时,Android会停止一些服务 像这样: 我知道我可以使用前台服务来禁止android终止我的服务 public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate()

当内存不足时,Android会停止一些服务

像这样:

我知道我可以使用前台服务来禁止android终止我的服务

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);
    }
}
但这将在屏幕上显示通知

我宁愿终止服务也不愿显示通知,但我也不想显示停止的消息

我发现了一些应用程序,它可以显示没有消息时,android杀死它

e、 g.屏幕调光器


如何禁止android显示应用程序停止消息?

一种方法是使用您自己的自定义故障代码实现
UncaughtExceptionHandler
。安装处理程序的API如下所示:

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh);
这个班。作为一个非常基本的例子:

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!");
    }
}

一个完整的工作示例是。

一种方法是使用您自己的自定义故障代码实现一个
UncaughtExceptionHandler
。安装处理程序的API如下所示:

public static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh);
这个班。作为一个非常基本的例子:

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!");
    }
}
完整的工作示例是。

检查以下内容:

根据这一点,我使用了以下代码来处理异常。我想显示另一条消息,所以我添加了我自己的消息,但是如果您使用他的答案,则不会显示任何消息

    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);
        }
    });
选中此项:

根据这一点,我使用了以下代码来处理异常。我想显示另一条消息,所以我添加了我自己的消息,但是如果您使用他的答案,则不会显示任何消息

    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);
        }
    });