Android之Intent及其七大属性

       Intent是Android系统用来抽象描述要执行的一个操作,也可以在不同组件之间进行沟通和消息传递,平常在开发中处处可以看到,今天简单总结一下备忘:

基本介绍

       1.Intent 用于封装程序的”调用意图“。两个Activity之间,可以把需要交换的数据封装成Bundle对象,然后使用Intent携带Bundle对象,实现两个Activity之间的数据交换;

       2.Intent还是各种应用程序组件之间通信的重要媒介。不管想启动⼀个Acitivity、Service还是BroadcastReceiver,Android均使用统⼀的Intent对象来封装这种”启动意图“。很明显使用Intent提供了⼀致的编程模型;

       3.Intent还有⼀个好处,如果应用程序只是想启动具有某种特征的组件,并不想和某个具体的组件耦合,则可以通过在intent-filter中配置相应的属性进行处理。

       4.Intent对象大致包括7大属性:ComponentName、Action、Category、Data 、Type、Extra、Flag。

七大属性

       Intent对象大致包括7大属性:ComponentName、 Action 、 Category 、 Data 、Type、 Extra 、Flags。

一.ComponentName属性

       1.指定了ComponentName属性的Intent已经明确了它将要启动哪个组件,因此这种Intent被称为显式Intent,没有指定ComponentName属性的Intent被称为隐式Intent。隐式Intent没有明确要启动哪个组件,应用会根据Intent指定的规则去启动符合条件的组件;
       示例代码:

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;此外, ComponentName属性可以实现⼀个app跳转到另⼀个app。

Intent intent = new Intent();
//第⼀个参数是要跳转到的app的包名;
//第⼆个参数是该包中的要跳转到app的页面的class
ComponentName cName = new ComponentName("com.seven.test.main","com.seven.test.main.MainActivity");
intent.setComponent(cName);
startActivity(intent);

二.Action、Category属性与intent-filter配置

       Action、Category属性通常结合使用,定义这两个属性都是在配置文件的<intent-filter>节点中。Intent通过定义Action属性(其实就是⼀段自定义的字符串),这样就可以把Intent与具体的某个Activity分离,实现了解耦。否则,每次跳转,都要写成类似new Intent(MainActivity.this, NextActivity.class)这样的形式,也就是说必须将要跳转的目标Activity的名字写出来,这样的编码其实是“硬编码”,并没有实现松耦合。调用Intent对象的setAction()方法实现页面跳转虽然
略微复杂(需要在AndroidManifest.xml文件中配置),但是实现了解耦。

1.示例代码

Intent intent = new Intent(); 
intent.setAction("com.seven.test.mainactivity"); 
startActivity(intent);

       在配置文件中注册Activity的时候需要声明:

<activity android:name="com.seven.test.MainActivity">
    < intent-filter>
       <action android:name=" com.seven.test.mainactivity" />
       <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属性常量:

       Intent对象不仅可以启动本应用内的程序组件,也可以启动Android系统的其他应用的组件,包括系统内置的程序组件(需要设置权限)。
ACTION_MAIN:(android.intent.action.MAIN)Android程序入口。
每个Android应用必须且只能包含⼀个此类型的Action声明。【如果设置多个,则哪个在前,执行哪个。】
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)让用户选择数据,并返回所选数据。

三.Category 属性

       Category属性为Action增加额外的附加类别信息。CATEGORY_LAUNCHER意味着在加载程序的时候Acticity出现在最上面,而CATEGORY_HOME表示页面跳转到HOME界面。

1.实现页面跳转到HOME界面的代码

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGOTY_HOME); 
startActivity(intent); 

2.常用Category属性常量

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_BROWSEABLE:(android.intent.category.BROWSEABLE)设置该组件可以使用浏览器启动。

四.Data属性:

       1.Data属性通常用于向Action属性提供操作的数据。Data属性的值是个Uri对象。
              Uri的格式如下: scheme:/ /host:port/path
       2.系统内置的几个Data属性常量:
              tel://:号码数据格式,后跟电话号码。
              mailto://:邮件数据格式,后跟邮件收件人地址。
              smsto://:短息数据格式,后跟短信接收号码。
              content://:内容数据格式,后跟需要读取的内容。
              file://:文件数据格式,后跟文件路径。
              market:/ /search?q=pname:pkgname:市场数据格式,在Google Market里搜索包名为pkgname的应用。
              geo:/ /latitude, longitude:经纬数据格式,在地图上显示经纬度所指定的位置。
       3.Intent利用Action属性和Data属性 启动Android系统内置组件的代码:
       ①.拨打电话:

Intent intent=new Intent(); 
intent.setAction(Intent.ACTION_CALL);  
intent.setData(Uri.parse("tel:12345321234"));
startActivity(intent);

       ②.调用拨号面板:

