赞
踩
本demo源码下载地址 App拉起机制源码下载
做智能售卖设备需要app一直在前台显示,但是有时由于一些不当操作或者系统原因会使app退出,导致设备不能使用。这时就需要拉起机制了。
拉起机制目前实现方式主要有2种。
创建一个类MyApp继承自Application类,在该类通过监听所有activity生命周期来判断activity是否全部销毁,以此来判断app是否退出。
代码如下:
/** * 监听各个Activity生命周期情况 */ private void startActivityLifeListener(){ registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { showLog("add activity "+activity.toString()); } @Override public void onActivityStarted(Activity activity) { mActivityCount++; showLog(activity.getComponentName()+" onActivityStarted activityCounts= "+mActivityCount); } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { showLog(activity.getComponentName().toString()+" onActivityPaused"); } @Override public void onActivityStopped(Activity activity) { mActivityCount--; showLog(activity.getComponentName()+" onActivityStopped activityCounts= "+mActivityCount); if (mActivityCount <= 0){ // 5秒后拉起 new Handler().postDelayed(new Runnable() { @Override public void run() { showLog("try to restart"); Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }, 5000); } } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { showLog("remove activity "+activity.toString()); } }); }
然后在onCreate方法里面调用startActivityLifeListener()方法。
通过ActivityManager类获取所有正在运行的进程,然后利用app包名找到应用的运行进程,再判断是否处于后台,不过该方法在有些系统版本可能不能用。
创建一个远程服务类PullUpService在后台一直运行轮询检查app是否后台运行。代码如下:
package com.mhwang.apppullup; import android.app.ActivityManager; import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.os.SystemClock; import android.util.Log; import java.util.List; /** 后台拉起服务 * Author : mhwang * Date : 2018/11/6 * Version : V1.0 */ public class PullUpService extends Service implements Runnable{ private boolean running = false; private Intent intent; private Thread thread; private static void showLog(String s){ Log.d("PullUpService=>", s); } public IBinder onBind(Intent intent) { return null; } public void onCreate(){ super.onCreate(); intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); if (intent != null) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } showLog("onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { showLog("onStartCommand"); if(thread == null){ thread = new Thread(this); thread.start(); } return START_STICKY; } @Override public void onDestroy() { showLog("onDestroy"); running = false; if(thread != null && !thread.isInterrupted()){ try { thread.interrupt(); thread.join(100); } catch (InterruptedException e) { e.printStackTrace(); } } thread = null; super.onDestroy(); } @Override public void run() { running = true; showLog("background service start"); while(running){ showLog("check background"); if(isBackground(PullUpService.this)){ showLog("restart activity"); startActivity(intent); } SystemClock.sleep(10 * 1000); // 由于模拟器较卡,这里提高10秒检查一次 } } /** 判断是否处于后台 * @param context * @return true:处于后台, false:不处于后台 */ public static boolean isBackground(Context context) { ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager .getRunningAppProcesses(); if(appProcesses == null){ return false; } for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { return true; } else { return false; } } } return false; } }
记得在AndroidMainifest.xml注册该服务。
<service android:name=".PullUpService" android:process=":remote"/>
然后在MyApp类开启服务:
如下图所示,使用back键和home键退出app,每隔5秒后都能有效拉起(图示效果运行在模拟器系统版本4.4.2)。但是有个缺点就是使用多任务键杀死时,由于此时整个app都被干掉了,因此MyApp里面的拉起机制不能生效。
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。