天天看點

Android開發:顯式/隐式Intent

顯式跳轉

是在已知包名和類名的情況下常用的跳轉方法:

[java] view plain copy

Intent mIntent = new Intent();  

mIntent.setClassName("com.android.settings","com.android.settings.Settings");  

mContext.startActivity(mIntent);  

我們也常這麼用:

Intent intent = new Intent(mContext, XXActivity.class);  

startActivity(intent);  

這是跳轉到目前應用的某個Activity,相信大家都十分熟悉,今天主要講的是如何使用隐式intent意圖跳轉

隐式跳轉

假定有一個Activity在清單中是這樣的聲明的:

[html] view plain copy

<activity android:name=".ActionActivity";   

     <intent-filter  

         action android:name="customer_action_here"  

     </intent-filter>  

 </activity>  

那麼我們就可以使用以下代碼進行跳轉到上面這個Activity中:

//建立一個隐式的 Intent 對象:Action 動作  

Intent intent = new Intent();  

//設定 Intent 的動作為清單中指定的action  

intent.setAction("customer_action_here");  

假定有一個Activity在清單中是這樣聲明的:

<activity android:name=".CategoryActivity" >  

    <intent-filter>  

        <action android:name="customer_action_here" />  

        <category android:name="customer_category_here" />  

    </intent-filter>  

</activity>  

我們可以使用如下代碼進行跳轉到以上Activity:

//建立一個隐式的 Intent 對象:Category 類别  

//添加與清單中相同的自定義category  

intent.addCategory("customer_category_here");  

假定有一個Activity是這樣定義的:

< activity android:name=".DataActivity">  

    < intent-filter>  

        < category android:name="android.intent.category.DEFAULT" />  

        < data  

            android:scheme="content"  

            android:host="com.example.intentdemo"  

            android:port="8080"  

            android:pathPattern=".*pdf"  

            android:mimeType="text/plain"/>  

    < /intent-filter>  

< /activity>  

我們可以使用如下代碼進行跳轉:

//建立一個隐式的 Intent 對象,方法四:Date 資料  

Uri uri = Uri.parse("content://com.example.intentdemo:8080/abc.pdf");  

//注:setData、setDataAndType、setType 這三種方法隻能單獨使用,不可共用                 

//單獨以 setData 方法設定 URI  

//intent.setData(uri);  

//單獨以 seType 方法設定 Type  

//intent.setType("text/plain");  

//上面分步驟設定是錯誤的,要麼以 setDataAndType 方法設定 URI 及 mime type  

intent.setDataAndType(uri, "text/plain");  

清單中的port及以下屬性時可選的,沒有必要一定添加,但是添加了port及以下屬性的話,java代碼中的Uri中要做相應的比對。

//web浏覽器  

Uri uri= Uri.parse("http://www.baidu.com");  

Intent intent = new Intent(Intent.ACTION_VIEW, uri);  

//打開地圖檢視經緯度  

Uri uri = Uri.parse("geo:38.899533,-77.036476");  

Uri uri = Uri.parse("tel:10086");  

Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意差別于下面4.4的action  

Uri uri = Uri.parse("tel:15980665805");  

Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意差別于上面4.3的aciton  

Intent intent = new Intent(Intent.ACTION_VIEW);      

intent.putExtra("sms_body", "這裡寫短信内容");      

intent.setType("vnd.android-dir/mms-sms");      

Uri uri = Uri.parse("smsto:10086");//指定接收者  

Intent intent = new Intent(Intent.ACTION_SENDTO, uri);  

intent.putExtra("sms_body", "你這個黑心營運商");  

Intent intent = new Intent(Intent.ACTION_SENDTO);   

intent.setData(Uri.parse("mailto:[email protected]"));   

intent.putExtra(Intent.EXTRA_SUBJECT, "這是标題");   

intent.putExtra(Intent.EXTRA_TEXT, "這是内容");   

Intent intent = new Intent(Intent.ACTION_VIEW);  

//Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");  

Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");  

intent.setDataAndType(uri, "audio/mp3");  

//Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");  

Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");  

intent.setDataAndType(uri, "video/mp4");  

調用視訊播放器和音樂播放器的差別在setDataAndType()時一個是audio類型,一個是video類型,很容易記住,不允許使用其他意思相近的單詞代替,代替無效。

Intent intent = new Intent();   

intent.setAction(Intent.ACTION_WEB_SEARCH);   

intent.putExtra(SearchManager.QUERY, "android");   

總結

相信大家經過上面的介紹,已經對Intent跳轉有了一個比較成熟的了解,Intent是元件之間的紐帶,使用它可以讓系統替我們完成很多工作,不需要我們來指定完成工作的程式。實際上,我們會發現,調用系統程式使用液無非是隐式跳轉,隻不過使用的是系統内置的一些Action,Uri,Data等資訊而已。希望對大家有所幫助。