0

I want to build app like parental control, so when child try to uninstall/remove my app I would like to require that a user type a password before being allowed to uninstall/remove my application.

i try this, but still don't understand :
Require a password to uninstall/remove application

Any suggest?

Community
  • 1
  • 1
virho
  • 149
  • 2
  • 2
  • 9

1 Answers1

5

You can lock the device if you use device administration. Users can't uninstall active device admins, then you can lock the device if they try to disable device admin, then the parent could type in the password to unlock it.

Warning: This could be considered malicious, if your user isn't VERY well informed of how this will work. Review the Terms & Conditions for whatever app store you're releasing on, in case this behavior isn't allowed.

In your manifest:

  <receiver android:name=".AdminReceiver"
        android:label="Administration"
        android:description="@string/descript"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data android:name="android.app.device_admin"
                       android:resource="@xml/deviceadmin" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>
        </receiver>

Then in @xml/deviceadmin

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
    <reset-password />
    <force-lock />
  </uses-policies>
</device-admin>

Then

public class AdminReceiver extends DeviceAdminReceiver {
@Override
        public CharSequence onDisableRequested(final Context context, Intent intent) {
            
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(startMain); //switch to the home screen, not totally necessary
            lockPhone(context, secPassword);
            //Log.i(TAG, "DEVICE ADMINISTRATION DISABLE REQUESTED & LOCKED PHONE");
            
            return "haha. i locked your phone.";
        }
    public static boolean lockPhone(Context context, String password){
        devAdminReceiver = new ComponentName(context, AdminReceiver.class);
        dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        boolean pwChange = dpm.resetPassword(password, 0);
        dpm.lockNow();
        return pwChange;
    }   
}

To enable your app as a device administrator:

devAdminReceiver = new ComponentName(context, AdminReceiver.class);
        dpm = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        pref = PreferenceManager.getDefaultSharedPreferences(context);
        dpm.isAdminActive(devAdminReceiver);
Reed
  • 14,703
  • 8
  • 66
  • 110
  • sorry, but i really confuse. Where i create `@xml/deviceadmin`, i mean in manifest or layout? please . . Help me – virho Jul 29 '14 at 05:46
  • Create a folder named `xml` in `res`, then create a file named `deviceadmin.xml` – Reed Jul 29 '14 at 17:49
  • how it works?, sorry but i still confused. when user try to uninstall this app, and then dialog box will show up to ask password. Like that?? – virho Jul 29 '14 at 18:15
  • No. 1) Child tries to uninstall and it says "You must disable device admin to uninstall." 2) Child goes to Security > Device Administrators and chooses to disable Device Administration for your app. 3) The phone automatically locks to the device's regular lock screen (with a password). 4) Child has to get parent to enter the password. – Reed Jul 30 '14 at 16:04
  • i have two class now, MainActivity and AdminReceiver. In adminReceiver the cotents as you wrote above. So, how to call it in MainActivity?? – virho Jul 31 '14 at 07:29
  • The last block of code is how you enable Device Administration. Alternatively, you can go to Security > Device Administrators and enable it from there. If you were still able to uninstall the app, you either did something wrong OR did not enable Device Administration. – Reed Jul 31 '14 at 15:19
  • you have some example in Android Project or apk? Please help me, i really need it – virho Jul 31 '14 at 15:40
  • 1
    No I don't. For the sake of problem solving/learning, I would suggest you take what I gave you and try your best to make it work. If you try to implement it all and you're getting errors or a specific problem with it and you can't figure it out, then you can post a new question on SO. Good luck. – Reed Jul 31 '14 at 16:14
  • owhh, Ok . I will post a new question later. Thank you very much . . – virho Aug 01 '14 at 03:18
  • A reader has tried to get in touch with you to ask about your solution here, Jakar (via an answer on this page, now mod-deleted). Would you have [a read](http://stackoverflow.com/q/32529633/472495)? – halfer Sep 12 '15 at 10:43