Android中监听应用进入后台
2018-11-08 本文已影响0人
monkey_who
最近看到好几个银行类app,应用切出去之后会提示“XXX进入后台运行”
API 14之后,Application可以注册ActivityLifecycleCallbacks这个接口
application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
//说明从后台回到了前台
mFinalCount++;
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
mFinalCount--;
//说明从前台回到了后台
if (mFinalCount == 0 ) {
ToastTools.showShort(application.getApplicationContext(), "进入后台运行");
}
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
});