Android Intent和Intent构造器
来源:互联网 发布:知乎药丸 编辑:程序博客网 时间:2024/06/11 15:34
一、Intent介绍
Intent 是一个消息传递对象,基本用例包括启动Activity、启动服务、传递广播
二、Intent类型
Intent类型包括显式Intent和隐式Intent
显式Intent:即直接指定需要打开的activity对应的类
隐式Intent:即不是像显式的那样直接指定需要调用的Activity,隐式不明确指定启动哪个Activity,而是设置Action、Data、Category,让系统来筛选出合适的Activity。筛选是根据所有的intent-filter来筛选
设置Intent时所传入的类必须要注册,隐性设置Intent时需要设置action和category 属性,常见的category 属性如下
http://blog.csdn.net/qq_22989777/article/details/52936675
三、显示Intent举例
1、构造方法传入Component
Intent intent = new Intent(this, SecondActivity.class); startActivity(intent);
2、setComponent方法
ComponentName componentName = new ComponentName(this, SecondActivity.class); // 或者ComponentName componentName = new ComponentName(this, "com.example.app016.SecondActivity"); // 或者ComponentName componentName = new ComponentName(this.getPackageName(), "com.example.app016.SecondActivity"); Intent intent = new Intent(); intent.setComponent(componentName); startActivity(intent);
3、setClass/setClassName方法
Intent intent = new Intent(); intent.setClass(this, SecondActivity.class); // 或者intent.setClassName(this, "com.example.app016.SecondActivity"); // 或者intent.setClassName(this.getPackageName(), "com.example.app016.SecondActivity"); startActivity(intent);
四、隐式Intent举例
设置一个Action,
<activity android:name="com.example.app016.SecondActivity"> <intent-filter> <action android:name="abcdefg"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity>
然后,在MainActivity中有如下方法设置
1、setAction方法
Intent intent = new Intent(); intent.setAction("abcdefg"); startActivity(intent);
2、构造方法直接设置Action
Intent intent = new Intent("abvdefg");startActivity(intent);
其他程序可以调用这个Activity只要使用这个Action字符串即可,我们也可以调用其他程序的Action例如:
Intent intent = new Intent(Intent.ACTION_DIAL); // 或者Intent intent = new Intent("android.intent.action.DIAL"); // Intent.ACTION_DIAL是内置常量,值为"android.intent.action.DIAL" startActivity(intent);
我们可以调用拨号界面
3、一个Activity可调用多个Action
如果我们设置的intent-filter中的Action为”ACTION_DIAL”那么在启动该Intent时会自动跳出选择界面,而且一个Activity中可以处理多个Action
<activity android:name="com.example.app016.SecondActivity"> <intent-filter> <!-- 可以处理下面三种Intent --> <action android:name="com.example.app016.SEND_EMAIL"/> <action android:name="com.example.app016.SEND_MESSAGE"/> <action android:name="com.example.app016.DAIL"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
如果没有该Activity系统会抛出ActivityNotFoundException异常,我们应该要用try/catch捕获或者也可以使用Intent的resolveActivity方法判断这个Intent是否能找到合适的Activity
Intent intent = new Intent(Intent.ACTION_DIAL); if(intent.resolveActivity(getPackageManager()) == null) { // 设置控件不可用 }
注意resolveActivity方法返回值就是显式Intent上面讲到的ComponentName对象,如果有多个Activity那么返回用户所选择的对象
Intent intent = new Intent(Intent.ACTION_DIAL); ComponentName componentName = intent.resolveActivity(getPackageManager()); if(componentName != null) { String className = componentName.getClassName(); Toast.makeText(this, className, Toast.LENGTH_SHORT).show(); }
- Android Intent和Intent构造器
- Android Intent 和 Intent Filter
- Android--Intent和Intent Filters
- 【android】Intent 和 Intent Filter
- Android Intent和Intent filter
- Android -- Intent 和 Intent 过滤器
- Android Intent和Intent过滤器
- Intent方法 介绍和构造
- android intent 和 intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- android intent和intent action大全
- 类装载器学习
- 最大连续子数组
- [Hive]客户端几种数据导入方式
- AndroidStudio怎样导入library项目开源库
- linux用户&群组&权限
- Android Intent和Intent构造器
- Gulp开发教程(翻译)
- 简单DP HDOJ 1003 MAX SUM
- windows 7 Shell_NotifyIcon 使用的问题
- 安卓中控件跟随手指移动
- UIViewController view视图的加载
- unity3d : Failed to query D3D11 context for ID3DUserDefinedAnnotation interface (hr = 0x80004002)
- Android快速实现微信支付
- java 字符串匹配 删除 img 的width height属性