How to open a webpage using a specified Browser

Source: Internet
Author: User

I just saw an Android interview question: if you use a browser to open the webpage.
All the explanations on the Internet are simple. Actually, they are very simple. You can set intent. However, here we will explain some additional knowledge.
Check the available browsers on the local machine. Because Google Voice commands need to be detected when detecting the speech recognition program, you can simply modify it here.
The Code is as follows:

Package com. google. code. cakedroid. demo;
 
 
Import java. util. List;
 
 
Import com. google. code. cakedroid. R;
 
 
Import android. app. Activity;
Import android. content. Intent;
Import android. content. pm. PackageManager;
Import android. content. pm. ResolveInfo;
Import android.net. Uri;
Import android. OS. Bundle;
 
 
Public class BrowserDemo extends Activity {
 
 
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Get the view web intent
Intent intent = this. getViewWebIntent ();
This. printInterestedActivitiesByIntent (intent );
// Set the className to use the specific browser to open the webpage.
Intent. setClassName ("com. tencent. mtt", "com. tencent. mtt. MainActivity ");
StartActivity (intent );
}
 
 
/*
* Get the desired view web intent
*/
Private Intent getViewWebIntent (){
Intent viewWebIntent = new Intent (Intent. ACTION_VIEW );
Uri uri = Uri. parse ("http://www.bkjia.com ");

ViewWebIntent. setData (uri );
Return viewWebIntent;
}

/*
* Print the activities that are interested about the intent
*/
Private void printInterestedActivitiesByIntent (Intent intent ){
PackageManager pm = this. getPackageManager ();
List <ResolveInfo> activities = pm. queryIntentActivities (intent, 0 );
If (null! = Activities ){
For (int I = 0; I <activities. size (); I ++ ){
ResolveInfo info = activities. get (I );
System. out. println (info. activityInfo. name );
}
} Else {
System. out. println ("no interested activities ");
}
}
 
 
}


Output result:
12-17 05:02:30. 096: I/System. out (217): com. android. browser. BrowserActivity
12-17 05:02:30. 096: I/System. out (217): com. tencent. mtt. MainActivity
12-17 05:02:30. 096: I/System. out (217): cn. doldolphin. browser. BrowserActivity

Column Excerpt from BossDarcy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.