Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello World, thanks for taking the time to help me out!

In a nutshell here's my trouble. I'm using the Android Device Admin Policy Receiver to OnDisable/OnDisabledRequested methods to detect if the APP is going to be uninstalled. Since you have to disable the admin policy in settings before you can uninstall the app it serves as a way of alerting the app its about to die. When the app is running everything works fine. But, if the app is not running then the receiver doesn't pick anything up. I think my solution is to either pass the receiver object (instanced in the mainActivity) and then passed to the service or have the service open the admin activity when it's started. I think the latter is my best option. Only trouble is I cannot get the activity to start.

Here's my Service Class code:

Java
  @Override
    public void onCreate() {

   DevicePolicyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);

        DevicePolicyAdmin = new ComponentName(this,
                MyDevicePolicyReceiver.class);

        Intent intent = new Intent(
                DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(
                DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                DevicePolicyAdmin);
        intent.putExtra(
                DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                getString(R.string.admin_explanation));       
        startActivity(intent);

public static class MyDevicePolicyReceiver extends DeviceAdminReceiver {

        @Override
        public void onDisabled(Context context, Intent intent) {
            Toast.makeText(context, "",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEnabled(Context context, Intent intent) {
            Toast.makeText(context, "Truiton's Device Admin is now enabled",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public CharSequence onDisableRequested(Context context, Intent intent) {
            CharSequence disableRequestedSeq = "Requesting to disable Device Admin";
            return disableRequestedSeq;
        }}
}



Sadly this won't open the Device Admin Enable Window. It causes the screen to flash (which I think is the activity trying to start) and then it just returns to the activity from which I launched the service. Any ideas? THANKS!
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900