Android中的Intent和Intent-filter总结
一.相關(guān)概念
?
(一)基本概念
? ? ?Intent中文意思指”意圖”,按照Android的設(shè)計(jì)理念,Android使用Intent來封裝程序的”調(diào)用意圖”,不管啟動(dòng)Activity、Service、BroadcastReceiver,Android都使用統(tǒng)一的Intent對(duì)象來封裝這一”啟動(dòng)意圖”。此外,Intent也是應(yīng)用程序組件之間通信的重要媒介。在Android中指定的了具體是某個(gè)組件,那么就是顯性意圖;如果只是提出要求沒有指定具體的某個(gè)人,在Android中即沒有指定某個(gè)具體的組件,那么就是隱式意圖;所有Intent頁面跳轉(zhuǎn)的方式又分為顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)。?
?
(二)Intent和三大組件
Android應(yīng)用程序包含三種重要組件:Activity、Service、BroadcastReceiver,應(yīng)用程序采用一致的方式啟動(dòng)它們,都是依靠Intent來進(jìn)行啟動(dòng)的,Intent中封裝了程序要啟動(dòng)的意圖。
? 下面是Intent啟動(dòng)不同組件的部分方法:?
? Activity組件:?
? startActivity(Intent intent);startActivityForResult(Intent intent,int requestCode);?
? Service組件:?
? startService(Intent intent);bindService(Intent intent,ServiceConnection conn,int flags);?
? BroadcastReceiver組件:?
? sendBroadcast(Intent intent);sendOrderedBroadcast(Intent intent,String receiverPermission);?
?
(三)Intent的屬性類別
Intent的屬性及intent-filter的配置相結(jié)合使用;Intent的屬性大致包含:Component,Action,Category,Data,Type,Extra,Flag這7種屬性,其中Component用于明確指定需要啟動(dòng)的組件,而Extra則用于”攜帶”需要交換的數(shù)據(jù)。?
關(guān)于Intent屬性的詳解,在簡(jiǎn)單使用后再介紹。
?
二.Intent的使用
這里的Intent使用,主要是使用它來執(zhí)行頁面的跳轉(zhuǎn)功能。?
如果Intent對(duì)象中包含了目標(biāo)的class文件,那么就是顯示意圖的跳轉(zhuǎn);如果Intent沒有包含目標(biāo)的class文件,就是隱式意圖跳轉(zhuǎn),隱式意圖跳轉(zhuǎn)就涉及到比較多的Intent對(duì)象的屬性值的比較。
?
(一)Intent顯式跳轉(zhuǎn)頁面
其中實(shí)現(xiàn)跳轉(zhuǎn)的java代碼非常簡(jiǎn)單:
? Intent intent = new Intent(this, SecondActivity.class);?
? startActivity(intent);
其中具體實(shí)現(xiàn)是通過Intent的屬性類Component來實(shí)現(xiàn),不過Component類的方法都封裝在Intent的方法里面了,不需要我們過多的去了解。?
這如果要傳遞數(shù)據(jù)也可以通過Intent對(duì)象使用putExtra方法來傳遞數(shù)據(jù)。?
這里的目標(biāo)文件必須是要在AndroidManifest.xml里面注冊(cè)。
(二)Intent隱式跳轉(zhuǎn)頁面
隱式意圖的跳轉(zhuǎn)是本文主要講解的內(nèi)容,也是比較難理解的內(nèi)容!?
這里先使用Intent的Action和Data屬性來跳轉(zhuǎn)頁面做示例(特別要注意的是這里的Data不是Intent的數(shù)據(jù),而是Action動(dòng)作對(duì)應(yīng)的數(shù)據(jù)):
?
1.布局文件activity.xml文件設(shè)計(jì)
?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:id="@+id/activity_main"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:orientation="vertical">
? ? <TextView
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="意圖Intent" />
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="action"
? ? ? ? android:text="action" />
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="actionUri"
? ? ? ? android:text="actionUri" />
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="ToSecondActivity"
? ? ? ? android:text="跳轉(zhuǎn)到第二個(gè)頁面" />
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="MyAction"
? ? ? ? android:text="MyAction" />
? ? <Button
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:onClick="actionMain"
? ? ? ? android:text="返回桌面" />
</LinearLayout>12345678910111213141516171819202122232425262728293031323334353637383940414243
上面的xml布局文件設(shè)計(jì)有5個(gè)按鈕,具體功能開java代碼,下面詳細(xì)解釋。
1
?
2.java文件MainActivity.xml文件的設(shè)計(jì)
?
package com.example.intent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? //打開一個(gè)數(shù)據(jù)視圖,但是沒有要求,系統(tǒng)會(huì)默認(rèn)讓你從多個(gè)中選擇其中一個(gè)打開
? ? public void action(View v) {
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? startActivity(intent);
? ? }
? ? //打開一個(gè)數(shù)據(jù)視圖,有數(shù)據(jù)要求,系統(tǒng)幫你打開一個(gè)瀏覽器,并連接到相關(guān)的頁面
? ? public void actionUri(View v) {
? ? ? ? Intent intent = new Intent(Intent.ACTION_VIEW);
? ? ? ? intent.setData(Uri.parse("http://www.baidu.com"));
? ? ? ? startActivity(intent);
? ? }
? ? //使用顯示跳轉(zhuǎn)的方法跳轉(zhuǎn)到第二個(gè)頁面
? ? public void ToSecondActivity(View view) {
? ? ? ? Intent intent = new Intent(this, SecondActivity.class);
? ? ? ? startActivity(intent);
? ? }
? ? //這里只用自己定義的Action字符串來找到對(duì)應(yīng)的頁面
? ? public void MyAction(View v) {
? ? ? ? Intent intent = new Intent("HelloWorld");//Action字符串可以直接放在Intent的構(gòu)造函數(shù)里面,也可以單獨(dú)寫
? ? ? ? // intent.setAction("HelloWorld");
? ? ? ? intent.addCategory("android.intent.category.DEFAULT");//可以沒有
? ? ? ? //但是自定義的Action的xml文件里面必須要有category元素,否則會(huì)報(bào)錯(cuò)
? ? ? ? startActivity(intent);
? ? }
? ? //打開程序入口,特征是桌面
? ? public void actionMain(View v) {
? ? ? ? try {
? ? ? ? ? ? Intent intent = new Intent(Intent.ACTION_MAIN);
? ? ? ? ? ? intent.addCategory(Intent.CATEGORY_HOME);
? ? ? ? ? ? startActivity(intent);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? Toast.makeText(this, "找不到目標(biāo)頁面", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }
?
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
這要重點(diǎn)理解的代碼,下面會(huì)詳細(xì)介紹。
?
3.java文件SecondActivity.java文件的設(shè)計(jì)
K?
一個(gè)簡(jiǎn)單Activity的設(shè)計(jì),使用代碼設(shè)計(jì)一個(gè)線程布局并添加兩個(gè)文本,第二個(gè)文本添加的是跳轉(zhuǎn)后顯示的Intent屬性。
?
4.在AndroidManifest注冊(cè)一下SecondActivity.class文件
<activity android:name=".SecondActivity" ?>
? ? <intent-filter>
? ? ? ? <action android:name="HelloWorld"/>
? ? ? ? <category android:name="android.intent.category.DEFAULT"/>
? ? </intent-filter>
</activity>123456
文件中使用intent-filter(意圖過濾器),用來存放意圖的屬性
其中MainActivity.java文件是要重點(diǎn)理解的;上面寫了五個(gè)按鈕對(duì)應(yīng)的監(jiān)聽方法。?
運(yùn)行程序后顯示的界面:?
?
頁面顯示有五個(gè)按鈕,每個(gè)按鈕對(duì)應(yīng)的是一個(gè)按鈕的監(jiān)聽方法。下面分別介紹:?
第一個(gè)按鈕使用的是隱式意圖跳轉(zhuǎn)頁面,Action屬性是系統(tǒng)的Action對(duì)象值,是指跳轉(zhuǎn)到一個(gè)View的視圖,這沒有指向具體目標(biāo)。?
點(diǎn)擊這個(gè)按鈕會(huì)顯示多個(gè)View的視圖供你選擇,效果如圖:?
可以點(diǎn)擊對(duì)應(yīng)的視圖按鈕,進(jìn)入相關(guān)界面。?
其中這里的Action值是通過構(gòu)造方法來傳進(jìn)去的,也可以通過Intent對(duì)象進(jìn)行設(shè)置。?
?
?
第二個(gè)按鈕也是打開一個(gè)視圖,但是這個(gè)視圖有動(dòng)作對(duì)應(yīng)的數(shù)據(jù),所以它會(huì)直接打開一個(gè)瀏覽器,來顯示你要打開的網(wǎng)頁。?
效果如圖:?
對(duì)于常用的一些頁面跳轉(zhuǎn)的Action和它對(duì)應(yīng)的Date是需要我們程序員花時(shí)間去記一下的。后面會(huì)有常用的Action和Data介紹。?
?
?
第三個(gè)按鈕是顯示跳轉(zhuǎn)到另一個(gè)頁面,因?yàn)檫@個(gè)頁面是自定義的,所以需要在注冊(cè)表AndroidManifest里面中注冊(cè)。如果被顯式調(diào)用,只需要添加Activity的name屬性就可以了,不需要設(shè)置Activity的過濾器intent-filter也可以。?
點(diǎn)擊按鈕后顯示的結(jié)果:?
?
這里的Action對(duì)象的值和Category對(duì)象的值都是空的。因?yàn)榍懊娴奶D(zhuǎn)的Intent對(duì)象沒有這個(gè)值,所以它的對(duì)象是空的,對(duì)比下面一個(gè)按鈕就知道了。?
?
?
第四個(gè)按鈕是隱式意圖跳轉(zhuǎn)去另一個(gè)頁面,使用的Action值也不是系統(tǒng)定義的,而是自己定義的一個(gè)字符串。這里是通過Action和Category的值來確定一個(gè)頁面。Category代表的是動(dòng)作的類別的意思。?
點(diǎn)擊按鈕后顯示的結(jié)果:?
?
可以考到頁面顯示了,Intent里面包含的Action的值和Category的值。如果之前沒有傳進(jìn)去就不會(huì)有。?
?
?
第五個(gè)按鈕,也是隱式意圖跳轉(zhuǎn)的使用,這里的Action值和Category值都是系統(tǒng)定義的都具有特定的含義,兩個(gè)結(jié)合使用就相對(duì)于使用了Home鍵直接顯示到桌面。?
還有就是這個(gè)按鈕的方法里面使用到了try catch方法,這是防止程序崩潰的方法,正確情況如果輸入的Action值或Category值沒有指向某一個(gè)或一些頁面,系統(tǒng)會(huì)直接崩潰,所有使用try catch是十分必要的。?
意圖跳轉(zhuǎn)時(shí)使用try catch來防止出錯(cuò)是非常必要的。
?
三.intent-filter詳細(xì)屬性的介紹
?
<intent-filter/>是每一個(gè)Activity對(duì)應(yīng)的過濾器標(biāo)簽節(jié)點(diǎn)。每一個(gè)過濾器里面的元素可以有:
0個(gè)或多個(gè)<action.../>
0個(gè)或多個(gè)<category.../>
0個(gè)或1個(gè)<data.../>1234
(一)全部屬性的簡(jiǎn)介
Intent通過下面的屬性來描述的某個(gè)意圖:
?
1. action(動(dòng)作): 用來表示意圖的動(dòng)作,如:查看,發(fā)郵件,打電話
?
2. category(類別): 用來表示動(dòng)作的類別。
?
3. data(數(shù)據(jù)): 表示與動(dòng)作要操作的數(shù)據(jù)。如:查看指定的聯(lián)系人
?
4. type(數(shù)據(jù)類型): 對(duì)data類型的描述。
?
5. extras(附件信息): 附件信息。如:詳細(xì)資料,一個(gè)文件,某事。
?
6. component(目標(biāo)組件): 目標(biāo)組件。
下面詳細(xì)介紹各個(gè)組件的使用
?
(一)component屬性
指定了component屬性的Intent(調(diào)用setComponent(ComponentName)或者?
setClass(Context, Class),Class其實(shí)就是顯式調(diào)用需要的目標(biāo)類的文件名。這個(gè)屬性用得比較少,最好不用。如果是顯示調(diào)用直接指定目標(biāo)類的class文件名就可以使用了。?
比如:
?
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);12
Intent在后臺(tái)已經(jīng)幫我們實(shí)現(xiàn)了component屬性的設(shè)置,我們不需要很麻煩的再去實(shí)現(xiàn)它的過程。
(二)action動(dòng)作屬性
動(dòng)作很大程序上決定了Intent如何構(gòu)建,特別是數(shù)據(jù)和附加信息,就像一個(gè)方法名決定了參數(shù)和返回值一樣,所以應(yīng)該盡可能明確地指定動(dòng)作,并緊密關(guān)聯(lián)到其他的Intent字段,如Category和Data。
常用動(dòng)作?
最常用的是Action_MAIN(作為初始的Activity啟動(dòng),沒有數(shù)據(jù)的輸入輸出)?
1. ACTION_MAIN 作為一個(gè)主要的進(jìn)入口,而并不期望去接受數(shù)據(jù)?
2. ACTION_VIEW 向用戶去顯示數(shù)據(jù)?
3. ACTION_ATTACH_DATA 別用于指定一些數(shù)據(jù)應(yīng)該附屬于一些其他的地方,例如,圖片數(shù)據(jù)應(yīng)該附?
屬于聯(lián)系人?
4. ACTION_EDIT 訪問已給的數(shù)據(jù),提供明確的可編輯?
5. ACTION_GET_CONTENT 允許用戶選擇特殊種類的數(shù)據(jù),并返回(特殊種類的數(shù)據(jù):照一張相片或?
錄一段音) ACTION_DIAL 撥打一個(gè)指定的號(hào)碼,顯示一個(gè)帶有號(hào)碼的用戶界面,允許?
用戶去啟動(dòng)呼叫?
6. ACTION_CALL 根據(jù)指定的數(shù)據(jù)執(zhí)行一次呼叫(有缺陷,使用ACTION_DIAL)?
7. ACTION_SEND 傳遞數(shù)據(jù),被傳送的數(shù)據(jù)沒有指定,接收的action請(qǐng)求用戶發(fā)數(shù)據(jù)?
8. ACTION_SENDTO 發(fā)送一條信息到指定的某人?
9. ACTION_ANSWER 處理一個(gè)打進(jìn)電話呼叫?
10. ACTION_INSERT 插入一條空項(xiàng)目到已給的容器?
11. ACTION_DELETE 從容器中刪除已給的數(shù)據(jù)?
12. ACTION_SEARCH 執(zhí)行一次搜索?
13. ACTION_WEB_SEARCH 執(zhí)行一次web搜索?
上面的動(dòng)作都是Intent對(duì)象引用才有實(shí)際意義的。?
setAction(String action) 用來設(shè)置Intent的動(dòng)作,參數(shù)可以為常量?
? ? getAction() 方法用來獲取Intent動(dòng)作名稱 ?
上面的Action都是系統(tǒng)定義好的,具有一定意義的動(dòng)作指向的動(dòng)作。?
Intent的Action對(duì)象其實(shí)就是一個(gè)字符串常量,系統(tǒng)的Action對(duì)象是系統(tǒng)定義好的字符串常量,我們也可以自己定義自己的Action作為字符串常量。就像上面的例子使用到了自定義的Action字符串對(duì)象。
?
(三)category類別屬性
Intent的action、category屬性都是普通的字符串,其中action表示Intent需要完成的一個(gè)抽象”動(dòng)作”,而category則為action添加額外的類別信息,通常action和category一塊使用。?
需要指出的是,一個(gè)Intent中只能包含一個(gè)action屬性,但可以包含多個(gè)category屬性。當(dāng)程序創(chuàng)建Intent時(shí),該Intent默認(rèn)啟動(dòng)常量值為andorid.intent.category.DEFAULT的組件。這里的一個(gè)Intent中只能包含一個(gè)action屬性,并不是Activity中xml的設(shè)置規(guī)范,而是你要跳轉(zhuǎn)到的頁面去,你只能設(shè)置一個(gè)Action的值。?
常用的Category:?
1.CATEGORY_DEFAULT:Android系統(tǒng)中默認(rèn)的執(zhí)行方式,按照普通Activity的執(zhí)行方式執(zhí)行。 ?
2.CATEGORY_HOME:設(shè)置該組件為Home Activity。?
3.CATEGORY_PREFERENCE:設(shè)置該組件為Preference。 ?
4.CATEGORY_LAUNCHER:設(shè)置為當(dāng)前應(yīng)用程序優(yōu)先級(jí)最高的Activity,通常與ACTION_MAIN配合使用。 ?
5.CATEGORY_BROWSABLE:設(shè)置該組件可以使用瀏覽器啟動(dòng)。 ?
6.CATEGORY_GADGET:設(shè)置該組件可以內(nèi)嵌到另外的Activity中。?
上面的類別都是Intent對(duì)象引用才有實(shí)際意義的。
?
(四)data動(dòng)作數(shù)據(jù)
Data數(shù)據(jù)用來向Action屬性提供動(dòng)作的數(shù)據(jù)。這里的Data不是Intent里面的數(shù)據(jù),而是指明動(dòng)作的具體數(shù)據(jù),比如說動(dòng)作是打電話,那么打給具體的某一個(gè)人,就用到了date里面的數(shù)據(jù)來指定。同樣發(fā)郵件、或打開具體的某一個(gè)網(wǎng)址也是通過Data數(shù)據(jù)。?
Data屬性只接受Uri對(duì)象,Uri對(duì)象是統(tǒng)一資源標(biāo)識(shí)符。對(duì)應(yīng)概念不用太多理解,只需知道里面某些具體值的表現(xiàn)形式就可以了。?
Uri其實(shí)就是相當(dāng)于一個(gè)網(wǎng)址,如圖所示:
網(wǎng)址只是Uri其中一種格式的字符串,要使用它還要把它解析后轉(zhuǎn)化為Uri類型。?
為Intent對(duì)象添加Data數(shù)據(jù),代碼:?
?intent.setData(Uri.parse(“http://www.baidu.com“));?
這里的Uri的有兩個(gè)沒顯示的屬性:port的值是8080,path的值是/index?
通過下面三句代碼就可以跳轉(zhuǎn)到百度主頁面:
?
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);123
(五)type數(shù)據(jù)類型
與Data有關(guān)的,這個(gè)不是Intent的數(shù)據(jù)類型,是Intent的Action的Data數(shù)據(jù)的類型。?
比如:?
?{“.mp3”, ?“audio/x-mpeg”}, ?
{“.mp4”, “video/mp4”}, ?
{“.gif”, ?“image/gif”}, ?
{“.rmvb”, “audio/x-pn-realaudio”}, ?
這里只是做幾個(gè)簡(jiǎn)單的示例介紹,如果是打開gif格式的數(shù)據(jù)文件,設(shè)置type=“image/gif”
?
(六)extras額外的數(shù)據(jù)信息
Extras屬性主要用于傳遞目標(biāo)組件所需要的額外的數(shù)據(jù)。這個(gè)數(shù)據(jù)是可以通過Intent來保存的數(shù)據(jù)和Intent對(duì)象來獲取數(shù)據(jù)。
?
通過putExtras()方法設(shè)置。保存數(shù)據(jù)
?
通過putExtras()方法設(shè)置。獲取數(shù)據(jù)
通常我們使用Intent來直接傳遞Bundle對(duì)象,但也可以傳遞其他系統(tǒng)內(nèi)置的一些參數(shù)。?
如果要傳遞是是對(duì)象,那么對(duì)象必須實(shí)現(xiàn)序列化。
?
(七)intent-filter在AndroidManifest中
1.在里可以有多個(gè)?
,只需匹配其中一個(gè)即可。?
語法:
?
<intent-filter>
<data android:host="string"
? ? ? ?android:mimeType="string"
? ? ? ?android:path="string"
? ? ? ?android:pathPattern="string"
? ? ? ?android:pathPrefix="string"
? ? ? ?android:port="string"
? ? ? ?android:scheme="string" />
</intent-filter>123456789
也可以分開寫,如:
?
? ? ?<data android:scheme="something" android:host="project.example.com" android:port="80"/>
?等同于這樣寫:
?<data android:scheme="something"/>
?<data android:host="project.example.com"/>
?<data android:port="80"/>
等同于Uri uri = Uri.parse("something://project.example.com:80");123456
Uri的格式:scheme://host:port/path or pathPrefix or pathPattern?
如果scheme沒有指定,那其它的屬性均無效;?
如果host沒有指定,那么port,path,pathPrefix,pathPattern均無效;?
如果在manifest里這樣寫:?
那么Uri uri = Uri.parse(“something://project.example.com”); 才可以匹配
2.在里可以有多個(gè)data或action ,只需匹配其中一個(gè)即可。?
3.當(dāng)匹配不上任何Activity的話,會(huì)發(fā)生異常,跳出對(duì)話框:很抱歉…某某應(yīng)用程序意外停止,請(qǐng)重試。?
4.上面所說的全部適用于Service和BroadcastReceiver。
?
(八)幾個(gè)簡(jiǎn)單示例的代碼
?
/**
?* 跳轉(zhuǎn)到指定的網(wǎng)址頁面
?*/
public void toView(View v) {
? ? Intent intent = new Intent();
? ? //設(shè)置Data數(shù)據(jù)
? ? intent.setData(Uri.parse("http://www.baidu.com"));
? ? //設(shè)置Action數(shù)據(jù)
? ? intent.setAction(Intent.ACTION_VIEW);
? ? //頁面跳轉(zhuǎn)
? ? startActivity(intent);
}
/**
?* 跳轉(zhuǎn)到編輯聯(lián)系人的信息
?* 這里聯(lián)系人的姓名是:1
?*/
public void toEdit(View v) {
? ? Intent intent = new Intent();
? ? //設(shè)置Data數(shù)據(jù)
? ? intent.setData(Uri.parse("content://com.android.contacts/contacts/1"));
? ? //設(shè)置Action數(shù)據(jù)
? ? intent.setAction(Intent.ACTION_EDIT);
? ? //頁面跳轉(zhuǎn)
? ? try {
? ? ? ? startActivity(intent);
? ? } catch (Exception e) {
? ? ? ? Toast.makeText(this, "找不到該聯(lián)系人!", Toast.LENGTH_SHORT).show();
? ? }
}
/**
?* 跳轉(zhuǎn)到撥打電話的頁面
?* Data可以設(shè)置電話號(hào)碼
?*/
public void toDial(View v) {
? ? Intent intent = new Intent();
? ? //設(shè)置Data數(shù)據(jù)
? ? intent.setData(Uri.parse("tel:13814236512"));
? ? //設(shè)置Action數(shù)據(jù)
? ? intent.setAction(Intent.ACTION_DIAL);
? ? //頁面跳轉(zhuǎn)
? ? startActivity(intent);
}1234567891011121314151617181920212223242526272829303132333435363738394041424344
?
三.下面是一些Intent對(duì)象的常量值
附部分Intent屬性值:
Action:?
? ?ACTION_MAIN:Android Application的入口,每個(gè)Android應(yīng)用必須且只能包含一個(gè)此類型的Action聲明。 ?
ACTION_VIEW:系統(tǒng)根據(jù)不同的Data類型,通過已注冊(cè)的對(duì)應(yīng)Application顯示數(shù)據(jù)。?
ACTION_EDIT:系統(tǒng)根據(jù)不同的Data類型,通過已注冊(cè)的對(duì)應(yīng)Application編輯示數(shù)據(jù)。 ?
ACTION_DIAL:系統(tǒng)默打開撥號(hào)程序,如果Data中設(shè)置電話號(hào)碼,則撥號(hào)框中會(huì)顯示此號(hào)碼。 ?
ACTION_CALL:直接呼叫Data中所帶的號(hào)碼。 ?
ACTION_ANSWER:接聽來電。 ?
ACTION_SEND:由用戶指定發(fā)送方式進(jìn)行數(shù)據(jù)發(fā)送操作。?
ACTION_SENDTO:系統(tǒng)根據(jù)不同的Data類型,通過已注冊(cè)的對(duì)應(yīng)Application進(jìn)行數(shù)據(jù)發(fā)送操作。 ?
ACTION_BOOT_COMPLETED:Android系統(tǒng)在啟動(dòng)完畢后發(fā)出帶有此Action的廣播(Broadcast)。 ?
ACTION_TIME_CHANGED:Android系統(tǒng)的時(shí)間發(fā)生改變后發(fā)出帶有此Action的廣播(Broadcast)。 ?
ACTION_PACKAGE_ADDED:Android系統(tǒng)安裝了新的App之后發(fā)出帶有此Action的廣播(Broadcast)。 ?
ACTION_PACKAGE_CHANGED:Android系統(tǒng)中已存在的App發(fā)生改變之后(如更新)發(fā)出帶有此Action的廣播(Broadcast)。 ?
ACTION_PACKAGE_REMOVED:Android系統(tǒng)卸載App之后發(fā)出帶有此Action的廣播(Broadcast)。
Category:?
? ?CATEGORY_DEFAULT:Android系統(tǒng)中默認(rèn)的執(zhí)行方式,按照普通Activity的執(zhí)行方式執(zhí)行。 ?
CATEGORY_HOME:設(shè)置該組件為Home Activity。?
CATEGORY_PREFERENCE:設(shè)置該組件為Preference。 ?
CATEGORY_LAUNCHER:設(shè)置為當(dāng)前應(yīng)用程序優(yōu)先級(jí)最高的Activity,通常與ACTION_MAIN配合使用。 ?
CATEGORY_BROWSABLE:設(shè)置該組件可以使用瀏覽器啟動(dòng)。 ?
CATEGORY_GADGET:設(shè)置該組件可以內(nèi)嵌到另外的Activity中。
Extras:?
? ?EXTRA_BCC:存放郵件密送人地址的字符串?dāng)?shù)組。 ?
EXTRA_CC:存放郵件抄送人地址的字符串?dāng)?shù)組。?
EXTRA_EMAIL:存放郵件地址的字符串?dāng)?shù)組。 ?
EXTRA_SUBJECT:存放郵件主題字符串。 ?
EXTRA_TEXT:存放郵件內(nèi)容。 ?
EXTRA_KEY_EVENT:以KeyEvent對(duì)象方式存放觸發(fā)Intent的按鍵。 ?
EXTRA_PHONE_NUMBER:存放調(diào)用ACTION_CALL時(shí)的電話號(hào)碼。 ?
?
? Data:?
? ?tel://:號(hào)碼數(shù)據(jù)格式,后跟電話號(hào)碼。 ?
mailto://:郵件數(shù)據(jù)格式,后跟郵件收件人地址。?
smsto://:短息數(shù)據(jù)格式,后跟短信接收號(hào)碼。?
content://:內(nèi)容數(shù)據(jù)格式,后跟需要讀取的內(nèi)容。 ?
file://:文件數(shù)據(jù)格式,后跟文件路徑。?
market://search?q=pname:pkgname:市場(chǎng)數(shù)據(jù)格式,在Google Market里搜索包名為pkgname的應(yīng)用。?
geo://latitude, longitude:經(jīng)緯數(shù)據(jù)格式,在地圖上顯示經(jīng)緯度所指定的位置。?
MimeType:?
?{“.3gp”, ? ?“video/3gpp”}, ?
? ? ? ? ? ? {“.apk”, ? ?“application/vnd.android.package-archive”}, ?
? ? ? ? ? ? {“.asf”, ? ?“video/x-ms-asf”}, ?
? ? ? ? ? ? {“.avi”, ? ?“video/x-msvideo”}, ?
? ? ? ? ? ? {“.bin”, ? ?“application/octet-stream”}, ?
? ? ? ? ? ? {“.bmp”, ? ?“image/bmp”}, ?
? ? ? ? ? ? {“.c”, ?“text/plain”}, ?
? ? ? ? ? ? {“.class”, ?“application/octet-stream”}, ?
? ? ? ? ? ? {“.conf”, ? “text/plain”}, ?
? ? ? ? ? ? {“.cpp”, ? ?“text/plain”}, ?
? ? ? ? ? ? {“.doc”, ? ?“application/msword”}, ?
? ? ? ? ? ? {“.docx”, ? “application/vnd.openxmlformats-officedocument.wordprocessingml.document”}, ?
? ? ? ? ? ? {“.xls”, ? ?“application/vnd.ms-excel”},?
? ? ? ? ? ? {“.xlsx”, ? “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”}, ?
? ? ? ? ? ? {“.exe”, ? ?“application/octet-stream”}, ?
? ? ? ? ? ? {“.gif”, ? ?“image/gif”}, ?
? ? ? ? ? ? {“.gtar”, ? “application/x-gtar”}, ?
? ? ? ? ? ? {“.gz”, “application/x-gzip”}, ?
? ? ? ? ? ? {“.h”, ?“text/plain”}, ?
? ? ? ? ? ? {“.htm”, ? ?“text/html”}, ?
? ? ? ? ? ? {“.html”, ? “text/html”}, ?
? ? ? ? ? ? {“.jar”, ? ?“application/Java-archive”}, ?
? ? ? ? ? ? {“.java”, ? “text/plain”}, ?
? ? ? ? ? ? {“.jpeg”, ? “image/jpeg”}, ?
? ? ? ? ? ? {“.jpg”, ? ?“image/jpeg”}, ?
? ? ? ? ? ? {“.js”, “application/x-JavaScript”}, ?
? ? ? ? ? ? {“.log”, ? ?“text/plain”}, ?
? ? ? ? ? ? {“.m3u”, ? ?“audio/x-mpegurl”}, ?
? ? ? ? ? ? {“.m4a”, ? ?“audio/mp4a-latm”}, ?
? ? ? ? ? ? {“.m4b”, ? ?“audio/mp4a-latm”}, ?
? ? ? ? ? ? {“.m4p”, ? ?“audio/mp4a-latm”}, ?
? ? ? ? ? ? {“.m4u”, ? ?“video/vnd.mpegurl”}, ?
? ? ? ? ? ? {“.m4v”, ? ?“video/x-m4v”},?
? ? ? ? ? ? {“.mov”, ? ?“video/quicktime”}, ?
? ? ? ? ? ? {“.mp2”, ? ?“audio/x-mpeg”}, ?
? ? ? ? ? ? {“.mp3”, ? ?“audio/x-mpeg”}, ?
? ? ? ? ? ? {“.mp4”, ? ?“video/mp4”}, ?
? ? ? ? ? ? {“.mpc”, ? ?“application/vnd.mpohun.certificate”},?
? ? ? ? ? ? {“.mpe”, ? ?“video/mpeg”},?
? ? ? ? ? ? {“.mpeg”, ? “video/mpeg”},?
? ? ? ? ? ? {“.mpg”, ? ?“video/mpeg”},?
? ? ? ? ? ? {“.mpg4”, ? “video/mp4”},?
? ? ? ? ? ? {“.mpga”, ? “audio/mpeg”}, ?
? ? ? ? ? ? {“.msg”, ? ?“application/vnd.ms-outlook”}, ?
? ? ? ? ? ? {“.ogg”, ? ?“audio/ogg”}, ?
? ? ? ? ? ? {“.pdf”, ? ?“application/pdf”}, ?
? ? ? ? ? ? {“.png”, ? ?“image/png”}, ?
? ? ? ? ? ? {“.pps”, ? ?“application/vnd.ms-powerpoint”}, ?
? ? ? ? ? ? {“.ppt”, ? ?“application/vnd.ms-powerpoint”}, ?
? ? ? ? ? ? {“.pptx”, ? “application/vnd.openxmlformats-officedocument.presentationml.presentation”}, ?
? ? ? ? ? ? {“.prop”, ? “text/plain”}, ?
? ? ? ? ? ? {“.rc”, “text/plain”}, ?
? ? ? ? ? ? {“.rmvb”, ? “audio/x-pn-realaudio”}, ?
? ? ? ? ? ? {“.rtf”, ? ?“application/rtf”}, ?
? ? ? ? ? ? {“.sh”, “text/plain”}, ?
? ? ? ? ? ? {“.tar”, ? ?“application/x-tar”},?
? ? ? ? ? ? {“.tgz”, ? ?“application/x-compressed”},?
? ? ? ? ? ? {“.txt”, ? ?“text/plain”}, ?
? ? ? ? ? ? {“.wav”, ? ?“audio/x-wav”}, ?
? ? ? ? ? ? {“.wma”, ? ?“audio/x-ms-wma”}, ?
? ? ? ? ? ? {“.wmv”, ? ?“audio/x-ms-wmv”}, ?
? ? ? ? ? ? {“.wps”, ? ?“application/vnd.ms-works”}, ?
? ? ? ? ? ? {“.xml”, ? ?“text/plain”}, ?
? ? ? ? ? ? {“.z”, ?“application/x-compress”}, ?
? ? ? ? ? ? {“.zip”, ? ?“application/x-zip-compressed”},?
?
總結(jié):
? ? 本文只要是對(duì)Intent知識(shí)中隱式Intent意圖的講解,其實(shí)這部分內(nèi)容并不是很常用,也是比較難理解一些。
學(xué)習(xí)Intent只要學(xué)會(huì)下面幾個(gè)知識(shí)點(diǎn)就差不多了:?
1.Intent顯式頁面跳轉(zhuǎn)的實(shí)現(xiàn)?
2.Intent跳轉(zhuǎn)到撥打電話頁面,固定網(wǎng)頁上等基礎(chǔ)功能。?
3.Intent學(xué)會(huì)使用數(shù)據(jù)的傳入和拿出。?
其中數(shù)據(jù)的處理,之前有個(gè)例子,大家可以借鑒一下:
? http://blog.csdn.net/wenzhi20102321/article/details/52750526
?
總結(jié)
以上是生活随笔為你收集整理的Android中的Intent和Intent-filter总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android Studio 上Acti
- 下一篇: mp4文件格式系列