简体   繁体   English

使用密码保护应用程序

[英]Protect application with password

I've got an Application (extends), and it runs some service in the background to monitor beacons.我有一个应用程序(扩展),它在后台运行一些服务来监控信标。 Checking if you're in or out of range and then preform some actions.检查您是否在范围内,然后执行一些操作。

I only got one Activity in my Application and that is the Settings Activity.我的应用程序中只有一个活动,那就是设置活动。 The service automatically starts after boot, and runs indefinitely.该服务在启动后自动启动,并无限期运行。 When a person tries to open the app, the SettingsActivity should not become available unless a correct password gets provided.当一个人尝试打开应用程序时,除非提供了正确的密码,否则 SettingsActivity 不应变为可用。

So in the onPause() and onResume() I'd like to launch a Dialog that asks for a password.因此,在 onPause() 和 onResume() 中,我想启动一个要求输入密码的对话框。 When a correct password gets entered (Retrofit checks this and returns a boolean), he is allowed to the SettingsActivity.当输入正确的密码时(Retrofit 检查并返回一个布尔值),他被允许进入 SettingsActivity。

Problem: How do I make my activity invisible onPause() and how do I make it visible again after correct password (launch dialog in onResume()).问题:如何使我的活动在 onPause() 不可见,以及如何在输入正确密码后使其再次可见(onResume() 中的启动对话框)。

I could think of one solution and that is adding another View to my settings_view.xml, let it fill the parent and set it VISIBILITY.GONE after right password and VISIBILITY.VISIBLE @ onPause().我可以想到一个解决方案,那就是在我的 settings_view.xml 中添加另一个视图,让它填充父级并在正确的密码和 VISIBILITY.VISIBLE @ onPause() 之后将其设置为 VISIBILITY.GONE。 I want to do this in onPause() because then the person cannot "see" anything when going to the recent app screen.我想在 onPause() 中执行此操作,因为这样该人在转到最近的应用程序屏幕时将无法“看到”任何内容。 But this seems an extremely sloppy solution.但这似乎是一个非常草率的解决方案。

Is there anyway I can just blackout/tint the layout in onPause() and remove the blackout/tint after the right password is entered?无论如何,我可以在 onPause() 中对布局进行遮光/着色并在输入正确的密码后删除遮光/着色吗?

Thanks for the advice :)感谢您的建议 :)

Problem: How do I make my activity invisible onPause() and how do I make it visible again after correct password (launch dialog in onResume()).问题:如何使我的活动在 onPause() 不可见,以及如何在输入正确密码后使其再次可见(onResume() 中的启动对话框)。

You should consider the rest of your activity lifecycle for this.为此,您应该考虑活动生命周期的其余部分。 If you are getting some info from the internet, then lots of problems may take time in your application.如果您从 Internet 获取一些信息,那么在您的应用程序中可能会遇到很多问题。 You may have bad signal, no network available, or even the network connection adapter is off.您可能信号不好,没有可用的网络,甚至网络连接适配器已关闭。

Consider this instead: Activity "login" onCreate : Create a bland activity with the required files, and then a submit button.考虑一下: Activity "login" onCreate :使用所需的文件创建一个平淡的活动,然后是一个提交按钮。 Once submitted, query the network status and whatever you are using for login.提交后,查询网络状态以及您用于登录的任何内容。 Then save the result for a while (lets say a day, or 30 minutes, whatever feels best for your application), usually a SharedPreferences will do.然后将结果保存一段时间(可以说一天或 30 分钟,只要对您的应用程序感觉最好),通常SharedPreferences就可以了。

Activity "SettingsActivity" onResume : check if saved data allows user to see the Activity. Activity "SettingsActivity" onResume :检查保存的数据是否允许用户查看该活动。 If it doesnt, finish the activity, and start a "login" one.如果没有,请完成活动,并开始“登录”活动。

I solved this question as follows:我解决了这个问题如下:

I included a layout:我包括了一个布局:

File activity_settings.xml文件activity_settings.xml

<include layout="@layout/view_authentication"
    android:id="@+id/authenticationView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

File view_authentication.xml文件 view_authentication.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/authenticationView"
            android:clipToPadding="false">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:padding="10dp">

    <EditText
        android:id="@+id/authentication_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:hint="Password"
        android:password="true"/>

    <Button
        android:id="@+id/authentication_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="@string/authentication_button"/>

</LinearLayout>
</RelativeLayout>

In my SettingsActivity.java I added following line in onCreate().在我的 SettingsActivity.java 中,我在 onCreate() 中添加了以下行。 This prevents screenshots being taken and blocks seeing anything in recent apps这可以防止截取屏幕截图并阻止在最近的应用程序中看到任何内容

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

In onResume(), which gets called before opening the app for the first time, or re-opening it.在 onResume() 中,它在第一次打开应用程序或重新打开它之前被调用。 I call following method:我调用以下方法:

public void showAuthenticationFrame() {
    authenticationView.setVisibility(View.VISIBLE);
    contentFrame.setVisibility(View.GONE);
}

When the correct password is entered, I do the opposite, I set authenticationView to GONE and contentFrame to VISIBLE.当输入正确的密码时,我做相反的事情,我将 authenticationView 设置为 GONE,将 contentFrame 设置为 VISIBLE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM