密码保护我的Android应用程序(简单方式)

时间:2012-02-19 15:05:02

标签: java android passwords password-protection

我已经构建了我的第一个应用程序,我想用密码保护它。我可以将密码存储在Java文件中,并且该方法需要尽可能简单,因为在此应用程序之前我没有java或甚至xml的经验。我有几次尝试但失败了所以我希望有人可以帮助我。

我使用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();


                    }}});
    }}

3 个答案:

答案 0 :(得分:13)

这部分:

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

不正确。它应该是

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

在比较原始数据类型(例如intcharboolean)时,您可以使用==!=等。
在比较对象(例如StringCar等)时,您需要使用.equals()方法。

See also this page.

答案 1 :(得分:11)

检查你的密码是不安全的。

有多种方法可以轻松绕过您的代码

  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到你的服务器隐藏密码检查后面的机制。 (但这需要您的应用程序要求网络权限)

答案 2 :(得分:-6)

在编辑文本字段xml中,您可以添加

   android:password="true"
相关问题