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 app from getting uninstalled.
The checkbox when unchecked will DISABLE Device Administration.
My app is about security and needs to be protected from getting uninstalled. Can I get a simple solution for this?
PS - I have read the documentation about this, but can't seem to get it working.
This is not possible. You cannot decide on your own to make your app be a device administrator. You are welcome to lead the user over to the appropriate spot in the Settings app to have the user elect to make your app be a device administrator, though, via
ACTION_ADD_DEVICE_ADMIN
.For example, this activity will see if it is already a device admin (via
isActiveAdmin()
), then will launch anACTION_ADD_DEVICE_ADMIN
activity if needed:(from this sample project)
The two extras (
EXTRA_DEVICE_ADMIN
andEXTRA_ADD_EXPLANATION
) are optional, though they are a good idea. The first one should be aComponentName
identifying yourDeviceAdminReceiver
subclass; the second one should be a string (from a string resource) that explains why the user should make your app be a device admin.Since anybody can go in and decide to not make your app be a device administrator (again, through Settings), then uninstall it, this is not much of a defense.
And the MainActivity goes like this
And Manifest goes like this :