Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Device Administrator permission activity not launching

Tags:

android

I'm trying to enable my application as a device administrator. The code that attempts to call the activity which gives my app permission to be device administrator which is in the first activity of my app is as follows:

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 run my application the activity comes up, then has the animation in which it looks like it is opening a new activity, but then that animation stops and it remains on the current activity without prompting the user to enable device admin. None of my log messages pertaining to device admin are hit. However, when looking at the current device adminsitrators on the Settings->Security->Device Administrators page, my app shows up but it is unchecked as a device admin.

Here is the code for my DeviceAdmin subclass, though I can't see where the problem could be coming from 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 is as follows:

    <!-- 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>        
like image 870
tyler124 Avatar asked Feb 12 '23 04:02

tyler124


1 Answers

I guess you forgot to add the receiver in the manifest with the right 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 the 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>

For more information on Device Adminstration. Check this

like image 50
Jagadesh Seeram Avatar answered Feb 15 '23 11:02

Jagadesh Seeram