Android之Intent详解

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

Intent是应用程序种各个组件联系的桥梁,通信的载体,负责应用程序中数据的传递(运输大队长)

  • 启动一个Acitivity:
    Context.this.startActivity(intent);

  • 启动一个Sercvie
    Context.this.startService(intent);

  • 停止一个Service
    Context.this.stopService(intent);

  • 注册一个广播接收器
    Context.this.registerReceiver(receiver, filter);

  • 注销一个广播接收器
    Context.this.unregisterReceiver(receiver);

  • 发送一个广播
    Context.this.sendBroadcast(intent);

  • 1.1 Component:要请求的目标组件

  • 1.2 Action:被请求的组件所要完成的动作

  • 1.3 Category:为Action增加额外的附加类别信息,通常和Action配合使用

  • 1.4 Data:为Action提供所要处理的数据,通常是一个Uri对象【schema、host、port、path】

  • 1.5 Extras:要携带的数据

  • 1.6 Flags:设置Activity的启动模式(四种启动模式)

2.Intent的分类

显示的Intent:明确指定要启动的组件,
隐式的Intent:通过IntentFilter来指定要启动的组件
一般在同一个应用中使用显示的Intent,如果跨应用则使用隐式的Intent
如果需要隐式意图启动一个Activity则必须配置CATEGORY_DEFAULT
Android5.0以后不能使用隐式的Intent来启动一个Service,也就是Service组件不能配置IntentFilter
如果通过隐式意图启动的组件不存在,应用将崩溃
if(Intent.resolveActivity(getPackageManager())!=null){    
    startActivity(sendIntent);
}
image.gif
如果每次启动都希望选择则:
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作用?主要使用场合有哪些?

主要用于Intent的延时启动,使用场合有
3.1 包装一个Notification的Intent
3.2 包装一个App Widger的Intent(按Home键启动的Activity)
3.3 包装一个延时启动的Activity(AlarmManager)
通过PendingIntent.getActivity()启动一个activity;
通过PendingIntent.getService()启动一个Service;
通过PendingIntent.getBroadcast()启动一个BroadcastReceiver;

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

Intent的真正强大之处在于它的隐式Intent,隐式Intent需要配合Intent-filters使用。隐式Intent足够强大,以至于系统提供了大量的Intent方便开发者启动系统应用程序

  • 4.1 应用程序设置

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

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从内容提供者中获取数据   
   <data android:scheme="file"/>// file:从文件中获取数据   
   <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 发短信
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()方法中的参数值详解?

  • 5.1 FLAG_ACTIVITY_CLEAR_TOP

例如现在的栈情况为:A B C D 。D此时通过intent跳转到B,如果这个intent添加FLAG_ACTIVITY_CLEAR_TOP标记,则栈情况变为:A B。如果没有添加这个标记,则栈情况将会变成:A B C D B。也就是说,如果添加了FLAG_ACTIVITY_CLEAR_TOP标记,并且目标Activity在栈中已经存在,则将会把位于该目标activity之上的activity从栈中弹出销毁。这跟上面把B的Launch mode设置成singleTask类似。简而言之,跳转到的activity若已在栈中存在,则将其上的activity都销掉。

  • 5.2 FLAG_ACTIVITY_NEW_TASK

例如现在栈1的情况是:A B C。C通过intent跳转到D,并且这个intent添加了FLAG_ACTIVITY_NEW_TASK标记,如果D这个Activity在Manifest.xml中的声明中添加了Task affinity,系统首先会查找有没有和D的Task affinity相同的task栈存在,如果有存在,将D压入那个栈,如果不存在则会新建一个D的affinity的栈将其压入。如果D的Task affinity默认没有设置,则会把其压入栈1,变成:A B C D,这样就和不加FLAG_ACTIVITY_NEW_TASK标记效果是一样的了。注意如果试图从非activity的非正常途径启动一个activity(例见下文“intent.setFlags()方法中参数的用例”),比如从一个service中启动一个activity,则intent比如要添加FLAG_ACTIVITY_NEW_TASK标记(编者按:activity要存在于activity的栈中,而非activity的途径启动activity时必然不存在一个activity的栈,所以要新起一个栈装入启动的activity)。简而言之,跳转到的activity根据情况,可能压在一个新建的栈中。
Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

  • 5.3 FLAG_ACTIVITY_NO_HISTORY

例如现在栈情况为:A B C。C通过intent跳转到D,这个intent添加FLAG_ACTIVITY_NO_HISTORY标志,则此时界面显示D的内容,但是它并不会压入栈中。如果按返回键,返回到C,栈的情况还是:A B C。如果此时D中又跳转到E,栈的情况变为:A B C E,此时按返回键会回到C,因为D根本就没有被压入栈中。简而言之,跳转到的activity不压在栈中。

  • 5.4 FLAG_ACTIVITY_SINGLE_TOP

和Activity的Launch mode的singleTop类似。如果某个intent添加了这个标志,并且这个intent的目标activity就是栈顶的activity,那么将不会新建一个实例压入栈中。简而言之,目标activity已在栈顶则跳转过去,不在栈顶则在栈顶新建activity。

  • 5.5 Intent.FLAG_ACTIVITY_REORDER_TO_FRONT

例如现在栈情况为:A B C,现在要再次启动B,并且设置为该模式,则栈的变化为:A C B
如果这个activity已经启动了,就不产生新的activity,而只是把这个activity实例加到栈顶来就可以了

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,533评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,055评论 3 414
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 175,365评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,561评论 1 307
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,346评论 6 404
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,889评论 1 321
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,978评论 3 439
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,118评论 0 286
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,637评论 1 333
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,558评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,739评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,246评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,980评论 3 346
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,362评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,619评论 1 280
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,347评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,702评论 2 370

推荐阅读更多精彩内容