之前写过一遍Android中怎么判断是Home键或非正常退出(进入后台),这里讲的方法觉得有点low,在API 14+ (Android 4.0+)之后提供了Application.ActivityLifecycleCallbacks这个类,Application通过ActivityLifecycleCallbacks使用接口提供了一套回调方法,用于让开发者对Activity的生命周期事件进行集中处理。 ActivityLifecycleCallbacks接口回调可以简化监测Activity的生命周期事件,在一个类中作统一处理。继承这个类在
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
这两个方法里进行处理,便可以进行监听切到后台或切回前台。
ForegroundCallbacks.java代码实现。
package com.aoaoyi.commonutil.foreground; import android.app.Activity; import android.app.Application; import android.content.Context; import android.os.Bundle; import android.os.Handler; import com.aoaoyi.commonutil.MLog; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; /** * Created by yuzhenbei on 2017/4/27. */ public class ForegroundCallbacks implements Application.ActivityLifecycleCallbacks { public static final String TAG = ForegroundCallbacks.class.getName(); public static final long CHECK_DELAY = 500; private volatile static ForegroundCallbacks mInstance; private boolean mForeground = false, mPaused = true; private Handler mHandler = new Handler(); private List<Listener> mListeners = new CopyOnWriteArrayList<>(); private Runnable mCheck; public static ForegroundCallbacks getInstance(Application pApplication){ if (mInstance == null) { synchronized(ForegroundCallbacks.class){ if (mInstance == null) { mInstance = new ForegroundCallbacks(); register(pApplication); } } } return mInstance; } public static ForegroundCallbacks getInstance(Context pContext){ if (mInstance == null) { Context appCtx = pContext.getApplicationContext(); if (appCtx instanceof Application) { getInstance((Application)appCtx); } throw new IllegalStateException( "Foreground is not initialised and " + "cannot obtain the Application object"); } return mInstance; } public boolean isForeground(){ return mForeground; } public boolean isBackground(){ return !mForeground; } public void addListener(Listener listener){ mListeners.add(listener); } public void removeListener(Listener listener){ mListeners.remove(listener); } private static void register(Application pApplication){ if (null == pApplication){ return; } if (null == mInstance) { return; } pApplication.registerActivityLifecycleCallbacks(mInstance); } public void unregister(Application pApplication){ if (null == pApplication){ return; } if (null == mInstance) { return; } try { mListeners.clear(); pApplication.unregisterActivityLifecycleCallbacks(mInstance); } catch (Exception e) { e.printStackTrace(); } } @Override public void onActivityResumed(Activity activity) { mPaused = false; boolean wasBackground = !mForeground; mForeground = true; if (mCheck != null) mHandler.removeCallbacks(mCheck); if (wasBackground){ MLog.d ("went Foreground"); for (Listener listener : mListeners) { try { listener.onBecameForeground(); } catch (Exception exc) { MLog.d ("Listener threw exception!:"+exc.toString()); } } } else { MLog.d ("still Foreground"); } } @Override public void onActivityPaused(Activity activity) { mPaused = true; if (mCheck != null) mHandler.removeCallbacks(mCheck); mHandler.postDelayed(mCheck = new Runnable(){ @Override public void run() { if (mForeground && mPaused) { mForeground = false; MLog.d ("went background"); for (Listener listener : mListeners) { try { listener.onBecameBackground(); } catch (Exception exc) { MLog.d ("Listener threw exception!:"+exc.toString()); } } } else { MLog.d ("still Foreground"); } } }, CHECK_DELAY); } @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {} @Override public void onActivityStarted(Activity activity) {} @Override public void onActivityStopped(Activity activity) {} @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) {} @Override public void onActivityDestroyed(Activity activity) {} public interface Listener { void onBecameForeground(); void onBecameBackground(); } }
在Application 里进行使用
package com.aoaoyi.testcb; import android.support.multidex.MultiDex; import android.support.multidex.MultiDexApplication; import com.aoaoyi.commonutil.MLog; import com.aoaoyi.commonutil.foreground.ForegroundCallbacks; /** * Created by yuzhenbei on 2017/4/20. */ public class CBApplication extends MultiDexApplication { private static final String TAG = CBApplication.class.getSimpleName(); @Override public void onCreate() { MultiDex.install(getApplicationContext()); super.onCreate(); //监听APP进入后台或切回前台 addAppForegroundListener(); } @Override public void onLowMemory() { MLog.d(TAG, "LowMemory"); //System.gc(); super.onLowMemory(); } @Override public void onTerminate() { MLog.d(TAG, "Terminate"); ForegroundCallbacks.getInstance(this).unregister(this); super.onTerminate(); } /** * 监听APP进入后台或切回前台 */ private void addAppForegroundListener(){ ForegroundCallbacks.getInstance(this).addListener(new ForegroundCallbacks.Listener() { @Override public void onBecameForeground() { //切回前台 MLog.d(TAG, "BecameForeground"); } @Override public void onBecameBackground() { //切到后台 MLog.d(TAG, "BecameBackground"); } }); } }
是不是比Android中怎么判断是Home键或非正常退出(进入后台)的方法高大上?
文章评论
闲着没事,随便逛逛,心静自然凉。
受教了!呵呵!
古人日三省其身,我从博客里吸收养分!
I'm really impressed together with your writing abilities and also with the
layout for your weblog. Is this a paid subject or did you modify it your self?
Either way stay up the nice quality writing, it's uncommon to peer
a nice weblog like this one nowadays..