如何使用指定浏览器打开网页

本文详细介绍了如何在Android面试中通过代码查看本机可用浏览器,并使用指定浏览器打开网页的过程。通过设置Intent,不仅实现了网页的打开,还展示了如何获取感兴趣的活动列表来了解可用的浏览器应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚刚看到一道Android面试题:如果使用制定的浏览器打开网页。
网上讲解的都比较简单,其实确实很简单,主要就是设置一下intent就可以,不过这里,我们讲解一些附带的知识。
就是查看一下本机上可用的浏览器,因为之前做过检测语音识别程序时需要检测Google 语音命令,这里简单的修改了一下就可以查看了。
具体代码如下:
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.google.com.hk");
		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");
		}
	}


}


输出结果为:
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.dolphin.browser.BrowserActivity

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值