一、
public class ActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks`在这里插入代码片` {
private int mActiveActivityCount;// 当前活动Activity数量
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
AppManager.getInstance().add(activity);
}
@Override
public void onActivityStarted(Activity activity) {
if (mActiveActivityCount == 0) {
// 进入前台
for (AppLifecycleCallback callback : AppManager.getInstance().getAppLifecycleCallbacks()) {
callback.onAppEnterForeground();
}
}
mActiveActivityCount++;
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
mActiveActivityCount--;
if (mActiveActivityCount == 0) {
// 进入后台
for (AppLifecycleCallback callback : AppManager.getInstance().getAppLifecycleCallbacks()) {
callback.onAppEnterBackground();
}
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
AppManager.getInstance().remove(activity);
}
}
二、
/**
* 应用前后台切换时回调
*/
public interface AppLifecycleCallback {
void onAppEnterForeground();
void onAppEnterBackground();
}
三、
public class AppManager {
private static AppManager ourInstance = new AppManager();
public static AppManager getInstance() {
return ourInstance;
}
private AppManager() {
}
private Context mContext;
private Map<String, Activity> mActivityMap = new HashMap<>();
private Activity mCurrentActivity;
private List<AppLifecycleCallback> mAppLifecycleCallbacks = new ArrayList<>();
/**
* Application的onCreate()里初始化
*
* @param context 上下文
*/
public void init(Context context) {
Application application = (Application) context.getApplicationContext();
application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
super.onActivityCreated(activity, savedInstanceState);
mCurrentActivity = activity;
}
@Override
public void onActivityResumed(Activity activity) {
super.onActivityResumed(activity);
mCurrentActivity = activity;
}
@Override
public void onActivityStopped(Activity activity) {
super.onActivityStopped(activity);
if (activity == mCurrentActivity) {
mCurrentActivity = null;
}
}
@Override
public void onActivityDestroyed(Activity activity) {
super.onActivityDestroyed(activity);
if (activity == mCurrentActivity) {
mCurrentActivity = null;
}
}
});
mContext = application;
}
public Activity getCurrentActivity() {
return mCurrentActivity;
}
/**
* @param callback 应用前后台切换时回调
*/
public void addAppLifecycleCallback(AppLifecycleCallback callback) {
mAppLifecycleCallbacks.add(callback);
}
/**
* @param callback 应用前后台切换时回调
*/
public void removeAppLifecycleCallback(AppLifecycleCallback callback) {
mAppLifecycleCallbacks.remove(callback);
}
/**
* 退出应用
*/
public void exitApp() {
AppManager.getInstance().removeAll();
System.exit(0);
}
/**
* 重启应用
*
* @param delay 重启延时
*/
public void restartApp(long delay) {
Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent);
//退出程序
exitApp();
}
void add(Activity activity) {
mActivityMap.put(activity.toString(), activity);
}
void remove(Activity activity) {
mActivityMap.remove(activity.toString());
}
private void removeAll() {
for (Entry<String, Activity> entry : mActivityMap.entrySet()) {
entry.getValue().finish();
}
}
List<AppLifecycleCallback> getAppLifecycleCallbacks() {
return mAppLifecycleCallbacks;
}
}