15

I am trying to make my application device administrator , i followed some notes provided on the same at Device admin SAMPLE notes or TUTORIAL here but still could not implement it.

Can some one plz point out a working example of the same , i just need to make sure that to uninstall the application , user needs to enter the password which is set in my application.

Any help will be useful , Thanks in advance

rolling.stones
  • 496
  • 3
  • 9
  • 20

3 Answers3

8

i just need to make sure that to uninstall the application , user needs to enter the password which is set in my application.

Fortunately, this is not possible for an SDK application. Even a device admin app cannot prevent the user from uninstalling it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Cerberus app makes it. The app is device administrator, if the user does not check an option in the menu he will not be able to uninstall the app. How to do this? – Gabriel Augusto Apr 23 '13 at 14:08
  • @GabrielAugusto: Thank you for pointing out an Android security flaw. I will try to confirm your findings, then will work with the Android Security team to have this flaw corrected. – CommonsWare Apr 23 '13 at 14:29
  • @CommonsWare: You have mentioned that it is not possible for **SDK** apps. So, is it possible for system apps? Can I create a system app which is password protected and user cannot uninstall it? – user1010 Jun 08 '13 at 14:16
  • @user1010: If it is a system app, the user cannot uninstall it, period, unless they have root. – CommonsWare Jun 08 '13 at 14:30
  • oh yes, how can I forget that. – user1010 Jun 08 '13 at 15:27
  • @CommonsWare : I do not think it's a security flaw. An activated device admin app cannot be uninstalled unless the device-administrator is deactivated. – Kiran Parmar Oct 24 '13 at 06:39
  • I'm a little confused here. This is an accepted answer, but other posts on this site claim the opposite and provide evidence for it. So is this possible or not? – Jolinar Oct 21 '16 at 09:21
  • @Jolinar: There is nothing in the Android SDK to support this. Malware authors found some ways around it. Those ways have been blocked in newer versions of Android. – CommonsWare Oct 21 '16 at 11:01
3

I found the sample tutorial here. I hope it will help you.

Warning :::: If you click "Reset Device", your device will be factory reset because of

devicePolicyManager.wipeData(ACTIVATION_REQUEST);
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
Htoo Aung Hlaing
  • 2,173
  • 15
  • 26
1

You cannot activate it programmatically but you can ask user to enable it at runtime Here is the complete solution

 var devicePolicyManager: DevicePolicyManager? = null
    var deviceAdmin: ComponentName? = null

call this method in oncreate where ever it is needed

// call this method to check if its enabled or not

checkAndTakeUserToEnableEnableAdminApp()



private fun checkAndTakeUserToEnableEnableAdminApp() {
        devicePolicyManager = getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        deviceAdmin = ComponentName(this, DeviceAdminReceiverClass::class.java)
        if (devicePolicyManager!!.isAdminActive(deviceAdmin!!)) {
        //do whatever is needed here is its active
        } else {
            showDeviceAdminPopup(this)
        }

    }

Where DeviceAdminReceiverClass

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;



/**
 * This is the component that is responsible for actual device administration.
 * It becomes the receiver when a policy is applied. It is important that we
 * subclass DeviceAdminReceiverClass class here and to implement its only required
 * method onEnabled().
 */
public class DeviceAdminReceiverClass extends DeviceAdminReceiver {
    static final String TAG = "DeviceAdminReceiverClass";

    /** Called when this application is approved to be a device administrator. */
    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Toast.makeText(context, "Device admin is enabled",
                Toast.LENGTH_LONG).show();
        Log.d(TAG, "onEnabled");
    }

    /** Called when this application is no longer the device administrator. */
    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Toast.makeText(context, "Device admin is disabled",
                Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.d(TAG, "onPasswordChanged");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
        Log.d(TAG, "onPasswordFailed");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
        Log.d(TAG, "onPasswordSucceeded");
    }

}

manifest

<receiver
            android:name=".DeviceAdminReceiverClass"
            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>

@xml/device_admin

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<device-admin xmlns:android="http://schemas.android.com/apk/res/android"
    android:visible="false">
    <uses-policies>
        <watch-login />
        <force-lock />
    </uses-policies>
</device-admin>
Quick learner
  • 10,632
  • 4
  • 45
  • 55