Prevent app from Uninstall in Android

后端 未结 2 713
野趣味
野趣味 2020-12-15 13:48

What I Want

I want to have a simple checkbox in my settings menu, which if checked will ENABLE Device Administration for my app and will prevent my

2条回答
  •  情书的邮戳
    2020-12-15 14:12

    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 DeviceAdminReceiver class here and to implement its only required
     * method onEnabled().
     */
    public class DemoDeviceAdmin extends DeviceAdminReceiver {
        static final String TAG = "DemoDeviceAdmin";
    
        /** 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, R.string.device_admin_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, R.string.device_admin_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");
        }
    
    }
    

    And the MainActivity goes like this

    devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
            demoDeviceAdmin = new ComponentName(this, DemoDeviceAdmin.class);
            Log.e("DeviceAdminActive==", "" + demoDeviceAdmin);
    
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);// adds new device administrator
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, demoDeviceAdmin);//ComponentName of the administrator component.
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "Disable app");//dditional explanation
            startActivityForResult(intent, ACTIVATION_REQUEST);
    

    And Manifest goes like this :

     
            
                
    
                    
                    
                
    
                
                
            
    

提交回复
热议问题