密码保护推出Android应用 [英] Password protect launch of android application

查看:92
本文介绍了密码保护推出Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式来密码保护我的Andr​​oid应用程序的启动,即启动时/恢复属于我的APK包密码对话框的活动将被显示。

I'm searching for a way to password protect my android application on launch, i.e. when launching/resuming an activity belonging to my apk-package a password dialog will be shown.

我已经尝试了一些办法,这(扩大应用程序类等),但没有一个似乎工作。要么他们没有在UI线程上运行或对话框未在每次启动/恢复场合表示。

I've tried some approaches to this (extending application class and so on) but none seems to work. Either they don't run on the UI thread or the dialog isn't shown on every launch/resume occasion.

//米

推荐答案

所以这是我坚持的解决方案。在我的应用I类存储一个长变量与系统时间时,一个活动的最后暂停。

So this is the solution I stuck with. In my Application class i store a long variable with the system time when an activity was last paused.

import android.app.Application;
public class MyApplication extends Application {
    public long mLastPause;

    @Override
    public void onCreate() {
        super.onCreate();
        mLastPause = 0;
        Log.w("Application","Launch");
    }
}

在每的onPause-方法我更新此值到当前时间

In every onPause-method I update this value to the current time.

@Override
public void onPause() {
    super.onPause();
    ((MyApplication)this.getApplication()).mLastPause = System.currentTimeMillis();
}

和在每一个onResume我把它比作当前时间。如果一定量的时间(当前5秒)已通过示出了密码提示。

And in every onResume I compare it to the current time. If a certain amount of time (currently 5 seconds) has passed my password prompt is shown.

@Override
public void onResume() {
    super.onResume();
    MyApplication app = ((MyApplication)act.getApplication());
    if (System.currentTimeMillis() - app.mLastPause > 5000) {
        // If more than 5 seconds since last pause, prompt for password
    }
}

这篇关于密码保护推出Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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