android 监听app进入后台以及从后台进入前台

时间:2021-02-22 16:11:29
package com.pinshang.base;

import com.pinshang.common.CommonValue;
import com.pinshang.investapp.EnterGesturePasswordActivity;
import com.pinshang.investapp.MainApp;
import com.pinshang.utils.AbAppUtil;
import com.umeng.socialize.utils.Log; import android.content.Intent;
import android.support.v4.app.FragmentActivity; /**
* @author hjzhang
*/
public class BaseFragmentActivity extends FragmentActivity {
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if (AbAppUtil.isBackground(this)) {
// app 进入后台
CommonValue.isActive = true;
CommonValue.backTime = System.currentTimeMillis();
Log.d("haijiang", "-------------进入后台------->");
} else {
CommonValue.isActive = false;
CommonValue.backTime = 0;
}
} @Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (CommonValue.isActive) {
Log.d("haijiang", "------------- 从后台唤醒,进入前台------->");
if (System.currentTimeMillis() - CommonValue.backTime > 30 * 1000) {
if (MainApp.theApp.userId > 0
&& MainApp.theApp.mLoginUtils.getOpen()) {// 登录且开启手势密码
if (MainApp.theApp.mLockPatternUtils.hasPattern()) {
Intent intent = new Intent(BaseFragmentActivity.this,
EnterGesturePasswordActivity.class);
intent.putExtra("background", "background");
startActivity(intent);
overridePendingTransition(0, 0);
}
}
CommonValue.isActive = false;
CommonValue.backTime = 0;
}
}
}
}