Android Studio Code for Lock Screen
Pre-requisites: Eclipse IDE, Android SDK
Step 1: Create Android project
Launch Eclipse IDE and create a new Android application project called AndroidLockScreenDemo with package name com.example
Step 2: Create Activity class
Create a new Activity class called MyLockScreenActivity within the above package and add the following code!
MyLockScreenActivity.java
package com.example;import android.app.Activity;import android.app.admin.DevicePolicyManager;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MyLockScreenActivity extends Activity implements OnClickListener {    private static final int ADMIN_INTENT = 15;    private static final String description = "Sample Administrator description";    private DevicePolicyManager mDevicePolicyManager;     private ComponentName mComponentName;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_lock);        mDevicePolicyManager = (DevicePolicyManager)getSystemService(                  Context.DEVICE_POLICY_SERVICE);          mComponentName = new ComponentName(this, MyAdminReceiver.class);          Button btnEnableAdmin = (Button) findViewById(R.id.btnEnable);        Button btnDisableAdmin = (Button) findViewById(R.id.btnDisable);        Button btnLock = (Button) findViewById(R.id.btnLock);        btnEnableAdmin.setOnClickListener(this);        btnDisableAdmin.setOnClickListener(this);        btnLock.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.btnEnable:            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,description);            startActivityForResult(intent, ADMIN_INTENT);            break;        case R.id.btnDisable:            mDevicePolicyManager.removeActiveAdmin(mComponentName);              Toast.makeText(getApplicationContext(), "Admin registration removed", Toast.LENGTH_SHORT).show();            break;        case R.id.btnLock:            boolean isAdmin = mDevicePolicyManager.isAdminActive(mComponentName);              if (isAdmin) {                  mDevicePolicyManager.lockNow();              }else{                Toast.makeText(getApplicationContext(), "Not Registered as admin", Toast.LENGTH_SHORT).show();            }            break;        }    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (requestCode == ADMIN_INTENT) {            if (resultCode == RESULT_OK) {                Toast.makeText(getApplicationContext(), "Registered As Admin", Toast.LENGTH_SHORT).show();            }else{                Toast.makeText(getApplicationContext(), "Failed to register as Admin", Toast.LENGTH_SHORT).show();            }        }    }} | 
activity_lock.xml
    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MyLockScreenActivity">    <TextView        android:id="@+id/txt1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"/>    <Button        android:id="@+id/btnEnable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Enable"        android:layout_below="@+id/txt1"/>    <Button        android:id="@+id/btnDisable"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Disable"        android:layout_below="@+id/btnEnable"/>    <Button        android:id="@+id/btnLock"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Lock"        android:layout_below="@+id/btnDisable"/></RelativeLayout> | 
Step 3: Add lock policy and create receiver
Create a new folder called xml inside the project and add an XML file for exercising admin rights called admin.xml containing the following code!
    <uses-policies>        <force-lock />    </uses-policies></device-admin> | 
You also need to create an empty Receiver class that will extend the DeviceAdminReceiver. I am going to name my class as MyAdminReceiver.
MyAdminReceiver.java
package com.example;import android.app.admin.DeviceAdminReceiver;public class MyAdminReceiver extends DeviceAdminReceiver{} | 
In this case, one needs to update the AndroidManifest.xml file and add the Receiver entry as follows!
<?xml version="1.0" encoding="utf-8"?>    package="com.example"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.MyLockScreenActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver            android:name="MyAdminReceiver"            android:permission="android.permission.BIND_DEVICE_ADMIN">            <meta-data                android:name="android.app.device_admin"                android:resource="@xml/admin"/>            <intent-filter>                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />            </intent-filter>        </receiver>             </application></manifest> | 
Save all changes and make sure no errors are present. Finally, run the application on an Android device and you should now be able to lock your screen through code! 


Comments
Post a Comment