Device Manager for lock screen and data cleansing functions

Source: Internet
Author: User

1. First create a class deviceadminsample inherit Deviceadminreceiver (Android.app.admin.DeviceAdminReceiver), as the processing class to receive broadcast information (in fact, do not implement, The main internal is also maintained the Devicepolicymanager class instance, the main operation function is this class's internal method implementation)

 Package Com.example.deviceadmi; Import Android.app.admin.DeviceAdminReceiver; Import Android.content.Context; Import android.content.Intent;  Public class extends deviceadminreceiver {    @Override    publicvoid  onreceive (Context Context, Intent Intent) {        super. OnReceive (context, Intent);    }}

2. Configuring the receiver information on the manifest configuration file

<receiver            android:name= "Com.example.deviceadmi.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= "@layout/device_admin_sample"/><!- -device_admin_sample XML file needs to be created--            <intent-filter>                <action android:name= " Android.app.action.DEVICE_ADMIN_ENABLED "/>            </intent-filter>        </receiver>

3. Create a Device_admin_sample.xml file

<?xml version= "1.0" encoding= "Utf-8"? ><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>

4. Create a call directly on the activity to

 PackageCom.example.deviceadmi;ImportAndroid.app.admin.DevicePolicyManager;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {Privatecomponentname mdeviceadminsample; PrivateDevicepolicymanager MDPM; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); Button btn_active=(Button) Findviewbyid (r.id.btn_active); Button Btn_lock=(Button) Findviewbyid (R.id.btn_lock); Button Btn_wipedata=(Button) Findviewbyid (r.id.btn_wipedata); Button Btn_unintall=(Button) Findviewbyid (R.id.btn_unintall); Mdeviceadminsample=NewComponentName ( This, Deviceadminsample.class); MDPM=(Devicepolicymanager) Getsystemservice (Context.device_policy_service); Btn_active.setonclicklistener (NewOnclicklistener () {//invoke Device Manager's page display, activate need to click on your own@Override Public voidOnClick (View v) {Intent Intent=NewIntent (devicepolicymanager.action_add_device_admin);                Intent.putextra (Devicepolicymanager.extra_device_admin, mdeviceadminsample); Intent.putextra (Devicepolicymanager.extra_add_explanation,"Device Manager");            StartActivity (Intent);                }        }); Btn_lock.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {if(Mdpm.isadminactive (Mdeviceadminsample)) {//determine if the defined component is turned on and activatedMdpm.locknow (); Mdpm.resetpassword ("123", 0); }Else{Toast.maketext (Getapplicationcontext (),"The Device Manager has not been activated yet", Toast.length_long). Show ();                }            }        }); Btn_wipedata.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//The TODO Simulator is not clearing the data, the real machine clears the data                if(Mdpm.isadminactive (mdeviceadminsample)) {Mdpm.wipedata (0);//erase data stored inside the phone//Mdpm.wipedata (devicepolicymanager.wipe_external_storage);//erase data stored externally from your phone}Else{Toast.maketext (Getapplicationcontext (),"The Device Manager has not been activated yet", Toast.length_long). Show ();                        }            }        }); /*** Uninstall Page call * <activity android:name= ". Uninstalleractivity "android:configchanges=" Orientation|keyboardhidden "Android:theme=" @styl E/talltitlebartheme "> <intent-filter> <action android:name=" Android.intent.action.VI EW "/> <action android:name=" Android.intent.action.DELETE "/> <category android: Name= "Android.intent.category.DEFAULT"/> <data android:scheme= "package"/> </intent -filter> </activity>*/Btn_unintall.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent ("Android.intent.action.DELETE"); Intent.addcategory ("Android.intent.category.DEFAULT"); Intent.setdata (Uri.parse ("Package:" +getpackagename ()));//Uninstall the current appstartactivity (Intent);            }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

Activity_main.xml Layout file

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"Tools:context= "Com.example.deviceadmi.MainActivity" > <Button Android:id= "@+id/btn_active"Android:text= "One Click to open"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/btn_lock"Android:text= "One button lock screen"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/btn_wipedata"Android:text= "One-click Erase data"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/> <Button Android:id= "@+id/btn_unintall"Android:text= "One click Uninstall"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/></linearlayout>

Operation Result:

Reference Device Administration

Device Manager for lock screen and data cleansing functions

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.