0

I am trying to use device Admin API in my app.

My DeviceAdmin caller class is this:

public class DeviceAdminCaller extends Activity {
static final int activationCode = 47;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    ComponentName deviceAdminComponent = new ComponentName(this, DeviceAdmin.class);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponent);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "We recommend you to enable this");
    startActivityForResult(intent, activationCode);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case activationCode:
        if (resultCode == Activity.RESULT_OK) {
            Log.i("DeviceAdminSample", "Administration enabled!");
        } else {
            Log.i("DeviceAdminSample", "Administration enable FAILED!");
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
}

And my subclass of DeviceAdminReciever is this:

public class DeviceAdmin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onEnabled(context, intent);
    Log.i("Device Admin", "Enabled");
}

@Override
public String onDisableRequested(Context context, Intent intent) {
    // TODO Auto-generated method stub
    return "Admin disable Requested";
}

@Override
public void onDisabled(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onDisabled(context, intent);
    Log.i("Device Admin", "Disables");

}

@Override
public void onPasswordChanged(Context context, Intent intent) {
    // TODO Auto-generated method stub
    super.onPasswordChanged(context, intent);
    Log.i("Device Admin", "Password Changed");
}
}

The part of the Manifest containing the DeviceAdmin is :

<activity
        android:name=".DeviceAdmin"
        android:label="activity_sample_device_admin" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.SAMPLE_CODE" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DeviceAdminCaller"
        android:label="activity_caller_device_admin" >
        <intent-filter>
            <action android:name="android.intent.action.DEVICE_ADMIN_CALLER" />

            <category android:name="android.intent.category.DEFAULT" />
            <!-- <category android:name="android.intent.category.SAMPLE_CODE" /> -->
        </intent-filter>
    </activity>

    <receiver
        android:name=".DeviceAdmin"
        android:permission="android.permission.BIND_DEVICE_ADMIN" >
        <meta-data
            android:name="android.app.device_admin_sample"
            android:resource="@xml/device_admin_sample" />

        <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>

The LogCat output is: Administration enable FAILED!

What am I doing wrong? I have a project submission due today. Any help would be appreciated . :)

Cup of Java
  • 1,769
  • 2
  • 21
  • 34
David
  • 13
  • 3

1 Answers1

0

What am I doing wrong?

You are calling startActivityForResult(), for an Intent action that does not support it. In those cases, you always get back RESULT_CANCELLED.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the help :) should I just call the StartActivity(intent) ???? Will it do then??? Is it the only problem.? – David Dec 11 '15 at 21:12
  • @arnavbhartiya: "should I just call the StartActivity(intent) ?" -- you may as well, since `startActivityForResult()` is not going to give you a useful result. "Will it do then?" -- I do not understand your question, sorry. If it helps, [here is a sample app](https://github.com/commonsguy/cw-omnibus/tree/master/DeviceAdmin/LockMeNow) from [my book](https://commonsware.com/Android) that demonstrates implementing device administration, including using `ACTION_ADD_DEVICE_ADMIN`. – CommonsWare Dec 11 '15 at 21:14
  • I tried the StartActivity(intent) but it the DeviceAdmin screen is still not launching. Thanks – David Dec 11 '15 at 21:24
  • @arnavbhartiya: Your question does not say that the device admin screen is not launching. Check LogCat to see if there are any useful messages. – CommonsWare Dec 11 '15 at 21:26
  • I looked into your code and found that I had not attached a layout with it and so I created a R.layout.main but still the problem is not resolved. The logcat says : 12-11 17:06:30.359: E/AndroidRuntime(16807): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applock/com.example.applock.DeviceAdminCaller}: java.lang.NullPointerException. What is the NullPointerException here? – David Dec 11 '15 at 22:13
  • @arnavbhartiya: "What is the NullPointerException here?" -- I cannot tell you that. I suggest that you ask a separate Stack Overflow answer, where you post your current code for `DeviceAdminCaller`, along with the **complete stack trace** (not just the error message). – CommonsWare Dec 11 '15 at 22:17
  • I don't think that's an issue. Google documentation itself uses the same method call in a tutorial. – KMC Nov 01 '19 at 02:45