0

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

My DeviceAdmin caller class is this:

public class DeviceAdminCaller extends Activity implements OnClickListener {
static final int activationCode = 47;
Button b;
DevicePolicyManager mgr;
ComponentName deviceAdminComponent = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b.findViewById(R.id.ButtonAdmin);
    b.setOnClickListener(this);
    deviceAdminComponent = new ComponentName(this, DeviceAdmin.class);
    mgr = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.ButtonAdmin:
        if (mgr.isAdminActive(deviceAdminComponent)) {
            mgr.lockNow();
        } else {
            Toast.makeText(getApplicationContext(), "starting", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponent);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "We recommend you to enable this");
            startActivity(intent);
        }
    }
}

}

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:

12-11 18:25:56.677: E/AndroidRuntime(32734): Process: com.example.applock, PID: 32734
12-11 18:25:56.677: E/AndroidRuntime(32734): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applock/com.example.applock.DeviceAdminCaller}: java.lang.NullPointerException

12-11 18:25:56.677: E/AndroidRuntime(32734):    at com.example.applock.DeviceAdminCaller.onCreate(DeviceAdminCaller.java:23)

12-11 18:35:08.759: E/AndroidRuntime(4409): Process: com.example.applock, PID: 4409

12-11 18:35:08.759: E/AndroidRuntime(4409): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.applock/com.example.applock.DeviceAdminCaller}: java.lang.NullPointerException

12-11 18:35:08.759: E/AndroidRuntime(4409):     at com.example.applock.DeviceAdminCaller.onCreate(DeviceAdminCaller.java:23)

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

David
  • 13
  • 3
  • Please post the **entire stack trace**. – CommonsWare Dec 11 '15 at 23:30
  • I have edited and posted it.(Logs and the updated DeviceAdminCaller class) Please have a look :) – David Dec 11 '15 at 23:41
  • What is line 23 of `DeviceAdminCaller.java`? That will be inside your `onCreate()` method. – CommonsWare Dec 11 '15 at 23:51
  • Do you mean- if (mgr.isAdminActive(deviceAdminComponent)) ??? That is in the OnClick method so won't it be called from the OnclickListener of the OnCreate method??? Your code too has this thing out of the OnCreate method. Thanks a ton btw to answer my questions :) – David Dec 12 '15 at 00:00
  • According to your stack trace, the crash is `at com.example.applock.DeviceAdminCaller.onCreate(DeviceAdminCaller.java:23)`. What is line 23 of `DeviceAdminCaller`? It should be in your `onCreate()` method. If not, then your stack trace does not match your code, and you have work to do. – CommonsWare Dec 12 '15 at 00:01
  • Yupp, I cleared that flaw. The button wasn't declared properly in the line 23. Now there's no error in logcat but the DeviceAdminScreen is not launching. – David Dec 12 '15 at 00:34

0 Answers0