转:指定浏览器访问指定页面(支持UC、Opera、QQ、Dolphin、Skyfire、Steel、Google)

先看一下系统浏览器com.android.browser 启动类在AndroidManifest.xml 中的声明:

 

<activity android:theme="@style/BrowserTheme" android:label="@string/application_name" android:name="BrowserActivity" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation" android:alwaysRetainTaskState="true" android:windowSoftInputMode="adjustResize">  
    <intent-filter>  
        <action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter>  
    <intent-filter>  
        <action android:name="android.intent.action.VIEW" />  
        <category android:name="android.intent.category.DEFAULT" />  
        <category android:name="android.intent.category.BROWSABLE" />  
        <data android:scheme="http" />  
        <data android:scheme="https" />  
        <data android:scheme="about" />  
        <data android:scheme="javascript" />  
    </intent-filter>  
    <intent-filter>  
        <action android:name="android.intent.action.VIEW" />  
        <category android:name="android.intent.category.BROWSABLE" />  
        <category android:name="android.intent.category.DEFAULT" />  
        <data android:scheme="http" />  
        <data android:scheme="https" />  
        <data android:scheme="inline" />  
        <data android:mimeType="text/html" />  
        <data android:mimeType="text/plain" />  
        <data android:mimeType="application/xhtml+xml" />  
        <data android:mimeType="application/vnd.wap.xhtml+xml" />  
    </intent-filter>  
    <intent-filter>  
        <action android:name="android.intent.action.MAIN" />  
        <category android:name="android.intent.category.DEFAULT" />  
        <category android:name="android.intent.category.LAUNCHER" />  
        <category android:name="android.intent.category.BROWSABLE" />  
    </intent-filter>  
    <intent-filter>  
        <action android:name="android.intent.action.WEB_SEARCH" />  
        <category android:name="android.intent.category.DEFAULT" />  
        <category android:name="android.intent.category.BROWSABLE" />  
        <data android:scheme="" />  
        <data android:scheme="http" />  
        <data android:scheme="https" />  
    </intent-filter>  
    <intent-filter>  
        <action android:name="android.intent.action.MEDIA_SEARCH" />  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter>  
    <intent-filter>  
        <action android:name="android.intent.action.SEARCH" />  
        <category android:name="android.intent.category.DEFAULT" />  
    </intent-filter>  
    <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />  
</activity>  

 action 赋值为”android.intent.action.VIEW“ 时可接收如下scheme "http" 等等类型的data 。所以突发奇想,启动该程序后,指定action Uri ,即访问指定网页。好了,立马动手实践。代码如下:

 

package lab.sodino.specifybrowser;  
import java.util.List;  
import android.app.Activity;  
import android.content.Intent;  
import android.content.pm.PackageInfo;  
import android.content.pm.PackageManager;  
import android.net.Uri;  
import android.os.Bundle;  
/** 
 * @author Sodino 
 * @version 2010年11月8日22时45分47秒 
 * */  
