There is a dirty nifty hack that allows you to do that.
The inspiration for this hack could be found here
The idea
is to make android think that a new browser has just been installed, feeding it a fake component with a typical browser intent-filter.
I shall provide a little test case as a proof-of-concept, and it is up to you to decide how you could use it in your real-world application.
The proposed approach
seems to be general and applicable in plenty of cases (not only for launchers and browsers) and depends only on intent filter that is being fed to a resolver.
Lets assume that we want to override a default Activity for browsing simple http:// link
We shall declare a fake browser activity alongside the real ones in AndroidManifest.xml:
<activity
android:name=".FakeActivity"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http" />
</intent-filter>
</activity>
FakeActivity is absolutely blank:
public class FakeActivity extends Activity {}
We'll show app chooser by pressing a simple button found in activity_main.xml and test the default behaviour by pressing another button:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View changeDefaultButton = findViewById(R.id.changeDefButton);
changeDefaultButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showChooser();
}
});
View testDefaultButton = findViewById(R.id.testDefaultButton);
testDefaultButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
runDefaultApp();
}
});
}
void showChooser(){
PackageManager pm = getPackageManager();
ComponentName cm = new ComponentName(this, FakeActivity.class);
pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
runDefaultApp();
pm.setComponentEnabledSetting(cm, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
void runDefaultApp(){
Intent selector = new Intent(Intent.ACTION_VIEW);
selector.setData(Uri.parse("http://stackoverflow.com"));
startActivity(selector);
}
Every time you click on changeDefaultButton
, the chooser dialog is shown (assuming there are at least two suitable browser apps installed). Also, the chooser dialog always allows user to set the chosen application as default.
Links:
GitHub proof-of-concept project
Hope this helps.