Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解锁手机时显示应用程序锁定模式对话框_Java_Android - Fatal编程技术网

Java 解锁手机时显示应用程序锁定模式对话框

Java 解锁手机时显示应用程序锁定模式对话框,java,android,Java,Android,我有MainActivity,在其onResume方法中,我调用模式锁来创建和确认用户身份。在应用程序上处于活动状态时,以及手机处于睡眠模式且用户解锁时,用户来回访问并离开此主要活动。这两种方案都将调用onRestart、onStart和onResume方法,但我只想撤销解锁方案中的模式 handlePattern方法需要调用适当的区分 当我调用handlePattern方法时,如何区分这一点 主课 onCreate(){} onResume(){ //help needed to kn

我有MainActivity,在其onResume方法中,我调用模式锁来创建和确认用户身份。在应用程序上处于活动状态时,以及手机处于睡眠模式且用户解锁时,用户来回访问并离开此主要活动。这两种方案都将调用onRestart、onStart和onResume方法,但我只想撤销解锁方案中的模式

handlePattern方法需要调用适当的区分

当我调用handlePattern方法时,如何区分这一点

主课

onCreate(){}

onResume(){
   //help needed to know that user is just visiting activity in app back and forth
     or came back after unlocking the screen.
   if(isPatternCallRequired){
        handlePattern()
   }
}

在onStop方法调用中,您可以检查播放器是否处于睡眠模式并缓存布尔值

PowerManager pm = (PowerManager) 
_context.getSystemService(Context.POWER_SERVICE);
boolean isInSleepMode = !pm.isScreenOn();
检查生成版本

if( Build.VERSION.SDK_INT >= 20)
    // use isInteractive()
else
    // use isScreenOn()
在onRestart中,当您从睡眠中恢复时将调用它-根据缓存的值,您可以显示要解锁的模式

一旦使用完缓存值,您可能需要重置它

onResume可能不是调用的正确API,因为即使在活动加载时也会调用它

根据您的评论编辑答案

您也可以这样尝试ActivityLifecycleCallbacks

首先,在应用程序类中注册应用程序

public class StackApp extends Application {

    private static final String TAG = StackApp.class.getSimpleName();

    public static final String INTENT_ACTION_APP_STATE_CHANGE = "intent_action_app_state_change";
    public static final String INTENT_DATA_IS_IN_BACKGROUND = "intent_data_is_in_background";

    private static int mNumRunningActivities = 0;

    private static AtomicBoolean mIsAppInForeground = new AtomicBoolean();

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

        if (Build.VERSION.SDK_INT >= 14) {

            // registerActivityLifecycleCallbacks is supported only from the SDK version 14.
            registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
                @Override
                public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

                }

                @Override
                public void onActivityStarted(Activity activity) {
                    mNumRunningActivities++;

                    if (mNumRunningActivities == 1) {
                        notifyAppState(false);

                        Log.i(TAG, "APP IN FOREGROUND");
                    }
                }

                @Override
                public void onActivityResumed(Activity activity) {

                }

                @Override
                public void onActivityPaused(Activity activity) {

                }

                @Override
                public void onActivityStopped(Activity activity) {
                    mNumRunningActivities--;

                    if (mNumRunningActivities == 0) {

                        notifyAppState(true);


                    }
                }

                @Override
                public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

                }

                @Override
                public void onActivityDestroyed(Activity activity) {

                }
            });
        }
    }

    /**
     * To notify App state whether its in ForeGround or in Background
     *
     * @param isInBackground
     */
    private void notifyAppState(boolean isInBackground) {
        if (isInBackground) {
            mIsAppInForeground.set(false);
        } else {
            mIsAppInForeground.set(true);
        }

        sendAppStateChangeBroadcast(isInBackground);
    }

    public static boolean isInForeground() {
        return mIsAppInForeground.get();
    }

    private void sendAppStateChangeBroadcast(boolean isInBackground) {

        Log.i(TAG, "sendAppStateChangeBroadcast - isInBackground : " + isInBackground);

        Intent intent = new Intent();
        intent.setAction(INTENT_ACTION_APP_STATE_CHANGE);

        intent.putExtra(INTENT_DATA_IS_IN_BACKGROUND, isInBackground);

        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

}
并注册广播,然后收听应用程序是在后台运行还是在前台运行,如下面的示例活动示例所示

public class SampleMyActivity extends AppCompatActivity {

    private OnAppStateReceiver mAppStateReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_my);

        mAppStateReceiver = new OnAppStateReceiver();

        IntentFilter filter = new IntentFilter(StackApp.INTENT_ACTION_APP_STATE_CHANGE);
        LocalBroadcastManager.getInstance(this).registerReceiver(mAppStateReceiver, filter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mAppStateReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mAppStateReceiver);
        }
    }

    private class OnAppStateReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (!TextUtils.isEmpty(action) && StackApp.INTENT_ACTION_APP_STATE_CHANGE.equalsIgnoreCase(action)) {
                boolean isGoingBackground = intent.getBooleanExtra(StackApp.INTENT_DATA_IS_IN_BACKGROUND, false);

                if (isGoingBackground) {
                    //Your app is not vissible to the use
                } else {
                    // App is visible to the user.
                }
            }
        }
    }
}
注意:如果你想收听多个活动,你可以创建一个基地 类并在那里添加侦听器,您可以在 在这种情况下,您可以减少大量代码


隐马尔可夫模型。。我看到ISCreenon被贬值,其他方法如powerManager.isInteractive;powerManager.isDeviceIdleMode需要api级别20,我只能使用16。有更好的解决方案吗?在需要同时支持这两个版本的情况下,您可以随时检查构建版本。在您的目标版本大于20之前,这两个api都可以正常工作。有关于为什么否决的指导吗??