Android学习——Intent
2015-09-21 22:12
531 查看
Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。
本节主要介绍Intent如何实现两个Activity间的跳转,以及传递数据。
5.1 显式意图与隐式意图
activity_main.xml:
activity_reg.xml:
MainActivity.java:
RegActivity.java:
运行:
点击添加按钮,输入aaaaaa再点保存:
回到MainActivity查看,aaaaaa并没有显示在界面上。点击返回键,退出程序,再重新进入,发现aaaaaa已经显示出来了。
说明:static的数据不会随着程序的退出而消失,而是存在后台缓存中。可以在设置 - - >程序– >缓存进程中查看。因此不推荐写过多的static数据。
新添加的数据并没有立即显示在界面上,需要用otifyDataSetChanged方法。该方法的作用是当适配器的内容发生改变时强制重新加载Activity可以实现动态的刷新ListView的功能。
区别:显式意图与隐式意图:
显示意图:可以看见跳转目标,只能跳到当前应用的Activity
隐式意图:不能看到跳转目标,可以跳到其他应用的Activity
显示意图:
Intent intent = new Intent(this,OneActivity.class);//同一个工程的Activity
隐式意图:
Intent intent = new Intent(“com.my.reg”);//其他工程中的RegActivity
在RegActivity所在工程的AndroidMainfest.xml配置文件的activity标签添加:
注意:有多个隐式意图一样的话,会让选择
注意:该工程要重新发布一下,要不然运行时会报错。
5.2 传递数据
一个Activity放String数据:
另一个Activity取数据:
5.1中传递数据也可以按照如下写法:
Intent对象创建的三种方式:
(1)构造方法
(2)setComponent
(3)setClassName
5.3 Intent的Category属性
主要介绍以下三种:
LANCHER:使用这个种类来让一个 Activity 作为应用程序的启动项。
DEFAULT:表示让组件成为 Intent Filter 中定义的 data 的默认动作。这对使用显式 Intent 启动的 Activity 来说也是必要的。
HOME:表示放回到Home界面
5.4 action属性
(1)Activity2有两个action属性,那么通过”www.aa.com”或者”www.bb.com”都可以跳转到Activity2。
(2)Activity2和Activity3的action属性相同,都是”www.bb.com”,在实际跳转的时候会让选择跳转到哪个Activity。如下图:
AndroidMainfest.xml文件配置内容:
如果给Activity3添加一个data属性,则Activity2和Activity3就可以区分开。如:
如果再新建一个Activity4,属性如下:
那么则会创建两个桌面图标,通过桌面上的Activity4图标也可以访问。
5.5 intent的Flag属性:
在 android.content.Intent 中一共定义了20种不同的 flag,其中和 Task 紧密关联的有四种:
FLAG_ACTIVITY_CLEAR_TOP:清除栈顶,和singleTop类似
FLAG_ACTIVITY_NO_HISTORY: 没有历史记录
FLAG_ACTIVITY_NEW_TASK: 与taskAffinity配合使用
FLAG_ACTIVITY_SINGLE_TOP:与singleTask类似
使用方式:intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
5.6 系统action
本节主要介绍Intent如何实现两个Activity间的跳转,以及传递数据。
5.1 显式意图与隐式意图
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <ListView android:id="@+id/listView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1"/> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:onClick="add" android:text="添加" /> </RelativeLayout>
activity_reg.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/edt_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_marginTop="18dp" android:ems="10" android:hint="请输入用户名"> <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edt_name" android:layout_centerHorizontal="true" android:layout_marginTop="49dp" android:onClick="save" android:text="保存" /> </RelativeLayout>
MainActivity.java:
public class MainActivity extends Activity { private ListView listView; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) this.findViewById(R.id.listView1); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, UserDao.users);; listView.setAdapter(adapter); } public void add(View v) { Intent intent = new Intent(this,RegActivity.class); startActivity(intent); } }
RegActivity.java:
public class RegActivity extends Activity { private EditText edt_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reg); edt_name = (EditText) this.findViewById(R.id.edt_name); } public void save(View v){ UserDao.users.add(edt_name.getText().toString()); this.finish(); } }
运行:
点击添加按钮,输入aaaaaa再点保存:
回到MainActivity查看,aaaaaa并没有显示在界面上。点击返回键,退出程序,再重新进入,发现aaaaaa已经显示出来了。
说明:static的数据不会随着程序的退出而消失,而是存在后台缓存中。可以在设置 - - >程序– >缓存进程中查看。因此不推荐写过多的static数据。
新添加的数据并没有立即显示在界面上,需要用otifyDataSetChanged方法。该方法的作用是当适配器的内容发生改变时强制重新加载Activity可以实现动态的刷新ListView的功能。
@Override protected void onResume() { super.onResume(); adapter.notifyDataSetChanged(); }
区别:显式意图与隐式意图:
显示意图:可以看见跳转目标,只能跳到当前应用的Activity
隐式意图:不能看到跳转目标,可以跳到其他应用的Activity
显示意图:
Intent intent = new Intent(this,OneActivity.class);//同一个工程的Activity
隐式意图:
Intent intent = new Intent(“com.my.reg”);//其他工程中的RegActivity
在RegActivity所在工程的AndroidMainfest.xml配置文件的activity标签添加:
注意:有多个隐式意图一样的话,会让选择
注意:该工程要重新发布一下,要不然运行时会报错。
5.2 传递数据
一个Activity放String数据:
Intent intent = new Intent(this,OneActivity.class); intent.putExtra("name", "xxxx");//放数据 startActivity(intent);
另一个Activity取数据:
Intent intent = this.getIntent(); String name = intent.getStringExtra("name"); Toast.makeText(this, name, 0).show();
5.1中传递数据也可以按照如下写法:
//RegActivity.java: public void save(View v){ UserDao.users.add(edt_name.getText().toString()); Intent intent = this.getIntent(); //存放要返回的数据 intent.putExtra("name", edt_name.getText().toString()); //返回结果 this.setResult(1000, intent); this.finish(); } //MainActivity.java: public void add(View v) { Intent intent = new Intent(this,RegActivity.class); //跳转到一个指定的Activity,并要返回结果 this.startActivityForResult(intent, 1); } //拿返回的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //唤醒适配器,强制刷新界面 adapter.notifyDataSetChanged(); }
Intent对象创建的三种方式:
(1)构造方法
(2)setComponent
(3)setClassName
5.3 Intent的Category属性
主要介绍以下三种:
LANCHER:使用这个种类来让一个 Activity 作为应用程序的启动项。
DEFAULT:表示让组件成为 Intent Filter 中定义的 data 的默认动作。这对使用显式 Intent 启动的 Activity 来说也是必要的。
HOME:表示放回到Home界面
5.4 action属性
(1)Activity2有两个action属性,那么通过”www.aa.com”或者”www.bb.com”都可以跳转到Activity2。
(2)Activity2和Activity3的action属性相同,都是”www.bb.com”,在实际跳转的时候会让选择跳转到哪个Activity。如下图:
AndroidMainfest.xml文件配置内容:
<activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity2" android:label="@string/title_activity_other" > <intent-filter> <action android:name="www.aa.com" /> <action android:name="www.bb.com" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name=".Activity3" android:label="@string/title_activity_activity3" > <intent-filter> <action android:name="www.bb.com" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
如果给Activity3添加一个data属性,则Activity2和Activity3就可以区分开。如:
如果再新建一个Activity4,属性如下:
<activity android:name=".Activity4" android:label="@string/title_activity_activity4" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
那么则会创建两个桌面图标,通过桌面上的Activity4图标也可以访问。
5.5 intent的Flag属性:
在 android.content.Intent 中一共定义了20种不同的 flag,其中和 Task 紧密关联的有四种:
FLAG_ACTIVITY_CLEAR_TOP:清除栈顶,和singleTop类似
FLAG_ACTIVITY_NO_HISTORY: 没有历史记录
FLAG_ACTIVITY_NEW_TASK: 与taskAffinity配合使用
FLAG_ACTIVITY_SINGLE_TOP:与singleTask类似
使用方式:intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
5.6 系统action
@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Intent intent = new Intent(); Uri uri = null; String data=null; switch (position) { case 0: //查看第一个位置的联系人 data = "content://contacts/people/1"; uri = Uri.parse(data); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); break; case 1: //编辑第一个位置的联系人 data = "content://contacts/people/1"; uri = Uri.parse(data); intent.setAction(Intent.ACTION_EDIT); intent.setData(uri); startActivity(intent); break; case 2: //跳转到拨号界面 data = "tel:15133220000"; uri = Uri.parse(data); intent.setAction(Intent.ACTION_DIAL); intent.setData(uri); startActivity(intent); break; case 3: //直接打电话 data = "tel:15133220000"; uri = Uri.parse(data); intent.setAction(Intent.ACTION_CALL); intent.setData(uri); startActivity(intent); break; case 4: //用浏览器访问百度首页 data = "http://www.baidu.com"; uri = Uri.parse(data); intent.setAction(Intent.ACTION_VIEW); intent.setData(uri); startActivity(intent); break; default: break; } }
相关文章推荐
- android 关联源码
- Android Studio中集成Genymotion模拟器
- Android动画学习笔记-Android Animation
- Android 百度地图(一)——简单使用
- [Android Pro] RecyclerView实现瀑布流效果(二)
- android学习心得:error:Warning: Activity not started, its current task has been brought to the front
- Android Studio 1.2 修改自动保存文件,由用户决定是否要保存
- Android(java)学习笔记262:JNI之工具快速开发步骤
- [Android Pro] 超能RecyclerView组件使用
- Android学习——数据存储
- 【Android】设置 LinearLayout 的样式
- android遍历文件夹并显示在listview中
- Android中ArrayList的使用注意事项以及效率的比较
- Android学习——软件管理器之一
- 解决启动android studio模拟器启动失败之HAXM问题的方法
- Android showing LoadingView or EmptyView for Activity or Fragment
- android学习:使用它ContentProvider获取其他程序共享的数据
- Android Studio 导入第三方源码(以及删除)
- Android showing LoadingView or EmptyView for Activity or Fragment
- 我的Android进阶之旅------>Android项目运行报java.lang.NoClassDefFoundError错误的解决办法