Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Default app chooser dialog to appear even if the default app is already chosen already?

Tags:

android

Suppose I have a browser app,

And I'd like a Settings to be called "Make default browser" to show the dialog chooser, even though the current default browser is not my app.

How can I make I show the app chooser dialog programmatically?

Thank you.

Update Following the lead from Michal's answer, I wrote this:

String url = FinalVariables.JAVELIN_URL;
Intent i = new Intent(Intent.ACTION_VIEW);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse(url));
startActivity(Intent.createChooser(i, getString(R.string.choose_javelin)));

While it does show the chooser dialog, it does not show the "Always", "Just once" option. How can I have that option to show?

Thanks!

like image 500
nubela Avatar asked Apr 27 '14 10:04

nubela


1 Answers

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.

like image 70
Drew Avatar answered Nov 15 '22 16:11

Drew