In my app I want to change the icon of the app based on the notification received. i.e if there are 2 notifications received, then the app icon having "2" in it will be shown in home screen of the device.
For this I have used activity alias. I have added following intent filters for the activity alias:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
And to make a activity-alias active I have used following code:
This is to enable the activity alias:
ComponentName componentName = new ComponentName("PACKAGE_NAME_HERE",
lastEnabled);
pm.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Utility.setLastEnabled(lastEnabled,context);
This will disable previously enabled activity alias
componentName = new ComponentName(
"PACKAGE_NAME_HERE", prevLastEnabled);
pm.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
But as soon the app icon changes, my app closes without any exception. I have used dont kill app here, still it closes the app. Can anyone please suggest me if I am doing anything wrong.Thanks.
Your Activity is being closed probably because you have enabled/disabled your target Activity. Note that the App (Application Process) has not been killed. Anyways this is what I did to stop the Activity from closing:
In general you want to do the following:
Your AndroidManifest.xml should look something like this:
<activity android:name=".MainActivity" ... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity-alias android:name=".No-Icon-Badge"
android:targetActivity=".MainActivity"
android:enabled="true"
... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias android:name=".Icon-Badge-1"
android:targetActivity=".MainActivity"
android:enabled="false"
... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
<activity-alias android:name=".Icon-Badge-2"
android:targetActivity=".MainActivity"
android:enabled="false"
... >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity-alias>
...
So your MainActivity is enabled (keep it that way), but has no LAUNCHER category so it won't show up. Also the first activity-alias is enabled (this is your default) and all the other ones are disabled.
Good luck!
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