Android: recognized as an App or Activity that can

2019-09-08 16:11发布

When installed more than one Browser and the default is not set, I will get the chooser dialog with the possibility to set the default.

How does an application (or Activity) made itself recognizable by the system as a web browser. If I do something like this:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
startActivity(Intent.createChooser(intent, "TEST"));

I'll get a list of the apps: Browser (google), Contacts, Gmail, Phone, but not the Opera (mini) browser. So, Opera has no category Browsable but is still picked up by Android as a web browser. How does this work?

2条回答
ら.Afraid
2楼-- · 2019-09-08 16:22
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);

will bring up the browser chooser including the default checkbox. The data needs to be something of "http:" or "https:"-type.

Selecting an item in the dialog of course will open the browser going to the specified URL. And that is actually the case when at the base Home app when clicking to the browser icon.

This is not 100% what I hoped (100% would be open a browser without going to the URL), but acceptable.

查看更多
贼婆χ
3楼-- · 2019-09-08 16:40

It's achieved by adding appropriate <action> to <intent-filter> in your manifest file, so that Android knows what actions your app can perform and intents it can respond to.

查看更多
登录 后发表回答