6

I want to make the app which is similar to AppLock in android.app lock has a advance protection feature which stops uninstalling the application.It works with out rooting the phone. I tried hours together and tried so many solutions of similar kind of question from stackoverflow but could not make through it.

while doing this I came across "device admin rights".

  1. can I do thing which I want to do using admin rights.

  2. if not how AppLock advance protection works i mean how it restricting the user to uninstall the application.

N J
  • 27,217
  • 13
  • 76
  • 96
Tirupati Rao
  • 615
  • 6
  • 24
  • please check this link http://stackoverflow.com/a/28795022/4927900 – piyush poriya Feb 10 '16 at 07:46
  • Hi, thank you for the reply.Ya I tried but how can I restrict the user to uninstall the apps.I mean if possible, either i can desable the uninstall button or I can prompt a password screen when he/she opens app setting menu .But device admin receiver receiving the broadcast msg after uninstalling the app. – Tirupati Rao Feb 11 '16 at 06:29
  • is there any way to get the broadcast msg before uninstalling the app ? – Tirupati Rao Feb 12 '16 at 16:16

2 Answers2

1

You need to make your app as device owner-it has higher privileges than the device admin app.

For more info refer-http://florent-dupont.blogspot.com/2015/02/10-things-to-know-about-device-owner.html and here https://source.android.com/devices/tech/admin/provision.html

You can set a policy using the admins/owner app which will prevent uninstallation.

You can also have a look at MDM solution provided by the OEMs. Samsung has very powerful APIs to manage every aspect of the device-https://www.samsungknox.com/en/partners/mobile-device-managers

rupesh jain
  • 3,410
  • 1
  • 14
  • 22
  • Hi.I have gone through the links which you have given.Ya device admin have high privileges so, can i achieve what I want to do if my app is device admin." and I have one question "There can be only one Device Owner App per device…" ? what does it mean ? I have installed mutliple apps in my phone which has same feature (block user to uninstall).If so then applocker is not implemented using this concept . am I correct ? – Tirupati Rao Feb 11 '16 at 19:54
  • @TirupatiRao device owner has more previledges than device admin..you can have only one device owner but multiple profile owners for a device – rupesh jain Feb 11 '16 at 22:22
  • Hi thankyou, ya you are right I read that point in the documentation but how admin privileges solves my problem of preventing the uninstallation? – Tirupati Rao Feb 12 '16 at 16:15
  • @TirupatiRao You can set a policy using the admins/owner app which will prevent uninstallation – rupesh jain Feb 12 '16 at 17:35
  • .what policies should i set so that user cannot uninstall the apps ? – Tirupati Rao Feb 13 '16 at 06:18
0

The solution is to use: Device Administration API

The device administration API is specially designed for enterprise Applications. And this API provides administration features at the system level.

so how does it work?

You use the Device Administration API to write device admin applications that users install on their devices. The device admin application enforces the desired policies. Here's how it works:

If users do not enable the device admin app, it remains on the device, but in an inactive state. Users will not be subject to its policies, and they will conversely not get any of the application's benefits—for example, they may not be able to sync data.

If a user fails to comply with the policies (for example, if a user sets a password that violates the guidelines), it is up to the application to decide how to handle this. However, typically this will result in the user not being able to sync data.

If a device attempts to connect to a server that requires policies not supported in the Device Administration API, the connection will not be allowed. The Device Administration API does not currently allow partial provisioning. In other words, if a device (for example, a legacy device) does not support all of the stated policies, there is no way to allow the device to connect.

If a device contains multiple enabled admin applications, the strictest policy is enforced. There is no way to target a particular admin application.

To uninstall an existing device admin application, users need to first unregister the application as an administrator.

Here is a sample code that implements the same:

    import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.util.Log;
import android.view.Menu;

public class ActiveDevicePolicy extends Activity {
 private DevicePolicyManager activeDevicePolicyManager;
 private final String LOG_TAG = "ActiveDevicePolicy";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_active_device_policy);
 activeDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
 List<ComponentName> activeAdmins = activeDevicePolicyManager.getActiveAdmins();
 if(activeAdmins != null && !activeAdmins.isEmpty()){
 for(int index = 0; index < activeAdmins.size(); index++ ){
 Log.i(LOG_TAG, "flattenToShortString: "+ activeAdmins.get(index).flattenToShortString());
 Log.i(LOG_TAG, "flattenToString: "+ activeAdmins.get(index).flattenToString());
 Log.i(LOG_TAG, "getClassName: "+ activeAdmins.get(index).getClassName());
 Log.i(LOG_TAG, "getPackageName: "+ activeAdmins.get(index).getPackageName());
 Log.i(LOG_TAG, "getShortClassName: "+ activeAdmins.get(index).getShortClassName());
 Log.i(LOG_TAG, "toShortString: "+ activeAdmins.get(index).toShortString()); 
 }
 } else {
 Log.i(LOG_TAG, "No Active Device Policy Manager");
 }
 }

}

In the above code I have called DevicePolicyManager.getActiveAdmins() method to retrieve the list of all the admins that are active at the moment. Now the interesting part here is that, since its returning a list; there could be more than one device admins in that list. So the strictest policy remains active!

Hope it helps!

OBX
  • 6,044
  • 7
  • 33
  • 77
  • Hi.I already read this here http://developer.android.com/guide/topics/admin/device-admin.html how does it helps me ? I mean how to block the uninstallation ? – Tirupati Rao Feb 13 '16 at 06:16