Husam A. Al-ahmadi Husam A. Al-ahmadi - 2 months ago 18
Android Question

How to Set new Admin

I am using the Device Policy Manager to lock the android phone immediately and here is my activity code:

package com.husam.admin;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

public class AdminActivity extends Activity {

/** Called when the activity is first created. */
protected static final int REQUEST_CODE_ENABLE_ADMIN=1;
DevicePolicyManager dpm;
ComponentName mAdminName;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName=new ComponentName(this,MyAdmin.class);
Button button=(Button)findViewById(R.id.admin);
button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {

// TODO Auto-generated method stub
if(dpm.isAdminActive(mAdminName))
{
Log.w("Yes admin","Locking Now");
dpm.lockNow();
}
else
{Intent intent1 =new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Need new Admin");
Log.w("no Admin","Set admin");
startActivityForResult(intent1,REQUEST_CODE_ENABLE_ADMIN);
}

}
});

}

class MyAdmin extends DeviceAdminReceiver{

void OnEnabled(){

}
void onDisable(){

}
}
}


and My Mainfest.xml fill is as follow:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.husam.admin"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AdminActivity"
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=".MyAdmin"
android:label="@string/device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>

</manifest>


and my_admin.xml which is inside xml folder under res folder is as follows:

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


and when I run my app I faced an error in my Log file state that:

W/DeviceAdminAdd(433): Unable to retrieve device policy ComponentInfo{com.husam.admin/com.husam.admin.AdminActivity$MyAdmin}


I searched this website for similar issued but all what I found is that the error was in the mainfest file in closing the receiver before including the meat and intent filter inside it which is not as my case.

So any help or redirection to solve this issue will be completely appreciated

regards

Husam

Answer

I think I figured out the answer to the question I posted above. What I need to do is that in registering my receiver in my mainfest file I have to do the following:

        <receiver 
            android:name=".AdminActivity$MyAdmin"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data android:name="android.app.device_admin"
                android:resource="@xml/my_admin"/>
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>

Note:

in android:name I have to write .AdminActivity$Myadmin instead of .Myadmin, since my class for administration broadcast receiver is an inner class in my activity, not a separate one.