Intent用法实例
来源:互联网 发布:淘宝正品代购店铺推荐 编辑:程序博客网 时间:2024/06/07 02:52
1.无参数Activity跳转
Intent it = new Intent(Activity.Main.this, Activity2.class);startActivity(it);
2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)
Intent it = new Intent(Activity.Main.this, Activity2.class);Bundle bundle=new Bundle();bundle.putString("name", "This is from MainActivity!");it.putExtras(bundle); // it.putExtra(“test”, "shuju”);startActivity(it); // startActivityForResult(it,REQUEST_CODE);
对于数据的获取可以采用:
Bundle bundle=getIntent().getExtras();String name=bundle.getString("name");
3.向上一个Activity返回结果(使用setResult,针对
startActivityForResult(it,REQUEST_CODE)启动的Activity)
Intent intent=getIntent(); Bundle bundle2=new Bundle(); bundle2.putString("name", "This is from ShowMsg!"); intent.putExtras(bundle2); setResult(RESULT_OK, intent);
4.回调上一个Activity的结果处理函数(onActivityResult)
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode==REQUEST_CODE){ if(resultCode==RESULT_CANCELED) setTitle("cancle"); else if (resultCode==RESULT_OK) { String temp=null; Bundle bundle=data.getExtras(); if(bundle!=null) temp=bundle.getString("name"); setTitle(temp); } } }
Intent-Filter的定义
IntentFilter就是用于描述intent的各种属性, 比如action, category等
一些属性设置的例子:
<action android:name="com.example.project.SHOW_CURRENT" /><category android:name="android.intent.category.DEFAULT" /><data android:mimeType="video/mpeg" android:scheme="http" . . . /><data android:mimeType="image/*" /><data android:scheme="http" android:type="video/*" />
关于IntentFilter的几点注意事项:
(1).android.intent.action.MAIN 与 android.intent.category.LAUNCHER
android.intent.action.MAIN决定一个应用程序最先启动那个组件
android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里(说白了就是是否在桌面上显示一个图标)
这两个属性组合情况:
第一种情况:有MAIN,无LAUNCHER,程序列表中无图标
原因:android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里
第二种情况:无MAIN,有LAUNCHER,程序列表中无图标
原因:android.intent.action.MAIN决定应用程序最先启动的Activity,如果没有Main,则不知启动哪个Activity,故也不会有图标出现
所以这两个属性一般成对出现。
如果一个应用中有两个组件intent-filter都添加了android.intent.action.MAIN和
android.intent.category.LAUNCHER这两个属性, 则这个应用将会显示两个图标, 写在前面的组件先运行。
(2).关于隐式intent
每一个通过 startActivity() 方法发出的隐式 Intent 都至少有一个 category,就是 “android.intent.category.DEFAULT”,所以只要是想接收一个隐式 Intent 的 Activity 都应该包括 “android.intent.category.DEFAULT” category,不然将导致 Intent 匹配失败.
比如说一个activity组件要想被其他组件通过隐式intent调用, 则其在manifest.xml中的声明如下:
<activity android:name="com.gesture.QGestureListActivity"> <intent-filter> <action android:name="com.google.test" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
(3).关于intent-filter匹配优先级
首先查看Intent的过滤器(intent-filter),按照以下优先关系查找:action->data->category
(4).android.intent.category.LAUNCHER与android.intent.category.HOME的区别
android.intent.category.LAUNCHER:android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里,就是android开机后的主程序列表。
android.intent.category.HOME:按住“HOME”键,该程序显示在HOME列表里。
- Intent用法实例
- 一些Intent用法实例
- Intent用法实例
- Intent用法实例
- Intent 用法具体实例
- 一些Intent用法实例
- Intent的实例用法
- Android:intent用法实例
- Intent用法实例
- intent 用法 实例
- Intent用法实例
- Intent用法实例
- Intent用法实例
- Intent用法实例
- Intent用法实例
- Intent用法实例
- android的Intent用法实例
- android的Intent用法实例
- 条款一:*{margin:0; padding:0;},清除有可能的默认脏边距;
- AndroidManifest.xml清单配置文件的相关介绍一
- c++ 中 const_iterator 和 const vector<>::iterator的区别
- Node.js环境搭建和学习(windwos环境)
- 全屏滚动图片网站制作(HTML5+JS+jQuery)——(jQuery篇)
- Intent用法实例
- 全局变量和静态本地变量
- 采集数据
- MySQL的备份与还原
- Dijkstra算法与Prim算法的异同
- CentOS6一键升级GCC4.8
- Genymotion模拟器及其相关虚拟机下载地址
- Android Context 是什么?
- App更新策略课程-实现通知栏进度更新&消息提醒