Implementation of android Using Browser to Open Specified Page

  • 2021-09-12 02:06:51
  • OfStack

Previously, when I did 1 function, the server returned 1 url address, and I needed to jump to the specified web page. But I found that I didn't know how to do it. I searched the information on the Internet and finally solved the problem. Record 1 here.

Start the android default browser

In the Android program, we can start the default browser by sending an implicit Intent. If the phone itself has more than one browser installed and no default browser is set, the system will let the user choose which browser to use to open the connection.


Uri uri = Uri.parse("https://www.baidu.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
startActivity(intent); 

Start the specified browser to open

In the Android program, we can launch the specified browser by sending an explicit Intent. For example, my mobile phone has several browsers installed: QQ browser, chrome browser and uc browser. I can specify a browser to open this link. For example, open the QQ browser as follows:


Uri uri = Uri.parse("https://www.baidu.com"); 
Intent intent = new Intent(Intent.ACTION_VIEW,uri); 
//intent.setClassName("com.UCMobile","com.uc.browser.InnerUCMobile");// Open UC Browser  
intent.setClassName("com.tencent.mtt","com.tencent.mtt.MainActivity");// Open QQ Browser  
startActivity(intent); 

To open with uc browser, just comment out the line of code that opens qq browser, and then open uc browser that line of code to cancel the gaze.

You can also specify the browser that comes with the system:


Intent intent = new Intent(); 
intent.setAction("android.intent.action.VIEW"); 
Uri content_url = Uri.parse("http://www.baidu.com");  
intent.setData(content_url); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity"); 
mContext.startActivity(intent); 

Preferential use

It is recommended to use the first one, so that users can choose which browser to open it with. The second type will be used unless there are special needs.

The second kind of error rate is relatively high. If you want to open it with uc browser, but the new version of uc browser does not use the original package name, you can't open it at this time. There is also a compatibility problem with uc browser. I will only display the homepage of UC instead of directly opening the http link provided by me. But QQ browser does not have this problem.

To put it simply, in fact, using a browser to open a web page means jumping to a browser with intent, setting Action of intent as Intent.ACTION_VIEW, and then setting Data of intent as URI of the website address, you can use the browser of the system to open a web page. If you need to jump to a specific browser, just set setClassName for the specific browser package name and activity name, the specific code is written in detail.

You can also use WebView to open web pages here, which is also very simple.

1. Customize a simple WebView browser and set the following properties:


mWebView = (ProgressWebView) findViewById(R.id.baseweb_webview); 
mWebView.getSettings().setJavaScriptEnabled(true); 
mWebView.setWebViewClient(new WebViewClient()); 

2. Specify the amount page to be opened and open it in the custom WebViewActivity, such as:


WebView myWebView = (WebView) findViewById(R.id.webview); 
myWebView.loadUrl(<a href="http://www.hao123.com" target="_blank">http://www.hao123.com</a>); 

Related articles: