[英]Password protect app on start
Hey guys i need a way on password protecting my app so when users click the app the password actitivty comes up first and they can only access the app if correct password. 嘿家伙我需要一种密码保护我的应用程序的方式,所以当用户点击应用程序密码actitivty首先出现,他们只能访问应用程序,如果密码正确。 Its part of project im doing but im stuck on this bit. 它的一部分项目正在做,但我坚持这一点。 PLease guys any hepl would be appreciated. PLease家伙任何肝脏将不胜感激。
Assuming you have a button that submits the contexts of TextEdit field: 假设您有一个提交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);
}
This requires in your layout for the password activity you have an edittext called passwordedittext and a button called submitbutton. 这需要在您的密码活动布局中,您有一个名为passwordedittext的edittext和一个名为submitbutton的按钮。
And you have your main activity (which needs to be included in your manifest file) which you should replace your_other_activity.class with. 并且您有自己的主要活动(需要包含在清单文件中),您应该将其替换为your_other_activity.class。
Error in this line: 这行错误:
if(passwordEditText.getText().ToString()=="your app password")
it should be 它应该是
if (passwordEditText.getText().ToString().equals("your app password")
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. 比较原始数据类型(如int,char,boolean)时,可以使用==,!=等。比较对象(如String,Car等)时,需要使用.equals()
方法。
take the text from your password activity and store it as a SharedPreference. 从密码活动中获取文本并将其存储为SharedPreference。 then everytime the user starts the app perform a check against the sharedpreferences you have stored 然后,每次用户启动应用程序时,都会对您存储的共享首选项执行检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.