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

发布于 2025-01-07 05:28:44 字数 2055 浏览 5 评论 0原文

我已经构建了我的第一个应用程序,我想用密码保护它。对我来说,将密码存储在 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();


                    }}});
    }}

I've built my first app, and I would like to password protect it. It's fine for me to store the password in the Java files and the method needs to be as simple as possible because i have no experience of java or even xml before this app. I've had a few attempts and failed so I was hoping someone can help me out.

I've created the layout with an EditText field:

<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 />

and a submit button:

<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"/>

The Java file:

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();


                    }}});
    }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简单爱 2025-01-14 05:28:44

这部分:

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

不正确。应该是

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

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

另请参阅此页面。

This part:

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

is incorrect. It should be

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

When comparing primitive data types (like int, char, boolean) you can use ==, !=, etc.
When comparing objects (like String, Car, etc) you need to use the .equals() method.

See also this page.

╭⌒浅淡时光〆 2025-01-14 05:28:44

这样检查您的密码绝对不安全。

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

  1. 直接从另一个应用程序调用活动

  2. 读取反汇编的smali 代码用于检索密码

  3. 修改代码使用smali始终跳转到codeblock

解决方案可用于解决这些问题:

  1. 隐藏您的代码(最差的选择,但在大多数情况下可能就足够了)

  2. 比较哈希密码:安全得多。但应该是加盐的哈希。
    (还有一个更简单易懂的实现说明)

  3. 使用 HTTP 请求到您的服务器进行隐藏背后的机制密码检查。 (但这需要您的应用程序请求网络权限)

It is in no way safe to check your password like that.

There are several ways to easily bypass your code

  1. Calling the activity directly from another App

  2. Reading the disassembled smali code to retrieve the password

  3. Modifying the code using smali to always jump into the codeblock

Solutions available to solve these problems:

  1. Obscure your code (Worst option, but might be enough in most cases)

  2. Comparing the Hashed Password: Much more secure. But should be a salted Hash.
    (There is also a more simple to understand explaination for the implementation)

  3. Use a HTTP Request to a server of yours to hide the mechanism behind the password check. (But that requires your app to ask for Networking Permissions)

一生独一 2025-01-14 05:28:44

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

   android:password="true"

In the edit text field xml you can add

   android:password="true"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文