Android 进入后台触发的事件,一般用于APP屏幕解锁

啰嗦个鸟啊,直接上代码

BaseActivity,所有类的父类

UnLockViewActivity,屏幕解锁类,但是这个类不继承

BaseActivity


BaseActivity覆写onStop方法

@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		if (!isAppOnForeground()) {
			// app 进入后台
			// 全局变量isActive = false 记录当前已经进入后台
			if (StaticFunction.isSystemHavePassword(BaseActivity.this)) {
				Intent intent = new Intent(BaseActivity.this,
						UnLockViewActivity.class);
				startActivity(intent);
			}
			

		}
	}

	/**
	 * 程序是否在前台运行
	 * 
	 * @return
	 */
	public boolean isAppOnForeground() {
		// Returns a list of application processes that are running on the
		// device

		ActivityManager activityManager = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();

		List appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;

		for (RunningAppProcessInfo appProcess : appProcesses) {
			// The name of the process that this object is associated with.
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}

		return false;
	}




你可能感兴趣的:(Android)