public class VisitUrlAct extends Activity {  
    /** 
     * 值为true时,使用传统方法让用户选择。<br/> 
     * 值为false时,程序自动为用户选定浏览器浏览。<br/> 
     * 目前支持且优先级从高到低为:<br/> 
     * 1.UC浏览器<br/> 
     * 2.Opera浏览器<br/> 
     * 3.QQ浏览器<br/> 
     * 4.Dolphin Browser(不支持WAP)<br/> 
     * 5.Skyfire Browser(不支持WAP)<br/> 
     * 6.Steel Browser(不支持WAP)<br/> 
     * 7.系统浏览器<br/> 
     * 验证是否支持直接启动访问指定页面的方法为:<br/> 
     * 执行下面的<code>doDefault()</code>时会出现如下选择框,<br/> 
     * 选择浏览器,如果能够正常访问,可以指定该浏览器访问指定网页;<br/> 
     * 如果该浏览器启动后没有跳转到指定网页,则不支持。<br/> 
     * 实践中,Go浏览器与天天浏览器并不支持。<br/> 
     * <img src="../../../../choice.png" mce_src="choice.png"/> <br/> 
     * 正确显示图片请在不改变包名的前提下将choice.png放于工程目录下,与src、res同级。<br/> 
     */  
    private boolean letUserChoice = false;  
    private String visitUrl = "http://blog.csdn.net/sodino";  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        // showUCBrowser();  
        // showQQBrowser();  
        // showOperaBrowser();  
        // showDolphinBrowser();  
        // showSkyfireBrowser();  
        // showSteelBrowser();  
        if (letUserChoice) {  
            doDefault();  
        } else {  
            choiceBrowserToVisitUrl(visitUrl);  
        }  
        // 直接退出程序  
        finish();  
    }  
    private void choiceBrowserToVisitUrl(String url) {  
        boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle = false;  
        String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", googlePath = "";  
        PackageManager packageMgr = getPackageManager();  
        List<PackageInfo> list = packageMgr.getInstalledPackages(0);  
        for (int i = 0; i < list.size(); i++) {  
            PackageInfo info = list.get(i);  
            String temp = info.packageName;  
            if (temp.equals("com.uc.browser")) {  
                // 存在UC  
                ucPath = temp;  
                existUC = true;  
            } else if (temp.equals("com.tencent.mtt")) {  
                // 存在QQ  
                qqPath = temp;  
                existQQ = true;  
            } else if (temp.equals("com.opera.mini.android")) {  
                // 存在Opera  
                operaPath = temp;  
                existOpera = true;  
            } else if (temp.equals("mobi.mgeek.TunnyBrowser")) {  
                dolphinPath = temp;  
                existDolphin = true;  
            } else if (temp.equals("com.skyfire.browser")) {  
                skyfirePath = temp;  
                existSkyfire = true;  
            } else if (temp.equals("com.kolbysoft.steel")) {  
                steelPath = temp;  
                existSteel = true;  
            } else if (temp.equals("com.android.browser")) {  
                // 存在GoogleBroser  
                googlePath = temp;  
                existGoogle = true;  
            }  
        }  
        if (existUC) {  
            gotoUrl(ucPath, url, packageMgr);  
        } else if (existOpera) {  
            gotoUrl(operaPath, url, packageMgr);  
        } else if (existQQ) {  
            gotoUrl(qqPath, url, packageMgr);  
        } else if (existDolphin) {  
            gotoUrl(dolphinPath, url, packageMgr);  
        } else if (existSkyfire) {  
            gotoUrl(skyfirePath, url, packageMgr);  
        } else if (existSteel) {  
            gotoUrl(steelPath, url, packageMgr);  
        } else if (existGoogle) {  
            gotoUrl(googlePath, url, packageMgr);  
        } else {  
            doDefault();  
        }  
    }  
    private void gotoUrl(String packageName, String url,  
            PackageManager packageMgr) {  
        try {  
            Intent intent;  
            intent = packageMgr.getLaunchIntentForPackage(packageName);  
            intent.setAction(Intent.ACTION_VIEW);  
            intent.addCategory(Intent.CATEGORY_DEFAULT);  
            intent.setData(Uri.parse(url));  
            startActivity(intent);  
        } catch (Exception e) {  
            // 在1.5及以前版本会要求catch(android.content.pm.PackageManager.NameNotFoundException)异常,该异常在1.5以后版本已取消。  
            e.printStackTrace();  
        }  
    }  
    private void doDefault() {  
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));  
        startActivity(intent);  
    }  
    /** 直接启动UC,用于验证测试。 */  
    private void showUCBrowser() {  
        Intent intent = new Intent();  
        intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");  
        intent.setAction(Intent.ACTION_VIEW);  
        intent.addCategory(Intent.CATEGORY_DEFAULT);  
        intent.setData(Uri.parse(visitUrl));  
        startActivity(intent);  
    }  
    /** 直接启动QQ,用于验证测试。 */  
    private void showQQBrowser() {  
        Intent intent = new Intent();  
        intent.setClassName("com.tencent.mtt", "com.tencent.mtt.MainActivity");  
        intent.setAction(Intent.ACTION_VIEW);  
        intent.addCategory(Intent.CATEGORY_DEFAULT);  
        intent.setData(Uri.parse(visitUrl));  
        startActivity(intent);  
    }  
    /** 直接启动Opera,用于验证测试。 */  
    private void showOperaBrowser() {  
        Intent intent = new Intent();  
        intent.setClassName("com.opera.mini.android",  
                "com.opera.mini.android.Browser");  
        intent.setAction(Intent.ACTION_VIEW);  
        intent.addCategory(Intent.CATEGORY_DEFAULT);  
        intent.setData(Uri.parse(visitUrl));  
        startActivity(intent);  
    }  
    /** 直接启动Dolphin Browser,用于验证测试。 */  
    private void showDolphinBrowser() {  
        // 方法一:  
        // Intent intent = new Intent();  
        // intent.setClassName("mobi.mgeek.TunnyBrowser",  
        // "mobi.mgeek.TunnyBrowser.BrowserActivity");  
        // intent.setAction(Intent.ACTION_VIEW);  
        // intent.addCategory(Intent.CATEGORY_DEFAULT);  
        // intent.setData(Uri.parse(visitUrl));  
        // startActivity(intent);  
        // 方法二:  
        gotoUrl("mobi.mgeek.TunnyBrowser", visitUrl, getPackageManager());  
    }  
    /** 直接启动Skyfire Browser,用于验证测试。 */  
    private void showSkyfireBrowser() {  
        // 方法一:  
        Intent intent = new Intent();  
        intent.setClassName("com.skyfire.browser",  
                "com.skyfire.browser.core.Main");  
        intent.setAction(Intent.ACTION_VIEW);  
        intent.addCategory(Intent.CATEGORY_DEFAULT);  
        intent.setData(Uri.parse(visitUrl));  
        startActivity(intent);  
        // 方法二:  
        // gotoUrl("com.skyfire.browser", visitUrl, getPackageManager());  
    }  
    /** 直接启动Steel Browser,用于验证测试。 */  
    private void showSteelBrowser() {  
        // 方法一:  
        // Intent intent = new Intent();  
        // intent.setClassName("com.kolbysoft.steel",  
        // "com.kolbysoft.steel.Steel");  
        // intent.setAction(Intent.ACTION_VIEW);  
        // intent.addCategory(Intent.CATEGORY_DEFAULT);  
        // intent.setData(Uri.parse(visitUrl));  
        // startActivity(intent);  
        // 方法二:  
        gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());  
    }  
}

 ok,成功了。

附choice.png图片:


转自:http://blog.csdn.net/sodino/archive/2010/11/08/5996490.aspx

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值