Android unlock interface development-teaches you how to implement screen lock software in minutes

Source: Internet
Author: User
Tags home screen

I have been trying to build a screen lock interface for a long time. In the last week, I have experienced great hardships and setbacks. I have finally met this requirement perfectly. I will share my screen lock ideas here. Note: This is not a one-click lock screen. It is similar to the go lock screen interface. Preparation: This program requires two activities: home and main. A service: myService, a receiver: bootReceiver, and a layout: layout. home serves as the activity dedicated for the screen home key, and main is the activity mainly used to display the screen lock interface. The service is used to receive screen lock/unlock broadcasts. layout is the interface to be displayed by main. Idea :! Note: The following code is not ordered. For more information, see the source code! 1. Add a service to the program. When the Service receives the screen lock/unlock broadcast, close the System screen lock interface and open its own screen lock interface. Key code:/onReceive: keyguardManager = (KeyguardManager) context. getSystemService (context. KEYGUARD_SERVICE); keyguardLock = keyguardManager. newKeyguardLock (""); keyguardLock. disableKeyguard (); // unlock the system lock screen startActivity (toMainIntent); // jump to the main interface. Note that the above Code requires the registration permission: <uses-permission android: name = "android. permission. DISABLE_KEYGUARD "/> In addition, to prevent repeated calls to the main interface, we need to add filter: // to set intent filtertoMainIntent in myservice = New Intent (myService. this, Main. class); toMainIntent. addFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // This flags indicates that if this activity already exists, it will be mentioned to the top of the stack; otherwise, a new activity will be created. Copy the code // change the Startup Mode of the main interface in manifest to singleTask. FLAG_ACTIVITY_NEW_TASK "is similar to <activityandroid: name =" com. example. screenlocker. main "android: label =" @ string/app_name "android: launchMode =" singleTask "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> copy the code. Of course, you can also set service restart in onDestroy to ensure that the service has been running repeatedly in the background. Code @ Overridepublic void onDestroy () {super. onDestroy (); unregisterReceiver (screenReceiver); // restart startActivity (new Intent (myService. this, myService. class);} copy the code and run some other items, such as registering the service and starting the service on the main interface ~ If no error occurs, the screen lock page is displayed first after unlocking. 2. Implement the lock screen, but there is another problem: when you press the return key or home key, our interface is easily KO. Don't worry, Let's block them one by one. First, open the knife with the return key: you only need to add the following code to the main interface: copy the code @ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent. KEYCODE_BACK: return true;} return super. onKeyDown (keyCode, event);} copies the Code. However, the home key cannot be blocked or captured using this method. This is a headache for programmers. There are also various solutions on the Internet. However, I decided to use the GO lock screen method for my reference: the idea of this method is: Set your program as the system master screen! In this way, you have to press the home Key to go to the home screen. After setting it, press the home Key to jump directly to our interface! Let's create a new activity named "home" to snatch the home Key. Then set the following in manifest: copy the Code <activity android: name = "com. example. screenlocker. home "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. HOME "/> <category android: name =" android. intent. category. DEFAULT "/> </intent-filter> </activity>: copy the code. When you press the home key, if you press it on the main interface, the system will not respond. If you press the home key at other times, it will jump to this activity. (During the first running, you will be asked to press the home Key to jump to the "home screen" or "test1 (appName of the program we write ourselves)". That is to say, we have implemented the screen lock interface to block home. If you press home in a non-lock status, the home interface is called. We only need to set this in the home interface: onCreate () {jump to the main screen of the system; // for the specific code, see finish (); // end this activity, there is no need to load the interface for this activity. Therefore, we need to set the theme of this activity to "do not show". This will not only avoid waste, but also avoid screen flashing during jump, copy the code that affects the user experience. // set theme of home to noDisplay for manifest <activity android: name = "com. example. screenlocker. home "android: theme =" @ android: style/Theme. noDisplay "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. HOME "/> <category android: name =" android. intent. category. DEFAULT "/> </intent- Filter> </activity> copy the Code. In oncreate of home, run the following code: Log. e ("", "home is called"); finish (); // call finish to prevent the program from being stuck in an activity that is not displayed and then run it again, we can clearly find that when we press the home key, this activity will be called. 3. The home key has been snatched. Next, let's jump the home interface to the main interface: first, we need to get a list: list the programs that can be used as the main screen: copy the code List <String> pkgNamesT = new ArrayList <String> (); List <String> actNamesT = new ArrayList <String> (); List <ResolveInfo> resolveInfos = context. getPackageManager (). queryIntentActivities (intent, PackageManager. MATCH_DEFAULT_ONLY); for (int I = 0; I <resolveInfos. size (); I ++) {String string = resolveInfos. get (I ). activityInfo. packageName; if (! String. equals (context. getPackageName () {// exclude your package name pkgNamesT. add (string); string = resolveInfos. get (I ). activityInfo. name; actNamesT. add (string) ;}} then copy the code. You can use alertDialog to select the screen to jump to and use sharedPreferences to record your selections. Copy the Code new AlertDialog. builder (context ). setTitle ("select the unlocked screen "). setCancelable (false ). setSingleChoiceItems (names, 0, new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {editor. putString (packageName, pkgNames. get (which); editor. putString (activityName, actNames. get (which); editor. commit (); originalHome (); dialog. dismiss ();}}). show (); copy the code so that the next time you can jump directly to the user-set home screen based on the package name and Class Name: copy the code String pkgName = sharedPreferences. getString (packageName, null); String actName = sharedPreferences. getString (activityName, null); ComponentName componentName = new ComponentName (pkgName, actName); Intent intent = new Intent (); intent. setComponent (componentName); context. startActivity (intent); (Activity) context ). finish (); copy Code 4. Of course, we also want to set it to boot: copy the code public class BootReceiver extends BroadcastReceiver {String myPkgName = "com. example. screenlocker "; // # package name String myActName =" com. example. acts. myService "; // # class name @ Override public void onReceive (Context context, Intent intent) {// start listening service Intent myIntent = new Intent (); myIntent. setAction (myPkgName + ". "+ myActName); context. startService (myIntent) ;}} copy the code. Note: Do not forget to add the boot permission to manifest: <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED "/> however, we recommend that you add the following code in the home: startService (new Intent (Home. this, ScreenReceiver. class ));

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.