Wednesday, 31 August 2016

How to make our application Device administrator in Android


                Here I am going to explain Device Administration in Android


    Device Administration API provides device administration features at the system level. These APIs allow you to create security-aware applications that are useful in enterprise settings, in which IT professionals require rich control over employee devices


  Here are examples of the types of applications that might use the Device Administration API:


  •           Email clients.
  •           Security applications that do remote wipe.
  •           Device management services and applications.
  •           Change Password.
  •           Lock Screen


                                      




Example

    public class MainActivity extends AppCompatActivity {

    private DevicePolicyManager mgr=null;
    private ComponentName cn=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar );

        cn = new ComponentName(this, AdminReceiver.class);
        mgr =(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);
        lockMe();
    }
     public void lockMe() {
            if (mgr.isAdminActive(cn)) {
                mgr.lockNow();
            }
            else {
                Intent intent=new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                        "AndroidDhina");
                startActivity(intent);
            }
        }
    }


AndroidManifest

  <uses-feature
        android:name="android.software.device_admin"
        android:required="true"/>

AdminReceiver

 public class AdminReceiver extends DeviceAdminReceiver 
  {
  }


xml

 <device-admin xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-policies>
        <force-lock/>
    </uses-policies>

</device-admin>


Happy Coding :)