Android 啟動瀏覽器的方法

來源:互聯網
上載者:User

標籤:android

[java] view plaincopyprint?
  1.   Intent intent = new Intent();          
  2. intent.setAction("android.intent.action.VIEW");      
  3. Uri url = Uri.parse("http://www.baidu.com");     
  4. intent.setData(url);    
  5. startActivity(intent);  

  

  
指定相應的瀏覽器訪問

  1、指定android內建的瀏覽器訪問

[java] view plaincopyprint?
  1. (“com.android.browser”:packagename;“com.android.browser.BrowserActivity”:啟動主activity)  
  2.   Intent intent = new Intent();          
  3.   intent.setAction("android.intent.action.VIEW");      
  4.   Uri content_url = Uri.parse("http://www.cnblogs.com");     
  5.   intent.setData(content_url);             
  6.   intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");   startActivity(intent);  


  2、啟動其他瀏覽器(當然該瀏覽器必須安裝在機器上)
  只要修改以下相應的packagename 和 主啟動activity即可調用其他瀏覽器

  intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");

  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檔案的時候,一定要指定某個瀏覽器,而不能採用方式一來瀏覽,具體範例程式碼如下

  

[java] view plaincopyprint?
  1. Intent intent = new Intent();  
  2.   intent.setAction("android.intent.action.VIEW");      
  3.   Uri content_url = Uri.parse("content://com.android.htmlfileprovider/sdcard/help.html");     
  4.   intent.setData(content_url);             
  5.   intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");     
  6.   startActivity(intent);  

  關鍵點是調用了”content“這個filter。
  以前有在win32編程的朋友,可能會覺得用這種形式”file://sccard/help.html“是否可以,可以很肯定的跟你說,預設的瀏覽器設定是沒有對”file“這個進行解析的,如果要讓你的預設android瀏覽器有這個功能需要自己到android源碼修改manifest.xml檔案,然後自己編譯瀏覽器代碼產生相應的apk包來重新在機器上安裝。

  大體的步驟如下:

  1、開啟 packages/apps/Browser/AndroidManifest.xml檔案把加到相應的<intent-filter>後面就可以了
  

[java] view plaincopyprint?
  1. <intent-filter>  
  2.                   <action android:name="android.intent.action.VIEW" />  
  3.                   <category android:name="android.intent.category.DEFAULT" />  
  4.                   <category android:name="android.intent.category.BROWSABLE" />  
  5.                   <data android:scheme="file" />  
  6.               </intent-filter>  

  2、重新編譯打包,安裝,這樣子,新的瀏覽器就支援”file“這個形式了。

Android 啟動瀏覽器的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.