Intent intent=new Intent();
intent.setAction(Intent.ACTION_DIAL); 
intent.setData(Uri.parse("tel:12345321234"));
startActivity(intent); 
//调用拨号面板:
Intent intent=new Intent();
intent.setAction(Intent.ACTION_VIEW); 
intent.setData(Uri.parse("tel:12345321234"));
startActivity(intent); 

       ③.利用Uri打开浏览器、打开地图等:

Uri uri = Uri.parse("http://www.google.com"); //浏览器
Uri uri=Uri.parse("geo:39.899533,116.036476"); //打开地图定位
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
startActivity(intent); 

五.Type属性:

       1.Type属性用于指定Data所指定的Uri对应的 MIME类型。MIME只要符合“abc/xyz”这样的字符串格式即可。
       2.Intent利用Action、Data和Type属性启动Android系统内置组件的代码:
       播放视频:

Intent intent = new Intent(); 
Uri uri = Uri.parse("file://sdcard/media.mp4"); 
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*"); 
startActivity(intent);

六.Extra属性:

       1.通过intent. putExtra(键, 值)的形式在多个Activity之间进行数据交换。
       2.系统内置的几个Extra常量:
              EXTRA_BCC:存放邮件密送人地址的字符串数组。
              EXTRA_CC:存放邮件抄送人的字符串数组。
              EXTRA_EMAIL:存放邮件地址的字符串数组。
              EXTRA_SUBJECT:存放邮件主题字符串。
              EXTRA_TEXT:存放邮件内容。
              EXTRA_KEY_EVENT:以KeyEvent对象方式存放触发Intent的按键。
              EXTRA_PHONE_NUMBER:存放调⽤ACTION_CALL时的电话号码。
       3.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", "content body"); 
startActivity(intent);

       发送短信息

Uri uri = Uri.parse("smsto:12345321234"); 
Intent  intent  = new Intent(); 
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(uri);
intent.putExtra("sms_body", "content body"); 
startActivity( intent ); 

       发送彩信,设备会提示选择合适的程序发送

Uri uri = Uri.parse("content://media/external/images/media/124"); //设备中的资源(图像或其他资源)
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png"); 
intent.putExtra("sms_body", "content"); 
intent.putExtra(Intent.EXTRA_STREAM, uri); 
startActivity(it);

       发送Email:

Intent intent=new Intent(); 
intent.setAction(Intent.  ACTION_SEND );
String[] tos={"androidlearn@126.com"}; 
String[] ccs={"youqwe@sina.com"}; 
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"));
Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = { "mobileservice@ablesky.com" };
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_TEXT, getPhoneParameter());
intent.putExtra(Intent.EXTRA_SUBJECT, "Android⽇志");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cacheDir));
intent.setType("message/rfc882");
intent.setType("plain/text");
Intent.createChooser(intent, "请选择邮件发送软件");
startActivity(intent);  

       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);  

       添加音频

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType( "video/*" ); 
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);  

       视频

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType( "video/*" ); 
Intent wrapperIntent = Intent.createChooser(intent, null);
startActivityForResult(wrapperIntent, requestCode);  

       录音

Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType( "audio/amr" ); 
intent.setClassName("com.android.soundrecorder","com.android.soundrecorder.SoundRecorder");
startActivityForResult(intent, requestCode);  

七.Flags属性

       Intent可调用 addFlags()方法来为Intent添加控制标记。
       1.FLAG_ACTIVITY_SINGLE_TOP:(效果同Activity LaunchMode的singleTop)
       如果在任务的栈顶正好存在该Activity的实例,就重用该实例,而不会创建新的Activity对象。
       2.FLAG_ACTIVITY_CLEAR_TOP:(效果同Activity LaunchMode的 singleTask)
       如果在栈中已经有该Activity的实例,就重用该实例。重用时,会让该实例回到栈顶,因此在它上面的实例将会被移除栈。如果栈中不存在该实例,将会创建新的实例放入栈中。
       3.FLAG_ACTIVITY_NEW_TASK: (效果类似Activity LaunchMode的 singleInstance )
       使用Intent的setFlags()方法设置窗口的启动模式。常用的有:

Intent.FLAG_ACTIVITY_SINGLE_TOP
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
Intent.FLAG_ACTIVITY_SINGLE_TOP:与在launchMode设置singleTop是完全⼀样的。
Intent.FLAG_ACTIVITY_CLEAR_TOP:

       当跳转到⼀个单独使用Intent.FLAG_ACTIVITY_CLEAR_TOP的Activity时,会检测回退栈是否已经有该窗口了,如果有就会清空压在上面的窗口和自己本身,然后在新建⼀个新的窗口压入回退栈。
       例如:如果窗口A跳转到B,B跳转到C,C又调转到B(跳转时给Intent设置了Intent.FLAG_ACTIVITY_CLEAR_TOP),此时回退栈顺序为:A-B,并且会执行B的onCreate方法。
       当Intent.FLAG_ACTIVITY_CLEAR_TOP与Intent.FLAG_ACTIVITY_SINGLE_TOP 同时使用,就与在launchMode设置singleTask,并跳转到程序内部的窗口的作用是⼀样的。
       示例代码:

