Android程序中如何启动浏览器

本文参照自:http://blog.csdn.net/liws2010/article/details/7452957
一、启动android默认浏览器
在Android程序中我们可以通过发送隐式Intent来启动系统默认的浏览器。如果手机本身安装了多个浏览器而又没有设置默认浏览器的话,系统将让用户选择使用哪个浏览器来打开连接。关于Intent的更多内容请参考《常用Intent》
示例1

     Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        Uri content_url = Uri.parse("http://www.163.com");
        intent.setData(content_url);
        startActivity(intent);

这样子,android就可以调用起手机默认的浏览器访问。
二、启动指定浏览器
在Android程序中我们可以通过发送显式Intent来启动指定的浏览器。
启动Android原生浏览器
示例2

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

只要修改以intent.setClassName(“com.android.browser”,”com.android.browser.BrowserActivity”);
中相应的应用程序packagename 和要启动的activity即可启动其他浏览器来
uc浏览器”:”com.uc.browser”, “com.uc.browser.ActivityUpdate“
opera浏览器:”com.opera.mini.android”, “com.opera.mini.android.Browser”
qq浏览器:”com.tencent.mtt”, “com.tencent.mtt.MainActivity”
三、使用浏览器打开本地html文件
打开本地的html文件的时候,一定要指定某个浏览器,而不能采用方式一来浏览。
示例3

      Intent intent = new Intent();        
         intent.setAction("android.intent.action.VIEW");    
         Uri content_url = Uri.parse("file:///sdcard/robin.hu/help.html");   
         intent.setData(content_url);           
         intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");   
         startActivity(intent);

虽然网上很多文章说即使使用浏览器打开本地的html文件的时,文件路径不支持”files:”,不应该使用

Uri.parse("file:///sdcard/robin.hu/help.html");  

这种形式而是应该使用

Uri content_url = Uri.parse("content://com.Android.htmlfileprovider/sdcard/robin.hu/help.html");

但是经过在HTC G10(Android2.3.5),Lenovo P770(Android4.0以上)测试,实际上只有

Uri.parse("file:///sdcard/robin.hu/help.html");

这种形式能很好的工作。

Uri content_url = Uri.parse("content://com.android.htmlfileprovider/sdcard/robin.hu/help.html");

这种形式反而不能很好的工作
四、对使用浏览器打开网页的封装
以下是对使用浏览器打开网页的一些封装

static void startBrower(Context context,String uri)
    {
        startBrower(context,Uri.parse(uri));
    }
    static void startBrower(Context context,Uri uri)
    {
         Intent intent = new Intent();
         intent.setAction("android.intent.action.VIEW");
         /*其实可以不用添加该Category*/
         intent.addCategory("android.intent.category.BROWSABLE");
         intent.setData(uri); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         /*如果想用浏览器打开本地html文件的话,则只能通过显式intent启动浏览器*/
         boolean explicitMode=false;
         String scheme=uri.getScheme();
         if(scheme!=null&&scheme.startsWith("file")) {
             explicitMode=true;
         }
         if(explicitMode) {
             intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
         } else {
             intent.addCategory("android.intent.category.BROWSABLE");
         }
         context.startActivity(intent);
    }
    static void startBrower(Context context,String uri,String packageName, String className)
    {
        startBrower(context,Uri.parse(uri),packageName,className);
    }
    static void startBrower(Context context,Uri uri,String packageName, String className)
    {
         Intent intent = new Intent();
         intent.setAction("android.intent.action.VIEW");
         intent.setData(uri); 
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.setClassName(packageName,className);
         context.startActivity(intent);
    }

