知道什么时候该展现了一通code锁 [英] Know when to show a passcode lock

查看:94
本文介绍了知道什么时候该展现了一通code锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要显示,每当用户离开应用程序,回来一通code屏幕的应用程序(无论是通过屏幕锁定,或者返回到主屏幕通过背部或home键) 。我是有工作用下面的:

I am developing an application that needs to display a passcode screen whenever a user leaves the app and comes back (be it through a screen lock, or going back to the home screen through the back or home button). I had it working using the following:

起始活动将要求在启动时通code检查,每项活动增加了以下功能到他们的onPause方法:

The starting activity would call for the passcode check on startup, and each activity added the following functionality to their onPause method:

@Override
public void onPause() {
    super.onPause();

    if (!isFinishing()) {
    new PasscodeCheckTask(this.getApplicationContext(),this).execute();
    }
}

在PassocdeCheckTask看起来像下面这样。它检查以查看是否屏幕关闭或该应用不再在背景

The PassocdeCheckTask looks like the following. It checks to see if the screen is off or the app is no longer in the background

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;
    }
}

本通code活动将结束时完成,并且调用活动将moveTaskToBackground(真)通行证code没有通过。该系统的工作精美,直到我试图在一个宏达电Evo与mikg ROM。出于某种原因,appProcess.importance一直没有露面的IMPORTANCE_FOREGROUND。它总是IMPORTANCE_BACKGROUND。因此,通code总是被提出来,即使该应用程序从未走进后台。

The Passcode activity would finish when done, and the calling activity would moveTaskToBackground(true) if the passcode didn't pass. This system worked beautifully until I tried it on an HTC Evo with mikg ROM. For some reason, the appProcess.importance never showed up as IMPORTANCE_FOREGROUND. It was always IMPORTANCE_BACKGROUND. Thus, the passcode would ALWAYS be brought up, even though the app never went into the background.

我试过的DropBox上的电话(其中有一个通code锁定为好),和它的工作精美。我似乎无法找到一个不同的方式来知道什么时候一个应用程序已经站到了后台,或者如果它被带到从背景回来。如何使这项工作任何想法?

I tried DropBox on that phone (which has a passcode lock as well), and it worked beautifully. I can't seem to find a different way to know when an app has gone to the background, or if it is being brought back from the background. Any ideas on how to make this work?

推荐答案

的onStop()每项活动的,请用你离开活动时的静态数据成员。在 ONSTART()每项活动的,检查的时间,如果超过一定超时阈值,显示您的身份验证活动。允许用户设置的超时值,这样,如果他们不希望每几秒被打扰,它们可以控制

In onStop() of each activity, update a static data member with the time you left the activity. In onStart() of each activity, check that time, and if it exceeds some timeout threshold, display your authentication activity. Allow the user to set the timeout value, so that if they don't want to be bothered every few seconds, they can control that.

这篇关于知道什么时候该展现了一通code锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