Android re-enable a disabled App upon installation

2019-08-05 10:31发布

问题:

Im currently disabling an application via setApplicationEnabledSetting(String, int, int)

The application is actually disabling itself. I would expect upon re-installation of the app, the app would re-enable itself, however this isnt the case.

Is there a particular setting required in the manifest to make this work. (Ive tried setting enabled=true)

Thanks

Im currently disabling all components apart from a broadcast receiver and am trying to catch the installation process to re-enable all the other components again. Which is nasty to say the least

回答1:

One way to do this is to listen to package installation broadcasts and take action accordingly.

  • Listen to Intent.ACTION_PACKAGE_ADDED in your broadcast receiver.
  • If the newly added package is yours, enable the other components.

Example

Manifest:

<receiver android:name =".MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_ADDED"/>
        <data android:scheme="package" />
    </intent-filter>
</receiver>

Receiver:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
            final String packageName = intent.getData().getSchemeSpecificPart();
            if (replacing && "my.package.name".equals(packageName)) {
                // Re-enable the other components
            }
        }
    }

}

Hope this helps.