Android Simple Combat Tutorial--the fifth session of the development of one-touch lock screen Application

Source: Internet
Author: User

Reprint Please specify the Source: http://blog.csdn.net/qq_32059827/article/details/51860900 Click to open linkDevice administration for this application, a lot of the market, but look at the review will know how bad effect, because 99% one-button lock screen application can not be uninstalled. Development of a small application today, enabling easy click on a button lock screen, while supporting the uninstall ...
Through the Google original document, there is a Device Manager API, from here can draw some code, developed a small application, that is, the introduction of the "one-key lock screen."
Depending on the document translation, the approximate steps to get the Device Manager are as follows:

1, creating Subclasses of class Deviceadminreceiver

如:com.itydl.lockscreen.DeviceAdminSample

2. Configure the broadcast recipient in the manifest file

<receiver android:name=" Com.itydl.lockscreen.DeviceAdminSample "android:des cription= "@string/sample_device_admin_description" android:label= "@string/sample_device_admin" Android:permi            ssion= "Android.permission.BIND_DEVICE_ADMIN" > <meta-data android:name= "android.app.device_admin" Android:resource= "@xml/device_admin_sample"/> <intent-filter> <action Android:na Me= "Android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver>  

3, configuring string-related information

 <string name="activity_sample_device_admin">设备管理员</string><string name="sample_device_admin">管理员</string><string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>

4, creating an XML folder under the Res directory , under this folder create Deviceadmin sample.xml file, Contents:

   <device-admin xmlns:android="http://schemas.android.com/apk/res/android">  <uses-policies>    <limit-password />    <watch-login />    <reset-password />    <force-lock />    <wipe-data />    <expire-password />    <encrypted-storage />    <disable-camera />  </uses-policies></device-admin>

5. Create Device Manager and components in code

dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);    ComponentName who = new ComponentName(this, DeviceAdminSample.class);

6, Write function

dpm.lockNow();一键锁屏
The above steps are somewhat obscure, take a look at the actual steps (recommended by reading the official website AP Document I steps):
1. Copy the code from the official document below to the project you created:

Copy past found error, because three description attribute information is not configured, then go to String.xml to configure the following three message:
 <string name="activity_sample_device_admin">设备管理员</string><string name="sample_device_admin">管理员</string><string name="sample_device_admin_description">开启设备管理员,不开启扣2000块</string>
Remove the Activity node information from the system in the configuration file. Modify the activity tag that was copied name for our project.
This is still an error, because there is a android:resource= "@xml/device_admin_sample" is not configured to create a new XML folder, build a device_admin_sample file, which needs to configure some content. Write all the contents of the official document. The contents are as follows:



Solved the problem of error.
2, according to the requirements of the document, create a new class Deviceadminsample extends Deviceadminreceiver. This is a service, there is no need to write any content
Remember to change the activity and service name in the configuration file to our own. Android:name= "Com.itydl.lockscreen.DeviceAdminSample"
Android:name= "Com.itydl.lockscreen.MainActivity"

3. Add the following two lines of code to the activity:
Devicepolicymanager dmp = (Devicepolicymanager) getsystemservice (Device_policy_service);
Dmp.locknow ();//A key lock screen at this time has actually been developed, but this is only valid for the programmer, into the Device Manager to activate the application; For the user does not know where to activate, it is not necessary to experience, so also need to set a key lock screen, add to open Device Manager activity and logic. The specific code is as follows: The manifest file:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.itydl.lockscreen "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" t Rue "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appt Heme "> <activity android:name=" com.itydl.lockscreen.MainActivity "Android:labe                L= "one button lock screen" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/>        <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name= "Com.itydl.lockscreen.DeviceAdminSample" Android: description= "@string/sample_device_admiN_description "android:label=" @string/sample_device_admin "android:permission=" Android.permission.BI Nd_device_admin "> <meta-data android:name=" android.app.device_admin "Andro Id:resource= "@xml/device_admin_sample"/> <intent-filter> <action android:name= "Android Oid.app.action.DEVICE_ADMIN_ENABLED "/> </intent-filter> </receiver> </application& Gt;</manifest>

Package Com.itydl.lockscreen;import Android.os.bundle;import Android.app.activity;import Android.app.admin.devicepolicymanager;import Android.content.componentname;import Android.content.Intent;import Android.view.menu;import Android.view.view;public class Mainactivity extends Activity {private Devicepolicymanager dmp ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);d MP = (Devicepolicymanager) getsystemservice (device_policy_service);} /** * One-touch lock screen * * @param v */public void Lockscreen (View v) {//If the device administrator is not activated, alert the user to work componentname who = new ComponentName (this , Deviceadminsample.class), if (Dmp.isadminactive (WHO)) {//TRUE indicates that Dmp.locknow () has been activated,//one-button lock screen} else {/** According to official documents, Write activity to start the Device Manager *//Launch The activity to the user enable our admin. Intent * Intent = new Intent (devicepolicymanager.action_add_device_admin); * Intent.putextra (devicepolicymanager.extra_device_admin, * mdeviceadminsample); * Intent.putextra(Devicepolicymanager.extra_add_explanation, * mactivity.getstring (R.string.add_admin_extra_app_text)); * Startactivityforresult (Intent, request_code_enable_admin); */intent Intent = new Intent (devicepolicymanager.action_add_device_admin); Intent.putextra ( Devicepolicymanager.extra_device_admin, WHO); Intent.putextra (devicepolicymanager.extra_add_explanation, "Device Manager ... "); Startactivityforresult (intent, 1);}}

Remember to deactivate the Device Manager at this time. One click to run the following:


Click on a button lock screen, automatically jump to the Device Manager page, press activiate. The application is in effect.
This time the application is developed, it is possible to implement the lock screen function, but there are many problems to be solved. You will find that at this time and the application of the same as the market comments, can not uninstall!!! This user experience is also very poor, and must be opened in order to lock the screen, there is no need to waste time to open an application to lock the screen. For the above several issues, the blog post in the back continues to update the resolution.

Android Simple Combat Tutorial--the fifth session of the development of one-touch lock screen Application

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.