Android re-enable a disabled App upon installation of update

微笑、不失礼 提交于 2019-12-24 16:22:26

问题


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.



来源:https://stackoverflow.com/questions/19679796/android-re-enable-a-disabled-app-upon-installation-of-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!