I want to remove my app from list of apps and the recent apps list. So I tried to disable my main / launcher activity with the following code:
ComponentName componentToDisable = new ComponentName(context, MainActivity.class);
context.getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
This does the job. But now I try to reinstall the app and it fails saying "activity MainActivity class doesn't exist". If I uninstall the app, the installation works again. How can I handle this issue? Thanks a lot for your time and help
I found that I have to make the activity enabled before reinstalling it. This can be done by having a broadcast receiver listen to package_add / remove events and in onReceive make the activity enabled again.
public void onReceive(Context context, Intent intent) {
Log.i("Receiver","got event");
ComponentName componentToDisable = new ComponentName(context,BlockableComponentActivity.class);
context.getPackageManager().setComponentEnabledSetting(componentToDisable,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
Manifest excerpt for the receiver:
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With