如何使用指定浏览器打开网页
刚刚看到一道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.zzzyk.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");
}
}
}
输出结果为:
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
摘自 BossDarcy的专栏执
补充:移动开发 , Android ,