密码保护我的 Android 应用程序(简单的方法)
我已经构建了我的第一个应用程序,我想用密码保护它。对我来说,将密码存储在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这部分:
不正确。应该是
当比较原始数据类型(如
int
、char
、boolean
)时,您可以使用==
,!=
等比较对象(例如
String
、Car
等)时,您需要使用.equals()
方法。另请参阅此页面。
This part:
is incorrect. It should be
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.
这样检查您的密码绝对不安全。
有多种方法可以轻松绕过您的代码
直接从另一个应用程序调用活动
读取反汇编的smali 代码用于检索密码
修改代码使用smali始终跳转到codeblock
解决方案可用于解决这些问题:
隐藏您的代码(最差的选择,但在大多数情况下可能就足够了)
比较哈希密码:安全得多。但应该是加盐的哈希。
(还有一个更简单易懂的实现说明)
使用 HTTP 请求到您的服务器进行隐藏背后的机制密码检查。 (但这需要您的应用程序请求网络权限)
It is in no way safe to check your password like that.
There are several ways to easily bypass your code
Calling the activity directly from another App
Reading the disassembled smali code to retrieve the password
Modifying the code using smali to always jump into the codeblock
Solutions available to solve these problems:
Obscure your code (Worst option, but might be enough in most cases)
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)
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)
在编辑文本字段 xml 中,您可以添加
In the edit text field xml you can add