隱式Intent使用--打開Android默認瀏覽器


     使用隱式Intent,AndroidManifest.xml通過在<activity>標簽下配置<intent-filter>的內容,可以指定當前活動能夠響應的action和category,我們不僅可以啟動自己程序內的活動,還可以啟動其他程序的活動,比如調用系統的瀏覽器來打開某網頁

 

  Intent intent = new Intent(Intent.ACTION_VIEW);    //為Intent設置Action屬性
  intent.setData(Uri.parse("http://www.baidu.com")); //為Intent設置DATA屬性
  startActivity(intent);

     Intent.ACTION_VIEW,這是一個Android系統內置的動作,其常量值為android.intent.action.VIEW。然后通過Uri.parse()方法,將一個網址字符串解析成一個Uri對象,再調用Intent的setData()方法將這個Uri對象傳遞進去。

 

     在AndroidManifest.xml中為Activity進行注冊。

<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <data android:scheme="http" />       <!--響應所有的http協議的Intent-->
</intent-filter>

 

      運行后就可以看到打開了系統瀏覽器。

      類似的,還可以指定很多其他協議,比如geo表示顯示地理位置、tel表示撥打電話,比如使用intent調用系統撥號界面

  Intent intent = new Intent(Intent.ACTION_DIAL);
  intent.setData(Uri.parse("tel:10086"));
  startActivity(intent);

   

      ACTION_DIAL表示intent的action是撥號這也是Android系統的內置動作。然后在data指定了協議是tel,號碼是10086 

 

                                   

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM