簡體   English   中英

使用密碼保護應用程序

[英]Protect application with password

我有一個應用程序(擴展),它在后台運行一些服務來監控信標。 檢查您是否在范圍內,然后執行一些操作。

我的應用程序中只有一個活動,那就是設置活動。 該服務在啟動后自動啟動,並無限期運行。 當一個人嘗試打開應用程序時,除非提供了正確的密碼,否則 SettingsActivity 不應變為可用。

因此,在 onPause() 和 onResume() 中,我想啟動一個要求輸入密碼的對話框。 當輸入正確的密碼時(Retrofit 檢查並返回一個布爾值),他被允許進入 SettingsActivity。

問題:如何使我的活動在 onPause() 不可見,以及如何在輸入正確密碼后使其再次可見(onResume() 中的啟動對話框)。

我可以想到一個解決方案,那就是在我的 settings_view.xml 中添加另一個視圖,讓它填充父級並在正確的密碼和 VISIBILITY.VISIBLE @ onPause() 之后將其設置為 VISIBILITY.GONE。 我想在 onPause() 中執行此操作,因為這樣該人在轉到最近的應用程序屏幕時將無法“看到”任何內容。 但這似乎是一個非常草率的解決方案。

無論如何,我可以在 onPause() 中對布局進行遮光/着色並在輸入正確的密碼后刪除遮光/着色嗎?

感謝您的建議 :)

問題:如何使我的活動在 onPause() 不可見,以及如何在輸入正確密碼后使其再次可見(onResume() 中的啟動對話框)。

為此,您應該考慮活動生命周期的其余部分。 如果您從 Internet 獲取一些信息,那么在您的應用程序中可能會遇到很多問題。 您可能信號不好,沒有可用的網絡,甚至網絡連接適配器已關閉。

考慮一下: Activity "login" onCreate :使用所需的文件創建一個平淡的活動,然后是一個提交按鈕。 提交后,查詢網絡狀態以及您用於登錄的任何內容。 然后將結果保存一段時間(可以說一天或 30 分鍾,只要對您的應用程序感覺最好),通常SharedPreferences就可以了。

Activity "SettingsActivity" onResume :檢查保存的數據是否允許用戶查看該活動。 如果沒有,請完成活動,並開始“登錄”活動。

我解決了這個問題如下:

我包括了一個布局:

文件activity_settings.xml

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

文件 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>

在我的 SettingsActivity.java 中,我在 onCreate() 中添加了以下行。 這可以防止截取屏幕截圖並阻止在最近的應用程序中看到任何內容

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

在 onResume() 中,它在第一次打開應用程序或重新打開它之前被調用。 我調用以下方法:

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

當輸入正確的密碼時,我做相反的事情,我將 authenticationView 設置為 GONE,將 contentFrame 設置為 VISIBLE。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM