Android之Intent詳解

1.Intent的作用?都有哪些屬性?

Intent是應(yīng)用程序種各個(gè)組件聯(lián)系的橋梁,通信的載體,負(fù)責(zé)應(yīng)用程序中數(shù)據(jù)的傳遞(運(yùn)輸大隊(duì)長(zhǎng))

  • 啟動(dòng)一個(gè)Acitivity:
    Context.this.startActivity(intent);

  • 啟動(dòng)一個(gè)Sercvie
    Context.this.startService(intent);

  • 停止一個(gè)Service
    Context.this.stopService(intent);

  • 注冊(cè)一個(gè)廣播接收器
    Context.this.registerReceiver(receiver, filter);

  • 注銷一個(gè)廣播接收器
    Context.this.unregisterReceiver(receiver);

  • 發(fā)送一個(gè)廣播
    Context.this.sendBroadcast(intent);

  • 1.1 Component:要請(qǐng)求的目標(biāo)組件

  • 1.2 Action:被請(qǐng)求的組件所要完成的動(dòng)作

  • 1.3 Category:為Action增加額外的附加類別信息,通常和Action配合使用

  • 1.4 Data:為Action提供所要處理的數(shù)據(jù),通常是一個(gè)Uri對(duì)象【schema、host、port、path】

  • 1.5 Extras:要攜帶的數(shù)據(jù)

  • 1.6 Flags:設(shè)置Activity的啟動(dòng)模式(四種啟動(dòng)模式)

2.Intent的分類

顯示的Intent:明確指定要啟動(dòng)的組件,
隱式的Intent:通過IntentFilter來(lái)指定要啟動(dòng)的組件
一般在同一個(gè)應(yīng)用中使用顯示的Intent,如果跨應(yīng)用則使用隱式的Intent
如果需要隱式意圖啟動(dòng)一個(gè)Activity則必須配置CATEGORY_DEFAULT
Android5.0以后不能使用隱式的Intent來(lái)啟動(dòng)一個(gè)Service,也就是Service組件不能配置IntentFilter
如果通過隱式意圖啟動(dòng)的組件不存在,應(yīng)用將崩潰
if(Intent.resolveActivity(getPackageManager())!=null){    
    startActivity(sendIntent);
}
image.gif
如果每次啟動(dòng)都希望選擇則:
Intent sendIntent = new Intent(Intent.ACTION_SEND);
String title = getResources().getString(R.string.chooser_title);
Intent chooser = Intent.createChooser(sendIntent,title);
if(sendIntent.resolveActivity(getPackageManager()) != null){
    startActivity(chooser);
}
image.gif

3.Pending Intent作用?主要使用場(chǎng)合有哪些?

主要用于Intent的延時(shí)啟動(dòng),使用場(chǎng)合有
3.1 包裝一個(gè)Notification的Intent
3.2 包裝一個(gè)App Widger的Intent(按Home鍵啟動(dòng)的Activity)
3.3 包裝一個(gè)延時(shí)啟動(dòng)的Activity(AlarmManager)
通過PendingIntent.getActivity()啟動(dòng)一個(gè)activity;
通過PendingIntent.getService()啟動(dòng)一個(gè)Service;
通過PendingIntent.getBroadcast()啟動(dòng)一個(gè)BroadcastReceiver;

4.常用的隱式Intent都有哪些?

Intent的真正強(qiáng)大之處在于它的隱式Intent,隱式Intent需要配合Intent-filters使用。隱式Intent足夠強(qiáng)大,以至于系統(tǒng)提供了大量的Intent方便開發(fā)者啟動(dòng)系統(tǒng)應(yīng)用程序

  • 4.1 應(yīng)用程序設(shè)置

    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    intent.setData(Uri.parse("package:" + getPackageName()));
    startActivityForResult(intent, APP_SETTING);
    
    image.gif
  • 4.2 GPS設(shè)置

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, GPS_SETTING);
image.gif
  • 4.3 APP安裝
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.setDataAndType(Uri.fromFile(newFile("/mnt/sdcard/mobilesafe72_2.apk")),"application/vnd.android.package-archive");
startActivity(intent);
image.gif
<intent-filter>  
   <action android:name="android.intent.action.VIEW"/>   
   <category android:name="android.intent.category.DEFAULT"/>   
   <data android:scheme="content"/>// content從內(nèi)容提供者中獲取數(shù)據(jù)   
   <data android:scheme="file"/>// file:從文件中獲取數(shù)據(jù)   
   <data android:mimeType="application/vnd.android.package-archive"/>
</intent-filter>
image.gif
  • 4.4 打電話
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:13621310260"));
image.gif
 <activity android:name="OutgoingCallBroadcaster"
                  android:configChanges="orientation|keyboardHidden"
                  android:permission="android.permission.CALL_PHONE"
                  android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.CALL"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="tel"/>
            </intent-filter>
            <intent-filter android:icon="@drawable/ic_launcher_sip_call">
                <action android:name="android.intent.action.CALL"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="sip"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CALL"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="voicemail"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CALL"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="vnd.android.cursor.item/phone"/>
                <data android:mimeType="vnd.android.cursor.item/phone_v2"/>
                <data android:mimeType="vnd.android.cursor.item/person"/>
            </intent-filter>
</activity>

image.gif
  • 4.5 發(fā)短信
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra("sms_body",content);
intent.setType("vnd.android-dir/mms-sms");
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto://0800000132"));
intent.putExtra("sms_body","The SMM text");
image.gif
 <activity android:name=".ui.ComposeMessageActivity"
                  android:configChanges="orientation|keyboardHidden"
                  android:launchMode="singleTop" android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="vnd.android-dir/mms-sms"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <action android:name="android.intent.action.SENDTO"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="sms"/>
                <data android:scheme="smsto"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <action android:name="android.intent.action.SENDTO"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:scheme="mms"/>
                <data android:scheme="mmsto"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="image/*"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="video/*"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="*/*"/>
            </intent-filter>
        </activity>
image.gif
  • 4.6 拍照
Intent intent = new Intent();//intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra(MediaStore.EXTRA_OUTPUT, 
FileProvider.getUriForFile(activity, "com.zhijiexing.travel.fileprovider", file));
activity.startActivityForResult(intent, requestcode);
image.gif
  • AndroidMinifest.xml文件下:
<provider 
         android:name="android.support.v4.content.FileProvider"
         android:authorities="com.zhijiexing.travel.fileprovider"
         android:exported="false" 
         android:grantUriPermissions="true">
        <meta-data 
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</provider>
image.gif
  • provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
      <external-path name="download" path=""/>
      <external-path name="Download" path=""/>
      <external-path name="external_files" path="."/>
    </paths>
</resources>
image.gif

5. intent.setFlags()方法中的參數(shù)值詳解?

  • 5.1 FLAG_ACTIVITY_CLEAR_TOP

例如現(xiàn)在的棧情況為:A B C D 。D此時(shí)通過intent跳轉(zhuǎn)到B,如果這個(gè)intent添加FLAG_ACTIVITY_CLEAR_TOP標(biāo)記,則棧情況變?yōu)椋篈 B。如果沒有添加這個(gè)標(biāo)記,則棧情況將會(huì)變成:A B C D B。也就是說,如果添加了FLAG_ACTIVITY_CLEAR_TOP標(biāo)記,并且目標(biāo)Activity在棧中已經(jīng)存在,則將會(huì)把位于該目標(biāo)activity之上的activity從棧中彈出銷毀。這跟上面把B的Launch mode設(shè)置成singleTask類似。簡(jiǎn)而言之,跳轉(zhuǎn)到的activity若已在棧中存在,則將其上的activity都銷掉。

  • 5.2 FLAG_ACTIVITY_NEW_TASK

