2012-02-19 93 views
8

我已經構建了我的第一個應用程序,我想密碼保護它。將密碼存儲在Java文件中並且該方法需要儘可能簡單,因爲我在這個應用程序之前沒有java甚至xml的經驗。我有過幾次嘗試,但都失敗了,所以我希望有人能幫助我。密碼保護我的Android應用程序(簡單的方式)

我曾與一個EditText字段中創建的佈局:

<EditText 
android:id="@+id/passwordedittext" 
android:layout_width="200dp" 
android:layout_height="50dp" 
android:inputType="textPassword" 
android:layout_marginTop="40dp" 
android:layout_marginLeft="20dp"> 
<requestFocus /> 

和一個提交按鈕:

<Button 
android:id="@+id/submitbutton" 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:layout_marginTop="40dp" 
android:background="@drawable/bgo" 
android:clickable="true" 
android:layout_gravity="right|center_horizontal" 
android:layout_marginRight="20dp"/> 

Java文件:

package com.berry; 
import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 


public class password extends Activity{ 

MediaPlayer mpbuttonclick; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

    setContentView(R.layout.password); 

    mpbuttonclick = MediaPlayer.create(this, R.raw.keypress); 

    Button sumbitButton = (Button) findViewById(R.id.submitbutton); 
    sumbitButton.setOnClickListener(new View.OnClickListener() {   
     public void onClick(View v){ 
     EditText passwordEditText = (EditText) findViewById(R.id.passwordedittext); 
        if(passwordEditText.getText().toString()=="MyPasswordHere"){ 
         startActivity(new Intent("com.berry.intro")); 
         mpbuttonclick.start(); 


        }}}); 
    }} 

回答

12

這部分:

if(passwordEditText.getText().toString()=="MyPasswordHere") 

不正確。它應該是

if(passwordEditText.getText().toString().equals("MyPasswordHere")) 

當比較基本數據類型(如intcharboolean),可以使用==!=
當比較對象(如StringCar等),你需要使用.equals()方法。

See also this page.

+0

謝謝你做的竅門! – SuperKombol 2012-02-19 17:32:15

-6

在編輯文本字段XML可以添加

android:password="true" 
+1

使用此標誌將通過使編輯文本具有密碼點而不是顯示實際字符來改進您的應用程序,但此標誌不會解決您遇到的問題。 – 2012-02-19 16:21:48

+0

編輯文本框無論如何都顯示點,因爲inputtype已被定義爲xml中的textpassword。謝謝。 – SuperKombol 2012-02-20 17:20:36

9

這是沒有辦法的安全檢查密碼這樣。

有幾種方法可以輕鬆地繞過你的代碼

  1. 直接從其他應用程序

  2. 調用活動讀拆卸smali code找回密碼

  3. 修改代碼使用smali總是跳入代碼塊

  4. 可用來解決這些問題個

解決方案:

  1. 暗紋代碼(最壞的選擇,但可能足以在大多數情況下)

  2. Hashed Password比較:安全得多。但應該是一個鹽漬哈希。 (There is also a more simple to understand explaination for the implementation)

  3. Use a HTTP Request到您的服務器以隱藏密碼檢查背後的機制。(但這需要你的應用程序要求網絡權限)

+1

嗯,因爲Java中的提問者很弱,我會推薦解決方案2,它可以像這樣實現:'SHA1.Sha1Hash(passwordGoesHere);' – 2012-02-19 16:25:34

+1

感謝您的提示,我添加了一個鏈接來解決這個問題。 – devsnd 2012-02-19 16:37:54

+0

感謝您的建議,我會研究您的解決方案,以便在構建更實質性的東西時獲得。 – SuperKombol 2012-02-19 17:36:49