How can I get the default application selection dialog, even if the already selected default application is already selected?

Suppose I have a browser application,

And I would like the settings to be called β€œMake default browser” to display a selection dialog, although the current default browser is not my application.

How can I make the application selection dialog box programmatic?

Thanks.

Update . Following the example of Michal, I wrote the following:

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 shows a selection dialog, it does not show the option "Always", "Only once." How do I show this parameter?

Thanks!

+8
android
source share
2 answers

There is a dirty great hack that allows you to do this. The inspiration for this hack can be found here.

Idea

is to make it an Android pattern that a new browser has just been installed, giving it a fake component with a typical browser intent filter. I will give a small test case as a proof of concept, and you decide how you can use it in your real application.

Proposed approach

It seems common and applicable in many cases (not only for launchers and browsers) and depends only on the intent filter that is supplied to the converter.

Suppose we want to override the default action to view a simple http: // link . We will declare fake browser activity next to 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 completely empty:

 public class FakeActivity extends Activity {} 

We will show the application selection by clicking the simple button found in the activity_main.xml file, and check the default behavior by clicking 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); } 

Each time you click changeDefaultButton , a selection dialog is displayed (provided that at least two suitable browser applications are installed). In addition, the selection dialog always allows the user to set the selected application by default .

References:

GitHub Evidence-Based Project

Hope this helps.

+14
source share

You can create an Intent with ACTION_CHOOSER , which is obtained using Intent.createChooser() The initial parameters and the header are passed in the method parameters. You can add more intentions to this selection by adding additional Intent.EXTRA_INITIAL_INTENTS with an array of intentions.

0
source share

All Articles