How to reset default app programmatically, which user previously chooses as open Always? Expected end result is to show the app chooser again, when user view the file next time.
Thanks.
How to reset default app programmatically, which user previously chooses as open Always? Expected end result is to show the app chooser again, when user view the file next time.
Thanks.
By default, Android does not allow that. Security reasons and so on.
However, there is a loophole - The default reset every time the system recognizes a new component was added which can handle the given intent.
So, you will need to do something like this:
public void resetDefault() {
PackageManager manager = getPackageManager();
ComponentName component = new ComponentName("com.example.app", "com.mypackage.Component");
manager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
manager.setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
com.mypackage.Component is a dummy Activity
, which you have in your manifest and which is not enabled by default. It needs to have an IntentFilter
with the Intent you want to be default app of, like this:
<activity
android:name="Component"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.SOME_ACTION" />
<category android:name="android.intent.category.SOME_CATEGORY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The method will enable the dummy component and the system will be notified that a new possible receiver was added (the same happens when you install a new launcher for example) and will reset defaults. The the code will disable the dummy so it does not show in the chooser.
You will probably want to check if your app is default or not, and if there is a default app at all. See this answer for details on how to do that.