Implementation of the Android app Password

Source: Internet
Author: User

Recently, I made a demo. The function is to lock my application. During access, I need to enter a password, and I need to re-enter the password every time I lock the screen. Main Idea: Implementation: 1. Implementation of global variables 1.1 It is OK to use a Global static variable. 1.2 but on Android, we can use a more "elegant" implementation: using Android Application objects (documents ).

  • Write a MyApplication class that inherits from android. app. Application.
  • Define a state variable in MyApplication and initialize it in the onCreate () method.
  • Add the name attribute to the tag in AndroidMainfest. xml with the value of the complete Class name of MyApplication.

    Android: name = "barry. demo. passwordApp. MyApplication"

  • You can useMyApplication myApplaction = (MyApplication) getApplication ();To obtain the instance of MyApplication and use or modify the variables.
2. Determine whether to enter a password Based on the status.

BecauseOnResume ()The method is called every time the Activity is displayed or re-painted (see the lifecycle of the Activity), so you can determine the status in this method.

The specific method can be:

  • Write a PasswordActivity interface for entering the password;

  • Write a BaseActivity, write the judgment code in the onResume () method, and then all other activities inherit the BaseActivity.-Of course, except PasswordActivity on the password interface.

BaseActivity looks like this:

Public class BaseActivity extends Activity {MyApplication myApplaction; protected void onResume () {super. onResume (); myApplaction = (MyApplication) getApplication (); if (myApplaction. isLocked) {// determine whether to jump to the password interface Intent intent = new Intent (this, PasswordActivity. class); startActivity (intent );}};}

PasswordActivity looks like this:

Public class PasswordActivity extends Activity {MyApplication myApplaction; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_password); myApplaction = (MyApplication) getApplication (); final EditText editTextPassword = (EditText) findViewById (R. id. editTextPassword); findViewById (R. id. button1 ). setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {String password = editTextPassword. getText (). toString (); if (password! = Null & password. equals (myApplaction. password) {Toast. makeText (PasswordActivity. this, "the password is correct! ", Toast. LENGTH_SHORT). show (); myApplaction. isLocked = false; PasswordActivity. this. finish () ;}else {Toast. makeText (PasswordActivity. this," the password is incorrect! ", Toast. LENGTH_SHORT). show (); editTextPassword. setText ("");}}});}}


3. Listen for lock screen events

After the above two steps, you can implement the application lock-when the application is opened, it will automatically jump to the password interface, enter the password and then enter the content Activity. -- However, the password will only be entered once when the program is opened. If the program is not completely closed, the phone will be locked and then opened again. You still do not need to enter the password.

To enable the lock after the phone is turned on, you must listen to the lock events of the phone. After the lock, change the application status to locked, in this case, the password is required again.

3.1 listener lock implementation

For example, when the system is turned on, the screen is locked, the screen is turned on, the network status is changed, there are calls in, there are short messages in, the system power is too low, and so on, when the status of various systems changes, the Android system itself sends a broadcast.

What we need to do is register a broadcast receiver and use it to listen to the system broadcast. When a screen lock event is detected, change the application status.

Because the broadcast receiver is created when the application is started and belongs to the global application, we create it in the MyAppLication classOnCreate () method of MyApplicationInOnTerminate ()Method.

3.2 build a broadcast Receiver

It is very easy to construct a broadcast receiver. You only need to inherit the android. content. BroadcastReceiver class and implement the onReceive () method. The onReceive () method can be processed after being broadcasted.

Class LockScreenReceiver extends BroadcastReceiver {@ Override public void onReceive (Context context, Intent intent) {/* process broadcast here */if (Intent. ACTION_SCREEN_OFF.equals (intent. getAction () {isLocked = true; // set the application status to locked }}}

3.3 register Broadcast

Construct an IntentFilter, add the broadcast type to be received in it, and then call the registerReceiver method of Context.

// Register aggreger = new LockScreenReceiver (); filter = new IntentFilter (); filter. addAction (Intent. ACTION_SCREEN_OFF); this. registerReceiver (receiver, filter); // cancel registration this. unregisterReceiver (receiver );


So far, an application demo that can be accessed with a password is basically OK.

Download Demo code: Address

The interface is ugly. You have time to change it.

Synchronous publishing in http://www.barryzhang.com/archives/146

Advertise my new blog. Welcome to visit Kazakhstan ~ :BarryZhang.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.