您的位置:首页 > 其它

Intent传值问题(非Integer and String 数据类型)以及 intent使用方法

2010-11-28 00:20 591 查看
Q:在使用Intent进行页面跳转的时候,希望将自己定义的ArrayList数据传递到下一个Activity,请问通过Intent可以实现这种传值吗?如果不可以,有没有什么好的办法?希望大家给点帮助,谢谢啦!
A:
Intent可以传递Integer和String类型的ArrayList
如果不是Integer或者String,感觉只能用这个万能的串行化了
首先定义一个要传递的Class,要引用Serializable接口,类中定义了一个值为eoeMobile的String域

1.package com.eoemobile.sunhr.test;
2.
3.import java.io.Serializable;
4.
5.public class Data implements Serializable{
6.
7./**
8.*
9.*/
10.private static final long serialVersionUID = -6746185864607829411L;
11.
12.private String mValue = null;
13.
14.public Data(){
15.mValue = new String("eoeMobile");
16.}
17.
18.public String getValue(){
19.return mValue;
20.}
21.}


然后在第一个Acrivity——Test1对应的xml中,添加一个Button,Test1的代码如下

1.package com.eoemobile.sunhr.test;
2.
3.import android.app.Activity;
4.import android.content.Intent;
5.import android.os.Bundle;
6.import android.view.View;
7.import android.view.View.OnClickListener;
8.import android.widget.Button;
9.
10.public class Test1 extends Activity {
11./** Called when the activity is first created. */
12.@Override
13.public void onCreate(Bundle savedInstanceState) {
14.super.onCreate(savedInstanceState);
15.setContentView(R.layout.main);
16.
17.Button button = (Button)findViewById(R.id.test1_button);
18.button.setOnClickListener(new OnClickListener(){
19.
20.@Override
21.public void onClick(View v) {
22.//Initialize the object
23.Data data = new Data();
24.//Put object into intent
25.Intent intent = new Intent(Test1.this,Test2.class);
26.intent.putExtra("DATA", data);
27.startActivity(intent);
28.}
29.
30.});
31.}
32.}


在要跳转到的Test2对应的xml中添加一个TextView,Test2代码如下

1.package com.eoemobile.sunhr.test;
2.
3.import android.app.Activity;
4.import android.content.Intent;
5.import android.os.Bundle;
6.import android.widget.TextView;
7.
8.public class Test2 extends Activity {
9.
10.@Override
11.protected void onCreate(Bundle savedInstanceState) {
12.super.onCreate(savedInstanceState);
13.setContentView(R.layout.test2);
14.//Get object
15.Intent intent = this.getIntent();
16.Data data = (Data)intent.getSerializableExtra("DATA");
17.//Output
18.if(data != null){
19.TextView tv = (TextView)findViewById(R.id.test2_text);
20.tv.setText(data.getValue());
21.}
22.}
23.
24.
25.}


我用intent跳转后,用
Bundle data = getIntent().getExtras();
if(data != null){
String str = data.getString("wordId");
}

debug的时候,明明能看到data的值为:Bundle[{wordId=5}]
为何用String str = data.getString("wordId")就是得不到值啊,str为null。
郁闷了。
晕了,搞了我半天,才是传了int型,而我得到的时候用了getString().
得值的时候必须类型对应。

android的intent使用方法

1. 说明
Android中提供了Intent机制来协助应用间或者应用程序内部的交互与通讯。
Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似;另一种是隐式的Intent,即Intent的发送者在构造Intent对象时,并不知道接收者是谁,只是指出接收者的一些特性(比如说启动音乐播放软件)

2. 使用方法

1) 启动服务

a) 关键函数
context.startService()或context.bindService()

b) 示例
Intent i = new Intent(this, MyTestService.class);
this.startService(i); // 启动service

2) 发送广播

a) 关键函数
context.sendBroadcast()

b) 发送方
String msg = “test”;
Intent i = new Intent(“com.test.bc”);
i.pubExtra(“msg”, msg);
this.sendBroadcase(i);

c) 接收方
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
registerReceiver(mReceiver.interFilter);

3) 启动应用程序

a) 关键函数
context.startActivity()

b) 示例
Intent intent =
new Intent(“com.android.browser“, “com.android.browser.BrowserActivity“);
startActivity(intent);

3. Intent的组成
Intent的参数可多可少,系统根据不同的参数组合过滤出一个或多个适合规则的界面

1) 调用方:以下几个规则可以同时指定,也可以指定一部分或几部分
Component:指定包名类名来调用(见上例),它是晚绑定,不会在编译时报错
Action:指定做什么的规则(比如ACTION_DIAL指定拨号类型应用),以供过滤
Data:提供的重要数据,通常是Uri,同时也提供数据的类型,以供过滤
Type:用于指定类型,以供过滤(比如ACTION_VIEW同时指定为Type为Image,则调出浏览图片的应用)
Category:指定范围
Extras:通过Bundle类传参, 数据多,数据量大时用它传
Flags:标志位(比如FLAG_ACTIVITY_NEW_TASK指定新开一个任务)

2) 被调用方
在AndroidManifest.xml中的<intent-filter>中声明规则
例如: 一般程序都需要在inter-filter中加入android.intent.category.LAUNCHER的声明, 以便被程序启动器(Launcher)识别, 即以点击图标的方式供用户运行

3) 示例
Intent intent = new Intent();
intent.setClassName(“com.android.browser“,
“com.android.browser.BrowserActivity“); // 打开浏览器
Uri data = Uri.parse(“http://www.google.com“);
intent.setData(data); // 打开某网页
intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK); // 以新建任务方式打开
intent.setAction(Intent.ACTION_VIEW); // 以浏览方式打开
startActivity(intent);

4. Intent的源码实现

1) Intent解析,过滤规则对应出具体应用
frameworks/base/core/java/android/content/IntentFilter.java

2) Intent定义,规定程序中的使用的Define与xml中字串的对应关系
frameworks/base/core/java/android/content/Intent.java
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