Android Hands-on tutorial Fifth one key lock screen application _android

Source: Internet
Author: User
Tags xmlns

Device Administration

For this application, a lot of the market, but look at the comment on how bad the effect, because 99% one-button lock screen application can not uninstall. Today to develop a small application, easy to click a key lock screen, while supporting the uninstall ...

Access to the original Google document, there is a Device Manager API, from here you can extract some code to develop a small application, that is about to introduce the "one key lock screen."
According to the documentation translation, the approximate steps to get Device Manager are as follows:

1, create subclass of Class Deviceadminreceiver

such as:com.itydl.lockscreen.DeviceAdminSample

2. Configure the broadcast receiver in the manifest file

<receiver
  android:name= "com.itydl.lockscreen.DeviceAdminSample"
  android:description= "@string/sample _device_admin_description "
  android:label=" @string/sample_device_admin "
  android:permission=" Android.permission.BIND_DEVICE_ADMIN ">
  <meta-data
   android:name=" Android.app.device_admin "
   Android:resource= "@xml/device_admin_sample"/>

  <intent-filter>
   <action android:name= " Android.app.action.DEVICE_ADMIN_ENABLED "/>
  </intent-filter>
 </receiver>

3. Configuration string Related information

 <string name= "Activity_sample_device_admin" > Device Administrator </string>
<string name= "Sample_device_admin" > Admin </string>
<string name= "sample_device_admin_description" > Open Device Manager, do not open buckle 2000 block </string >

4. Create the XML folder under the Res directory, create the Deviceadminsample.xml file under this folder, content:

 <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 component in code

DPM = (Devicepolicymanager) getsystemservice (device_policy_service);
 ComponentName who = new ComponentName (this, deviceadminsample.class);

6, function

Dpm.locknow (); one-button lock screen

The above steps are somewhat obscure, take a look at the actual operation steps (recommended by reading the official website AP Document I step-by-step operation):

1. Copy the code of the configuration file given by the official document below to the project you created:


Replication has found an error in the past, because three description attribute information is not configured, then go to String.xml to configure the following three information:

<string name= "Activity_sample_device_admin" > Device Administrator </string>
<string name= "Sample_device_admin" > Admin </string>
<string name= "sample_device_admin_description" > Open Device Manager, do not open buckle 2000 block </string >

Deletes the activity node information from the system from the configuration file. Modify the copied activity label name to our project's active name.

This is still an error, because there is a android:resource= "@xml/device_admin_sample" is not configured
Create a new XML folder and build a device_admin_sample file with some content to configure. Write all the content in the official document.
The contents are as follows:

Resolved the error problem.

2, according to the document requirements, a new class Deviceadminsample extends Deviceadminreceiver. This is a broadcast receiver, which does not need to write any content, and remember to modify the activity and receiver names in the configuration file to our own.

Android:name= "Com.itydl.lockscreen.DeviceAdminSample"
Android:name= "Com.itydl.lockscreen.MainActivity"

3, in the event click on the incident to add the following two lines of code:

  Devicepolicymanager dmp = (Devicepolicymanager) getsystemservice (device_policy_service);
  Dmp.locknow ()//One Key lock screen

At this point in fact has been developed, after the operation will be an error, found no "owner" permission similar to the wrong message. Because this is only effective for programmers, enter the device Manager to activate this application, and then open it can be used, for users, do not know where to activate, it is not necessary to experience, so you need to set a key lock screen, start the program, first into the logic of the Device Manager activity, let the user manually activated.
The specific code is as follows:
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 A ndroid:minsdkversion= "8" android:targetsdkversion= "/>" <application android:allowbackup= "true" Andro id:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <act Ivity android:name= "com.itydl.lockscreen.MainActivity" android:label= "one key lock screen" > <intent-filter> &L T;action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCH" ER "/> </intent-filter> </activity> <receiver android:name=" Com.itydl.lockscreen.Devic Eadminsample "android:description=" @string/sample_device_admin_description "android:label=" @string/sample_device _admin "Android:permission=" Android. permission. Bind_device_admin "> <meta-data android:name=" android.app.device_admin "android:resource=" @xml/device_ Admin_sample "/> <intent-filter> <action android:name=" Android.app.action.DEVICE_ADMIN_ENABLED "/&G" 
   T 
 </intent-filter> </receiver> </application> </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; 
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     Setcontentview (R.layout.activity_main); 
 Gets the device Administrator object dmp = (Devicepolicymanager) getsystemservice (Device_policy_service); /** * One-button lock screen * * @param v/public void lockscreen (View v) {//If the Device Manager is not activated, remind the user to do things componentname 
  who = new ComponentName (this, deviceadminsample.class); The IF (Dmp.isadminactive (WHO)) {//True indicates that it has been activated and that the test administrator has been activated. The WHO in fact is our custom empty broadcast receiver Dmp.locknow ()//a key lock} else {////helps the user to open the interface that activates the Device Manager, allowing the user to manually activate/** according to the official document, write the Activity of the Device Manager *//Launch The activities to have 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 point.

After one key is run as follows:

Click a key lock screen, automatically jump to the Device Manager page, press activiate. The application is effective.

This time the application is a development completed, it is true to achieve the lock screen function, but there are many problems to be solved.
You'll find that at this point, the application is just like the market comment, and you can't uninstall!!!.

This user experience is also very poor, and must be used to lock the screen, there is no need to waste time to open an application to lock the screen.

For the above several questions, in the back of the posting continue to update the resolution, click View :http://www.jb51.net/article/96996.htm.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.