五、Android原生浏览器的AndroidManifest.xml
以下是Android 4.0原生浏览器的AndroidManifest.xml文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.browser">
    <original-package android:name="com.android.browser" />
    <permission android:name="com.android.browser.permission.PRELOAD"
        android:label="@string/permission_preload_label"
        android:protectionLevel="signatureOrSystem" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_DOWNLOAD_MANAGER"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.NFC" />
    <uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
    <uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>
    <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
    <application   android:name="Browser"
                   android:label="@string/application_name"
                   android:icon="@mipmap/ic_launcher_browser"
                   android:backupAgent=".BrowserBackupAgent"
                   android:hardwareAccelerated="true"
                   android:taskAffinity="android.task.browser" >
        <provider android:name=".provider.BrowserProvider2"
                  android:authorities="com.android.browser;browser"
                  android:multiprocess="true"
                  android:readPermission="com.android.browser.permission.READ_HISTORY_BOOKMARKS"
                  android:writePermission="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS">
            <path-permission android:path="/bookmarks/search_suggest_query"
                    android:readPermission="android.permission.GLOBAL_SEARCH" />
        </provider>
        <activity android:name="BrowserActivity"
                  android:label="@string/application_name"
                  android:launchMode="singleTask"
                  android:alwaysRetainTaskState="true"
                  android:configChanges="orientation|keyboardHidden|keyboard|screenSize"
                  android:theme="@style/BrowserTheme"
                  android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <!-- For these schemes were not particular MIME type has been
                 supplied, we are a good candidate. -->
            <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>
            <!--  For these schemes where any of these particular MIME types
                  have been supplied, we are a good candidate. -->
            <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>
            <!-- For viewing saved web archives. -->
            <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="file" />
                <data android:mimeType="application/x-webarchive-xml"/>
            </intent-filter>
            <!-- Accept inbound NFC URLs at a low priority -->
            <intent-filter android:priority="-101">
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http" />
                <data android:scheme="https" />
            </intent-filter>
            <!-- We are also the main entry point of the browser. -->
            <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>
            <!-- The maps app is a much better experience, so it's not
                 worth having this at all... especially for a demo!
            <intent-filter android:label="Map In Browser">
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.item/postal-address" />
            </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>
        <activity android:name="ShortcutActivity"
            android:theme="@style/ShortcutTheme"
            android:label="@string/shortcut_bookmark"
            android:icon="@mipmap/ic_launcher_shortcut_browser_bookmark">
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="BrowserPreferencesPage" android:label="@string/menu_preferences">
            <intent-filter>
               <action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
               <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="BookmarkSearch"
                  android:label="@string/bookmarks_search"
                  android:stateNotNeeded="true"
                  android:theme="@android:style/Theme.NoDisplay"
                  android:excludeFromRecents="true">
            <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/bookmarks_searchable" />
        </activity>
        <activity android:name="AddBookmarkPage" android:label="Save bookmark"
                  android:theme="@style/DialogWhenLarge"
                  android:configChanges="orientation|keyboardHidden|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.INSERT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="vnd.android.cursor.dir/bookmark"/>
            </intent-filter>
        </activity>

        <activity android:name="ComboViewActivity">
        </activity>
        <!-- Bookmark thumbnail homescreen widget -->
        <receiver
            android:name=".widget.BookmarkThumbnailWidgetProvider"
            android:label="@string/bookmarks">
            <intent-filter>
                <action
                    android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action
                    android:name="com.android.browser.BOOKMARK_APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/bookmarkthumbnailwidget_info" />
        </receiver>
        <service
            android:name=".widget.BookmarkThumbnailWidgetService"
            android:permission="android.permission.BIND_REMOTEVIEWS"
            android:exported="false" />
        <receiver
            android:name=".widget.BookmarkWidgetProxy"
            android:exported="false" />
        <activity android:name=".widget.BookmarkWidgetConfigure"
            android:theme="@android:style/Theme.Holo.DialogWhenLarge">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
            </intent-filter>
        </activity>
        <!-- Makes .BrowserActivity the search target for any activity in Browser -->
        <meta-data android:name="android.app.default_searchable" android:value=".BrowserActivity" />
        <!-- Application code for RLZ tracking.  RLZ assigns non-unique, non-personally identifiable
             tracking labels to client products; these labels sometimes appear in Google search
             queries.  See http://code.google.com/p/rlz for more info.
             This value signifies to the RLZ client that this application uses RLZ tracking. -->
        <meta-data android:name="com.google.android.partnersetup.RLZ_ACCESS_POINT"
                   android:value="@string/rlz_access_point" />
        <receiver android:name=".OpenDownloadReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
            </intent-filter>
        </receiver>
        <!-- For custom home pages (like most visited) -->
        <provider
            android:name=".homepages.HomeProvider"
            android:authorities="com.android.browser.home"
            android:readPermission="com.android.browser.permission.READ_HISTORY_BOOKMARKS"
            android:exported="false" />
        <receiver android:name=".AccountsChangedReceiver">
            <intent-filter>
                <action android:name="android.accounts.LOGIN_ACCOUNTS_CHANGED" />
            </intent-filter>
        </receiver>
        <receiver android:name=".PreloadRequestReceiver"
             android:permission="com.android.browser.permission.PRELOAD" >
             <intent-filter>
                 <action android:name="android.intent.action.PRELOAD"/>
                 <data android:scheme="http" />
             </intent-filter>
         </receiver>
        <provider android:name=".provider.SnapshotProvider"
                  android:authorities="com.android.browser.snapshots"
                  android:exported="false" />
    </application>
</manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值