Intent intent = new Intent(this, MainActivity.class);
//将Activity栈中处于MainActivity主页⾯之上的Activity都弹出。
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

PendingIntent

       PendingIntent是Intent的一种特殊类型,它可以在将来触发Intent。PendingIntent通常用于在特定条件下触发某个操作,例如在特定时间、特定位置或特定事件发生时触发。
       PendingIntent可以用于启动Activity、Service、BroadcastReceiver等组件,也可以用于发送通知。

常用方法

public static PendingIntent getActivity(Context context, int requestCode,Intent intent, @Flags int flags)
该意图触发时,效果相当于context.startActivity(intent)

public static PendingIntent getService(Context context, int requestCode,@NonNull Intent intent, @Flags int flags)
该意图触发时,效果相当于context.startService(intent)

public static PendingIntent getBroadcast(Context context, int requestCode,Intent intent, @Flags int flags)
该意图触发时,效果相当于context.sendBroadcast(intent)

方法参数
  1. context:上下文对象,通常是当前的Activity或Service的上下文。
  2. requestCode:请求码,用于区分不同的PendingIntent。如果多个PendingIntent的requestCode相同,那么它们会被认为是同一个PendingIntent。
  3. intent:要执行的操作,一个Intent对象。可以通过Intent指定要启动的Activity、要发送的Broadcast或要执行的Service。
  4. flags:标志位,用于指定PendingIntent的行为。常用的标志位有:
    • PendingIntent.FLAG_CANCEL_CURRENT:如果PendingIntent已经存在,则取消当前的PendingIntent,重新创建一个新的PendingIntent。
    • PendingIntent.FLAG_NO_CREATE:如果PendingIntent已经存在,则返回null,而不是重新创建一个新的PendingIntent。
    • PendingIntent.FLAG_ONE_SHOT:只能使用一次,使用后会自动取消。
    • PendingIntent.FLAG_UPDATE_CURRENT:如果PendingIntent已经存在,则更新当前的PendingIntent。
代码实例
  • 创建
private PendingIntent getPendingIntent(int id) {
    Intent intent = new Intent(context, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
  • 使用
//发送通知
NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel("notification_debug_channel", "notification_debug", importance);
mManager.createNotificationChannel(channel);
int icon = R.drawable.ic_notifications;
Notification.Builder notification = new Notification.Builder(context, "notification_debug_channel");
notification.setCategory(type);
notification.setSmallIcon(icon);
notification.setContentTitle(type);
notification.setContentText("这是" + type + "类型的通知");
notification.setContentIntent(getPendingIntent(notiId));
mManager.notify(notiId, notification.build());
  • 触发
public void send() throws CanceledException {
    send(null, 0, null, null, null, null, null);
}

       当执行PendingIntent的send()方法后,跟随调用关系,最后会调用到PendingIntentRecord类中的sendInner()方法,简单看一下:

public int sendInner(int code, Intent intent, String resolvedType, IBinder whitelistToken,
            IIntentReceiver finishedReceiver, String requiredPermission, IBinder resultTo,
            String resultWho, int requestCode, int flagsMask, int flagsValues, Bundle options) {
     ...............
            switch (key.type) {
                case ActivityManager.INTENT_SENDER_ACTIVITY:
                    try {
                        if (key.allIntents != null && key.allIntents.length > 1) {
                            res = controller.mAtmInternal.startActivitiesInPackage(xxxx);
                        } else {
                            res = controller.mAtmInternal.startActivityInPackage(xxx);
                        }
                    } catch (RuntimeException e) {
                        Slog.w(TAG, "Unable to send startActivity intent", e);
                    }
                    break;
                case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
                    controller.mAtmInternal.sendActivityResult(-1, key.activity, key.who,
                                key.requestCode, code, finalIntent);
                    break;
                case ActivityManager.INTENT_SENDER_BROADCAST:
                    try {
                        int sent = controller.mAmInternal.broadcastIntentInPackage(xxxx);
                        if (sent == ActivityManager.BROADCAST_SUCCESS) {
                            sendFinish = false;
                        }
                    } catch (RuntimeException e) {
                        Slog.w(TAG, "Unable to send startActivity intent", e);
                    }
                    break;
                case ActivityManager.INTENT_SENDER_SERVICE:
                case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
                    try {
                        controller.mAmInternal.startServiceInPackage(xxxxxx);
                    } catch (RuntimeException e) {
                        Slog.w(TAG, "Unable to send startService intent", e);
                    } catch (TransactionTooLargeException e) {
                        res = ActivityManager.START_CANCELED;
                    }
                    break;
            }

       可以看到,在该方法内会根据type来进行对应的意图触发,type在是创建PendingIntentRecord的时候传入的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容