Action not allowed with Android admin rights.
I am trying to enable my application as a device administrator. The code that tries to invoke the action that gives my app device admin permission, which is in the first activity of my app, looks like this:
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.GooglePlayServicesUtil;
public class RegisterActivity extends Activity {
// alert dialog manager
AlertDialogManager alert = new AlertDialogManager();
// Internet detector
ConnectionDetector cd;
// UI elements
EditText txtName;
EditText txtEmail;
// Register button
Button btnRegister;
static final int ACTIVATION_REQUEST = 47; //Request ID for Device Administrator
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//Unrelated
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != 0) {
Toast.makeText(this, "This device is not supported - please download Google Play Services.",
Toast.LENGTH_LONG).show();
}
//Begin enabling device administrator
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "You must enable device administration for certain features"
+ " of the app to function.");
//I thought that this would start the activity that lets the user
//choose whether to enable the app as a device admin
startActivityForResult(intent, ACTIVATION_REQUEST);
//Also contains code pertaining to unrelated Google Cloud messaging features
//and an onClick() method, all inside of onCreate()
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ACTIVATION_REQUEST:
if (resultCode == Activity.RESULT_OK) {
Log.i("DeviceAdminSample", "Administration enabled!");
}
else {
Log.i("DeviceAdminSample", "Administration enable FAILED!");
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
Using this code, when I launch my app an action appears and then an animation in which it looks like it opens a new activity, but then this animation stops and stays in the current activity without asking the user to allow the device administrator. None of my log messages related to the device administrator got hit. However, when viewing the current device adminsitrators on the Settings-> Security-> Device Administrators page, my app appears, but it is not marked as a device administrator.
Here is the code for my DeviceAdmin subclass, although I can't see where the problem might occur in this code:
import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
super.onEnabled(context, intent);
Log.i("Device Admin: ", "Enabled");
}
@Override
public String onDisableRequested(Context context, Intent intent) {
return "Admin disable requested";
}
@Override
public void onDisabled(Context context, Intent intent) {
super.onDisabled(context, intent);
Log.i("Device Admin: ", "Disabled");
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
super.onPasswordChanged(context, intent);
Log.i("Device Admin: ", "Password changed");
}
}
Any help would be greatly appreciated!
UPDATE: The Device Admin Receiver in my Android Manifest looks like this:
<!-- Device Administration Receiver -->
<receiver
android:name="com.myPackage.DeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
source to share
I found the problem with Jagadesh.
In my android manifest file, I have set the receiver name to DeviceAdminReceiver as shown here.
<receiver
android:name="com.myPackage.DeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
I was wrong in that the receiver should carry the class name DeviceAdminReceiver instead of the name of the subclass I wrote that extends DeviceAdminReceiver, DeviceAdmin
Updated working receiver in manifest file:
<!-- Device Administration Receiver -->
<receiver
android:name="com.myPackage.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
source to share
I think you forgot to add recipient to manifest with correct intent filters
<receiver
android:name="com.yourpackage.DeviceAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<intent-filter>
This action is required
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
This is required this receiver to become device admin component.
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
</receiver>
And XMl
<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>
Learn more about device administration. check it
source to share