Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android reinstall fails after making the launcher activity disabled

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

like image 869
png Avatar asked May 01 '12 10:05

png


1 Answers

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>

like image 109
png Avatar answered Sep 25 '22 13:09

png