參考:https://www.cnblogs.com/android-blogs/p/5690844.html
一、Intent的作用是什麼?
-
Intent 用於封裝程序的”調用意圖“。兩個Activity之間,可以把需要交換的數據封裝成Bundle對象,然後使用Intent攜帶Bundle對象,實現兩個Activity之間的數據交換;
-
Intent還是各種應用程序組件之間通信的重要媒介。不管想啓動一個Service、Acitivity還是BroadcastReceiver,Android均使用統一的Intent對象來封裝這種”啓動意圖“。很明顯使用Intent提供了一致的編程模型;
-
Intent還有一個好處,如果應用程序只是想啓動具有某種特徵的組件,並不想和某個具體的組件耦合,則可以通過在intent-filter中配置相應的屬性進行處理,與stucts2中的MVC框架思路類似。
二、啓動Intent的方式
-
第一種方式:開啓另一個Activity
Intent intent=new Intent(this,SecondActivity.class); Intent intent=new Intent();
-
第二種方式:使用ComponentName
//使用ComponentName組件名來開啓另一個Activity ComponentName component=new ComponentName(this, SecondActivity.class); //第二個參數是包含包名,類名的一個完整字符串 ComponentName component=new ComponentName(this, "com.qianfeng.day06_intentattribute01.SecondActivity"); //第一個參數是單純的包名;第二個參數是包含包名,類名的一個完整字符串 ComponentName component=new ComponentName("com.qianfeng.day06_intentattribute01", "com.qianfeng.day06_intentattribute01.SecondActivity"); intent.setComponent(component);
-
第三種方式:setClass也可以實現跳轉
intent.setClass(this, SecondActivity.class);
-
第四種方式:setClassName跳轉方式
intent.setClassName(this, "com.qianfeng.day06_intentattribute01.SecondActivity");
-
第五種:通過隱式意圖開啓另一個Activity
intent.setAction("ergouzisimida"); //其實系統默認就有這句話,默認省略了這句話 intent.addCategory("android.intent.category.DEFAULT"); startActivity(intent);
二、Intent對象的七個屬性
<1>ComponentName屬性:
1、指定了ComponentName屬性的Intent已經明確了它將要啓動哪個組件,因此這種Intent被稱爲顯式Intent,沒有指定ComponentName屬性的Intent被稱爲隱式Intent。隱式Intent沒有明確要啓動哪個組件,應用會根據Intent指定的規則去啓動符合條件的組件。
2、示例代碼:
Intent intent = new Intent();
ComponentName cName = new ComponentName(MainActivity.this,NextActivity.class);
intent.setComponent(cName);
startActivity(intent);
//實際上,以上的寫法都被簡化爲以下寫法:
Intent intent = new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
//也就是說,平時我們最常用的Intent頁面跳轉的寫法就調用的是顯式Intent。
<2>Action屬性
Action、Category屬性與intent-filter配置:
通常,Action、Category屬性結合使用,定義這兩個屬性都是在配置文件的節點中。Intent通過定義Action屬性(其實就是一段自定義的字符串),這樣就可以把Intent與具體的某個Activity分離,實現瞭解耦。否則,每次跳轉,都要寫成類似new Intent(MainActivity.this,NextActivity.class)這樣的形式,也就是說必須將要跳轉的目標Activity的名字寫出來,這樣的編碼其實是“硬編碼”,並沒有實現松耦合。調用Intent對象的setAction()方法實現頁面跳轉雖然略微複雜(需要在AndroidManifest.xml文件中配置),但是實現瞭解耦。
1、示例代碼:
Intent intent = new Intent();
intent.setAction("com.train.task01.editactivity");
startActivity(intent);
//在配置文件中註冊Activity的時候需要聲明:
<activity android:name="com.train.taskstack01.EditActivity">
<intent-filter>
<action android:name="com.train.task01.editactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
//當某個頁面是默認啓動頁面時,需要定義Action、Category的屬性必須爲以下字符串:【設置任務入口】
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
2、常用的屬性
ACTION_MAIN:(android.intent.action.MAIN)Android程序入口。
ACTION_VIEW: (android.intent.action.VIEW) 顯示指定數據。
ACTION_EDIT: (android.intent.action.EDIT) 編輯指定數據。
ACTION_DIAL: (android.intent.action.DIAL) 顯示撥號面板。
ACTION_CALL: (android.intent.action.CALL) 直接呼叫Data中所帶的號碼。
ACTION_ANSWER: (android.intent.action.ANSWER) 接聽來電。
ACTION_SEND: (android.intent.action.SEND) 向其他人發送數據(例如:彩信/email)。
ACTION_SENDTO: (android.intent.action.SENDTO) 向其他人發送短信。
ACTION_SEARCH: (android.intent.action.SEARCH) 執行搜索。
ACTION_GET_CONTENT: (android.intent.action.GET_CONTENT) 讓用戶選擇數據,並返回所選數據。
<3> Category屬性
Category屬性爲Action增加額外的附加類別信息。CATEGORY_LAUNCHER意味着在加載程序的時候Acticity出現在最上面,而CATEGORY_HOME表示頁面跳轉到HOME界面。
實現頁面跳轉到HOME界面的代碼:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
1、常用屬性
CATEGORY_DEFAULT: (android.intent.category.DEFAULT) Android系統中默認的執行方式,按照普通Activity的執行方式執行。
CATEGORY_HOME: (android.intent.category.HOME) 設置該組件爲Home Activity。
CATEGORY_PREFERENCE: (android.intent.category.PREFERENCE) 設置該組件爲Preference。
CATEGORY_LAUNCHER: (android.intent.category.LAUNCHER) 設置該組件爲在當前應用程序啓動器中優先級最高的Activity,通常與入口ACTION_MAIN配合使用。 CATEGORY_BROWSABLE: (android.intent.category.BROWSABLE) 設置該組件可以使用瀏覽器啓動。
<4>Data屬性
Data屬性通常用於向Action屬性提供操作的數據。Data屬性的值是個Uri對象。
Uri的格式如下:
scheme://host:port/path
uri屬性有以下4部分組成:android:scheme、android:host、android:port、android:path,其中host和port2個統稱爲authority。要使authority(host和port)有意義,必須指定scheme;要使path有意義,必須使scheme和authority(host和port)有意義
舉例說明:
URI爲: file://com.android.jony.test:520/mnt/sdcard,我們拆分如下:
scheme–>file:
host–>com.android.jony.test
port–>520
path–>mnt/sdcard
authority–>com.android.jony.test:520
舉例說明:
在AndroidManifest.xml 中進行如下設置:
<activity android:name=".TestActivity">
<intent-filter>
<action android:name="com.jony.test"/>
<data android:scheme="lee"
android:host="www.baidu.com"
android:port="8888"
android:path="/mypath"
//android:mimeType="abc/xyz"
/>
</intent-filter>
</activity>
啓動該Activity的Intent必須進行如下設置:
Intent intent = new Intent();
Uri uri = Uri.parse("lee://www.baudu.com:8888/mypath");
//intent.setDataAndType(Uri.parse("lee://www.baudu.com:8888/mypath"),"abc/xyz");
intent.setData(uri);
Intent利用Action屬性和Data屬性啓動Android系統內置組件代碼:
//發送短信
public void sendSMS(View view){
Intent intent=new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:13366201398"));
intent.putExtra("sms_body", "發送短信的內容");
startActivity(intent);
}
//打電話,
<!-- 增加打電話的權限 -->
<uses-permission android:name="android.permission.CALL_PHONE" />
public void callPhone(View view){
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:13366201398"));
startActivity(intent);
}
//打開網頁
<!-- 增加訪問網絡的權限 -->
<uses-permission android:name="android.permission.INTERNET" />
public void openHtml(View view){
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.baidu.com/"));
startActivity(intent);
}
//打開網絡圖片
public void openImage(View view){
Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("http://192.168.129.94:8080/images/s1.jpg"));
startActivity(intent);
}
//打開相冊
publicvoid openAlbum(View view){
Intent intent = new Intent();
intent.setAction(Intent. ACTION_GET_CONTENT );
intent.setType( "image/*" );
startActivityForResult(wrapperIntent, 100);
}
//打開相機
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
1、常用屬性
tel://:號碼數據格式,後跟電話號碼。
mailto://:郵件數據格式,後跟郵件收件人地址。
smsto://:短息數據格式,後跟短信接收號碼。
content://:內容數據格式,後跟需要讀取的內容。
file://:文件數據格式,後跟文件路徑。
market://search?q=pname:pkgname:市場數據格式,在Google Market裏搜索包名爲pkgname的應用。
geo://latitude, longitude:經緯數據格式,在地圖上顯示經緯度所指定的位置。
<5>Type屬性
- Type屬性用於指定Data所指定的Uri對應的MIME類型。MIME只要符合“abc/xyz”這樣的字符串格式即可。
- Intent利用Action、Data和Type屬性啓動Android系統內置組件的代碼:
播放視頻:
Intentintent = new Intent();
Uri uri = Uri.parse("file:///sdcard/media.mp4");
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
<6>Flag屬性
Intent可調用addFlags()方法來爲Intent添加控制標記
實例:
Intent intent = new Intent(this, MainActivity.class);
//將Activity棧中處於MainActivity主頁面之上的Activity都彈出。
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
常用屬性
FLAG_ACTIVITY_CLEAR_TOP:(效果同Activity LaunchMode的singleTask )
FLAG_ACTIVITY_SINGLE_TOP:(效果同Activity LaunchMode的singleTop)
FLAG_ACTIVITY_NEW_TASK:默認的跳轉類型,會重新創建一個新的Activity
FLAG_ACTIVITY_REORDER_TO_FRONT:如果activity在task存在,拿到最頂端,不會啓動新的Activity
FLAG_ACTIVITY_NO_HISTORY:被啓動的Activity一旦退出,他就不會存在於棧中
<7>Extra屬性
-
通過intent.putExtra(鍵, 值)的形式在多個Activity之間進行數據交換。
-
系統內置的幾個Extra常量:
EXTRA_BCC:存放郵件密送人地址的字符串數組。 EXTRA_CC:存放郵件抄送人地址的字符串數組。 EXTRA_EMAIL:存放郵件地址的字符串數組。 EXTRA_SUBJECT:存放郵件主題字符串。 EXTRA_TEXT:存放郵件內容。 EXTRA_KEY_EVENT:以KeyEvent對象方式存放觸發Intent的按鍵。 EXTRA_PHONE_NUMBER:存放調用ACTION_CALL時的電話號碼。
-
Intent利用Action、Data和Type、Extra屬性啓動Android系統內置組件的代碼:
//調用發送短信的程序
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setType("vnd.android-dir/mms-sms"); intent.putExtra("sms_body", "信息內容..."); startActivity(intent);
//發送短信息
Uri uri = Uri.parse("smsto:13200100001"); Intent intent = new Intent(); intent.setAction(Intent. ACTION_SENDTO ); intent.setData(uri); intent.putExtra("sms_body", "信息內容..."); startActivity( intent );
//發送彩信,設備會提示選擇合適的程序發送
Uri uri = Uri.parse("content://media/external/images/media/23"); //設備中的資源(圖像或其他資源) Intent intent = new Intent(); intent.setAction(Intent. ACTION_SEND ); intent.setType("image/png"); intent.putExtra("sms_body", "內容"); intent.putExtra(Intent.EXTRA_STREAM, uri); startActivity(it);
//發送Email:
Intent intent=new Intent(); intent.setAction(Intent. ACTION_SEND ); String[] tos={"[email protected]"}; String[] ccs={"[email protected]"}; intent.putExtra(Intent.EXTRA_EMAIL, tos); intent.putExtra(Intent.EXTRA_CC, ccs); intent.putExtra(Intent.EXTRA_TEXT, "The email body text"); intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); intent.setType("message/rfc822"); startActivity(Intent.createChooser(intent, "Choose Email Client"));
4、 Intent利用Action屬性中的ACTION_GET_CONTENT獲取返回值:
//選擇圖片 requestCode 返回的標識
Intent intent = new Intent(); intent.setAction(Intent. ACTION_GET_CONTENT ); intent.setType( "image/*" ); Intent wrapperIntent = Intent.createChooser(intent, null); startActivityForResult(wrapperIntent, requestCode);