• Android App退出检测


    app的退出检测是很难的,但是获取app“要退出”的状态就容易多了,退出的瞬间并不是真的退出了,ActivityManager要销毁activity,也需要一些时间和资源的。 
    先见下面的运行效果: 
    这里写图片描述

    gif做的比价粗啊, 
    两个activity的界面就不介绍了,主要是在APP启动的时候开启一个服务,application代码如下:

    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            Intent intent=new Intent(this,CheckExitService.class);
            getApplicationContext().startService(intent);
        }
    }

    service的代码如下:

    public class CheckExitService extends Service {
    
        private String packageName = "test.minwenping.com.appexitdemo";
    
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            super.onTaskRemoved(rootIntent);
            Toast.makeText(CheckExitService.this, "App要退出了", Toast.LENGTH_SHORT).show();
        }
    
        //service异常停止的回调
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            ActivityManager activtyManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activtyManager.getRunningAppProcesses();
            for (int i = 0; i < runningAppProcesses.size(); i++) {
                if (packageName.equals(runningAppProcesses.get(i).processName)) {
                    Toast.makeText(this, "app还在运行中", Toast.LENGTH_LONG).show();
                }
            }
            return START_NOT_STICKY;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Toast.makeText(CheckExitService.this, "App检测服务开启了", Toast.LENGTH_SHORT).show();
        }
    }

    还有尝试了守护线程,UI就只有一个线程,想从这方面下手,但是都失败了

  • 相关阅读:
    【Beta】Scrum Meeting 8
    【Beta】Scrum Meeting 7
    【Beta】Scrum Meeting 6
    【Beta】Scrum Meeting 5
    【Beta】Scrum Meeting 4
    【Beta】Scrum Meeting 3
    “北航Clubs”项目汇报
    Alpha阶段展示报告
    Alpha阶段产品功能说明
    Alpha阶段个人贡献分
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/7560690.html
Copyright © 2020-2023  润新知