簡體   English   中英

密碼保護應用程序啟動

[英]Password protect app on start

嘿家伙我需要一種密碼保護我的應用程序的方式,所以當用戶點擊應用程序密碼actitivty首先出現,他們只能訪問應用程序,如果密碼正確。 它的一部分項目正在做,但我堅持這一點。 PLease家伙任何肝臟將不勝感激。

假設您有一個提交TextEdit字段的上下文的按鈕:

public class Password extends Activity implements OnClickListener
{
   ... other code

    public void onCreate(Bundle savedInstanceState)
    {
        ...other code
        Button sumbitButton = (Button) findViewById(R.id.submitbutton);
        submitButton.setOnClickListener(this);
    }

    public void onClick(View v)
    { 
        EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext);
        //if people are allowed to set the password on the first run uncomment the following and delete the uncommented section of this function
        //SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
        //String password = prefs.getString("password","");
        //if(password=="")
        //{
        //    SharedPreference.Editor edit = prefs.edit();
        //    edit.putString("password",passwordEditText.getText().ToString());
        //    StartMain();
        //}
        //else
        //{
        //    if(passwordEditText.getText().ToString()==password)
        //    {
        //         StartMain();
        //    }
        //}
        if(passwordEditText.getText().ToString()=="your app password") 
        {
            Intent intent = new Intent(this, your_other_activity.class);
            startActivity(intent);
        }
    }

    public void StartMain()
    {
         Intent intent = new Intent(this, your_other_activity.class);
         startActivity(intent);
    }

這需要在您的密碼活動布局中,您有一個名為passwordedittext的edittext和一個名為submitbutton的按鈕。

並且您有自己的主要活動(需要包含在清單文件中),您應該將其替換為your_other_activity.class。

這行錯誤:

if(passwordEditText.getText().ToString()=="your app password") 

它應該是

if (passwordEditText.getText().ToString().equals("your app password")

比較原始數據類型(如int,char,boolean)時,可以使用==,!=等。比較對象(如String,Car等)時,需要使用.equals()方法。

從密碼活動中獲取文本並將其存儲為SharedPreference。 然后,每次用戶啟動應用程序時,都會對您存儲的共享首選項執行檢查

暫無
暫無

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

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