例如現(xiàn)在棧1的情況是:A B C。C通過intent跳轉(zhuǎn)到D,并且這個(gè)intent添加了FLAG_ACTIVITY_NEW_TASK標(biāo)記,如果D這個(gè)Activity在Manifest.xml中的聲明中添加了Task affinity,系統(tǒng)首先會(huì)查找有沒有和D的Task affinity相同的task棧存在,如果有存在,將D壓入那個(gè)棧,如果不存在則會(huì)新建一個(gè)D的affinity的棧將其壓入。如果D的Task affinity默認(rèn)沒有設(shè)置,則會(huì)把其壓入棧1,變成:A B C D,這樣就和不加FLAG_ACTIVITY_NEW_TASK標(biāo)記效果是一樣的了。注意如果試圖從非activity的非正常途徑啟動(dòng)一個(gè)activity(例見下文“intent.setFlags()方法中參數(shù)的用例”),比如從一個(gè)service中啟動(dòng)一個(gè)activity,則intent比如要添加FLAG_ACTIVITY_NEW_TASK標(biāo)記(編者按:activity要存在于activity的棧中,而非activity的途徑啟動(dòng)activity時(shí)必然不存在一個(gè)activity的棧,所以要新起一個(gè)棧裝入啟動(dòng)的activity)。簡(jiǎn)而言之,跳轉(zhuǎn)到的activity根據(jù)情況,可能壓在一個(gè)新建的棧中。
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

  • 5.3 FLAG_ACTIVITY_NO_HISTORY

例如現(xiàn)在棧情況為:A B C。C通過intent跳轉(zhuǎn)到D,這個(gè)intent添加FLAG_ACTIVITY_NO_HISTORY標(biāo)志,則此時(shí)界面顯示D的內(nèi)容,但是它并不會(huì)壓入棧中。如果按返回鍵,返回到C,棧的情況還是:A B C。如果此時(shí)D中又跳轉(zhuǎn)到E,棧的情況變?yōu)椋篈 B C E,此時(shí)按返回鍵會(huì)回到C,因?yàn)镈根本就沒有被壓入棧中。簡(jiǎn)而言之,跳轉(zhuǎn)到的activity不壓在棧中。

  • 5.4 FLAG_ACTIVITY_SINGLE_TOP

和Activity的Launch mode的singleTop類似。如果某個(gè)intent添加了這個(gè)標(biāo)志,并且這個(gè)intent的目標(biāo)activity就是棧頂?shù)腶ctivity,那么將不會(huì)新建一個(gè)實(shí)例壓入棧中。簡(jiǎn)而言之,目標(biāo)activity已在棧頂則跳轉(zhuǎn)過去,不在棧頂則在棧頂新建activity。

  • 5.5 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

例如現(xiàn)在棧情況為:A B C,現(xiàn)在要再次啟動(dòng)B,并且設(shè)置為該模式,則棧的變化為:A C B
如果這個(gè)activity已經(jīng)啟動(dòng)了,就不產(chǎn)生新的activity,而只是把這個(gè)activity實(shí)例加到棧頂來(lái)就可以了

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • LaunchMode 1、standard standard啟動(dòng)模式為最基本的啟動(dòng)模式,默認(rèn)為該種啟動(dòng)模式...
    ramblejoy閱讀 5,011評(píng)論 0 7
  • Task和BackStack的基本概念 Task的理解 Task是多個(gè)Activity的集合,用戶進(jìn)行操作時(shí)將與這...
    Reathin閱讀 2,286評(píng)論 1 4
  • 一、知識(shí)點(diǎn)回顧:Activity (一)、如何實(shí)現(xiàn)Activity頁(yè)面跳轉(zhuǎn)? 示例代碼: //第一種方式: Int...
    白話徐文濤閱讀 2,176評(píng)論 0 15
  • 個(gè)人使命宣言:我死的時(shí)候人們說,她勇敢、堅(jiān)強(qiáng)、充滿正能量、善良、專業(yè)、優(yōu)秀。她來(lái)過,折騰過,為自己起舞。年輕的時(shí)候...
    rainbow_72dd閱讀 1,893評(píng)論 0 0
  • 酸棗,對(duì)我們北方人來(lái)講都很熟悉,摘酸棗也是我們80后童年的必修課。酸棗成熟的時(shí)候模樣紅彤彤,味道酸溜溜,入...
    Ellen_閱讀 821評(píng)論 1 2

友情鏈接更多精彩內(nèi)容