我正在开发一个应用程序,需要显示一个密码屏幕,每当用户离开应用程序和回来(无论是通过屏幕锁定,或返回到主屏幕通过后退或主页按钮)。我使用下面的代码让它正常工作:
启动活动将在启动时调用密码检查,并且每个活动都将以下功能添加到其onPause方法中:
@Override
public void onPause() {
super.onPause();
if (!isFinishing()) {
new PasscodeCheckTask(this.getApplicationContext(),this).execute();
}
}PassocdeCheckTask如下所示。它会检查屏幕是否关闭或应用程序是否不再处于后台
public class PasscodeCheckTask extends AsyncTask<Void, Void, Boolean> {
public static final int CHECK_PASSCODE = 0;
private Context mActivityApplicationContext;
private Context mActivityContext;
public PasscodeCheckTask(Context applicationContext, Context activityContext){
mActivityApplicationContext = applicationContext;
mActivityContext = activityContext;
}
@Override
protected Boolean doInBackground(Void... params) {
Boolean result = false;
if (!((PowerManager)mActivityApplicationContext.getSystemService(android.content.Context.POWER_SERVICE)).isScreenOn() ||
!isAppOnForeground(mActivityApplicationContext)) {
result = true;
}
return result;
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
// Start passcode activity to check for passcode
/* CODE HERE */
((Activity)mActivityContext).startActivityForResult(intent, CHECK_PASSCODE);
}
}
protected boolean isAppOnForeground(final Context context) {
List<RunningAppProcessInfo> appProcesses = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) &&
appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}密码活动将在完成时结束,如果密码未通过,则调用活动将为moveTaskToBackground(true)。这个系统运行得很好,直到我在HTC Evo上用mikg ROM进行了试用。由于某些原因,appProcess.importance从未以IMPORTANCE_FOREGROUND的身份出现。一直都是IMPORTANCE_BACKGROUND。因此,密码总是会被调出,即使应用程序从未进入后台。
我在那部手机上试过DropBox (它也有密码锁),它工作得很好。我似乎找不到一种不同的方法来知道应用程序何时被转到后台,或者它是否被从后台带回来。有没有关于如何让它工作的想法?
发布于 2011-11-24 09:11:23
在每个活动的onStop()中,使用您离开该活动的时间更新静态数据成员。在每个活动onStart()中,检查时间,如果超过某个超时阈值,则显示您的身份验证活动。允许用户设置超时值,这样如果他们不想每隔几秒就被打扰一次,他们就可以控制它。
发布于 2013-05-31 18:12:33
我喜欢基于时间的方法,我已经努力了一段时间让它以一种很好的方式工作。基于时间的方法效果很好。为了便于使用,我创建了一个类。
public class PinCodeCheck {
private static long INIT_TIME = 0;
private static PinCodeCheck ref = null;
private static SharedPreferences values = null;
private PinCodeCheck(Context context){
values = context.getSharedPreferences("com.example.xxx", 0); //use your preferences file name key!
}//end constructor
public static synchronized PinCodeCheck getInstance(Context context) {
if (ref == null){
ref = new PinCodeCheck(context);
}
return ref;
}//end method
public void init(){
PinCodeCheck.INIT_TIME = System.currentTimeMillis();
}//end method
public void forceLock(){
PinCodeCheck.INIT_TIME = 0;
}//end method
public boolean isLocked(){
long currentTime = System.currentTimeMillis();
long threshold = values.getLong(Keys.PASSWORD_PROTECT_TIMEOUT, 30000); // check here, might change in between calls
if (currentTime - PinCodeCheck.INIT_TIME > threshold){
return true;
}
return false;
}//end method
}//end class使用
private static PinCodeCheck check = PinCodeCheck.getInstance(context);
@Override
public void onResume() {
super.onResume();
if (check.isLocked()) {
showDialog();
}
}//end method
@Override
public void onPause() {
super.onPause();
check.init();
}//end method https://stackoverflow.com/questions/8249651
复制相似问题