Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

android - Setting default browser for activity programmatically

I'm developing an android app and among other functionality I need to open some urls in external web browser. Can I programmatically set a default application for that, so the user won't be able to choose from the list of available browsers? I mean, I want to set default browser only for my app but not for the whole operating system.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, for this you can force your application to always open native android browser only. For this you have to identify the launching Activity of Browser application, something like this:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity"));
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
Uri uri = Uri.parse(url);
intent.setData(uri);
try
{
    startActivity(intent);
}
catch (Exception e)
{
   e.printStackTrace();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...