Prevent Android app uninstall

What I want

I want to have a simple checkbox in my settings menu, which, if installed, will be the ENABLE Device Administration for my application and will not allow my application to be deleted.

When unchecked, the checkbox will DISABLE Device Administration.

My application is security related and needs protection against deletion. Can I get a simple solution for this?

PS - I read the documentation about this, but I can not get it to work.

0
source share
2 answers

It's impossible. You cannot decide for your application to be the device administrator. You can bring the user to the appropriate place in the Settings application so that the user decides to make your application the device administrator, however through ACTION_ADD_DEVICE_ADMIN .

For example, this action will see if it is already the device administrator (via isActiveAdmin() ), then, if necessary, starts the ACTION_ADD_DEVICE_ADMIN action:

 /*** Copyright (c) 2012 CommonsWare, LLC 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. From _The Busy Coder Guide to Android Development_ http://commonsware.com/Android */ package com.commonsware.android.lockme; import android.app.Activity; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; public class LockMeNowActivity extends Activity { private DevicePolicyManager mgr=null; private ComponentName cn=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cn=new ComponentName(this, AdminReceiver.class); mgr=(DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE); } public void lockMeNow(View v) { if (mgr.isAdminActive(cn)) { mgr.lockNow(); } else { Intent intent= new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.device_admin_explanation)); startActivity(intent); } } } 

(from this sample project )

Two additional parameters ( EXTRA_DEVICE_ADMIN and EXTRA_ADD_EXPLANATION ) are optional, although they are a good idea. The first should be a ComponentName that identifies your subclass of DeviceAdminReceiver ; the second should be a string (from a string resource) that explains why the user should make your application a device administrator.

My application is security related and needs protection against deletion.

Since anyone can enter and decide that your application should not be the device administrator (again, through the "Settings"), then delete it, this is not a big part of protection.

+7
source
 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 MainActivity is as follows

 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 the manifest looks like this:

  <!-- This is where we register our receiver --> <receiver android:name=".DemoDeviceAdmin" 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_sample" /> </receiver> 
+4
source

All Articles