`

Android学习之Intent接收返回数据

阅读更多

send_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/MyLayout"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<Button
		android:id="@+id/mybut"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="按我将跳转到另一个Activity程序"/> 
	<TextView
		android:id="@+id/msg"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
</LinearLayout>

 

 

 

receive_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/MyLayout"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView
		android:id="@+id/show"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/> 
	<Button
		android:id="@+id/retbut"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="返回数据到Send。"/>	
</LinearLayout>

 

 

 

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_title">Intent操作</string>
    <string name="send_name">发送Intent的Activity程序。</string>
    <string name="receive_name">接收Intent的Activity程序。</string>
</resources>

 

 

 

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" android:versionCode="1" android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />

	<application android:icon="@drawable/icon" android:label="@string/app_title">
		<activity android:name="Send" android:label="@string/app_title">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name="Receive" android:label="@string/receive_name" />
	</application>
</manifest>

 

 

 

Send.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Send extends Activity {
	private Button mybut = null ;						// 按钮组件
	private TextView msg = null ;						// 文本组件
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.send_main);		// 默认布局管理器
		this.mybut = (Button) super.findViewById(R.id.mybut) ;	// 取得组件
		this.msg = (TextView) super.findViewById(R.id.msg) ;	// 取得组件
		this.mybut.setOnClickListener(new OnClickListenerImpl());	// 定义单击事件
	}
	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View view) {
			Intent it = new Intent(Send.this, Receive.class);	// 实例化Intent
			it.putExtra("myinfo", "我的发送信息测试") ;	// 设置附加信息
			Send.this.startActivityForResult(it, 1);						// 启动Activity
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {										// 判断操作类型
		case RESULT_OK:												// 成功操作
			msg.setText("返回的内容是:" + data.getStringExtra("retmsg"));
			break;
		case RESULT_CANCELED:										// 取消操作
			msg.setText("操作取消。");
			break ;
		default:
			break;
		}
	}
}

 

 

 

Receive.java:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Receive extends Activity {
	private TextView show = null ;						// 文本显示组件
	private Button retbut = null ;						// 按钮组件
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.receive_main);	// 调用默认布局管理器
		this.show = (TextView) super.findViewById(R.id.show) ;	// 取得组件
		this.retbut = (Button) super.findViewById(R.id.retbut) ;// 取得组件
		Intent it = super.getIntent() ;					// 取得启动此程序的Intent
		String info = it.getStringExtra("myinfo") ;		// 取得设置的附加信息
		this.show.setText(info) ;						// 设置文本显示信息
		this.retbut.setOnClickListener(new OnClickListenerImpl()) ;	// 设置监听
	}
	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View view) {
			Receive.this.getIntent().putExtra("retmsg", "我的返回信息测试") ;// 返回信息
			// 设置返回数据的状态,RESULT_OK与Send.java中的onActivityResult()里判断的对应
			Receive.this.setResult(RESULT_OK, Receive.this.getIntent()) ;
			Receive.this.finish() ;							// 结束Intent
		}
	}
}

 

分享到:
评论

相关推荐

    Android通过Intent传递数据

    - 使用`startActivityForResult(Intent intent, int requestCode)`时,可以在目标Activity通过`setResult()`返回结果,然后在源Activity中通过`onActivityResult()`接收。 理解并熟练运用Intent在Android开发中至...

    Android应用源码之Intent_Intent.zip

    这个压缩包“Android应用源码之Intent”很可能会包含多个示例项目,演示了Intent的各种用法,包括启动Activity、传递数据、使用Intent Filter等,通过学习这些示例,开发者可以更好地理解和掌握Intent在实际开发中的...

    Android源码——Intent切换.zip

    - `startActivityForResult(Intent intent, int requestCode)`:启动Activity并等待返回结果,适用于需要从被启动的Activity获取数据的场景。 - `startService(Intent intent)`:启动一个Service,Service可以在...

    Android中Intent使用、数据回写(显)

    在Android开发中,Intent是一个非常重要的组件,它用于在应用程序的不同组件之间传递消息,实现活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver)以及Content Provider之间的通信。本篇文章将...

    Android利用Intent启动和关闭Activity

    - 使用Intent传递数据时,可以通过`putExtra()`方法添加额外的信息,然后在接收端使用`getExtra()`获取。 - 在处理完Intent后,及时调用`finish()`关闭Activity,避免内存泄漏和不必要的后台运行。 总的来说,...

    Android代码-Intent切换.zip

    在Android开发中,Intent是一种非常重要的组件,它用于在应用程序的不同组件之间传递消息,实现活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver)以及内容提供者(ContentProvider)之间的交互。...

    Android intent 传递对象以及返回刷新

    当一个Activity完成其任务并返回数据时,通常会通过setResult()方法设置结果Intent,然后调用finish()来结束当前Activity。在启动Activity的Activity中,重写onActivityResult()方法来接收返回的数据并进行刷新操作...

    Android中intent的使用

    在Android应用开发中,Intent是连接应用程序组件之间通信的关键机制,它用于启动其他组件或传递数据。本篇文章将深入探讨Intent的基本概念、类型、构造方法以及如何在Android中有效地使用Intent。 Intent是一种意图...

    android开发中Intent在两个Activity间传值示例

    在这个“android开发中Intent在两个Activity间传值示例”中,我们将深入探讨如何使用Intent在两个Activity之间传递数据。 1. **Intent的基本概念** Intent是一个表示一个动作的意图对象,它包含了一个操作和操作的...

    android 利用intent调用activity 简明精炼的例子

    在启动Activity的原Activity中,我们需要重写`onActivityResult()`方法来接收返回的数据。这个方法包含三个参数:请求码、结果码和Intent。结果码`RESULT_OK`表示成功,`RESULT_CANCELED`表示取消。例如: ```java @...

    Android+Intent机制实例详解.rar

    startActivityForResult()用于需要接收返回结果的情况。 五、启动Service 使用startService(Intent)或bindService(Intent, ServiceConnection, 绑定模式)来启动Service。startService()用于一次性任务,bindService...

    android intent and intent-filters

    2. 发送广播:BroadcastReceiver通过Intent接收系统或应用广播事件,如网络状态变化、电池电量低等。 3. 启动Service:Intent用于启动后台服务,执行长时间运行的任务。 4. 启动App:ACTION_MAIN和CATEGORY_LAUNCHER...

    Android Studio 实验二:Intent的使用

    在Android开发环境中,Intent是应用间通信的重要工具,它用于启动其他组件或传递数据。本实验将深入探讨Android Studio中Intent的使用,帮助你更好地理解如何在不同的Activity之间跳转和传递信息。 首先,让我们...

    android中intent使用示例

    在Android开发中,Intent是一种非常重要的组件间通信机制。它被用来启动活动(Activity)、服务(Service)或者传递数据。本示例将深入探讨Intent的基本用法和常见应用场景。 首先,Intent分为显式Intent和隐式...

    android各组件详解- Intent.

    Intent是Android平台上的核心概念之一,充当着应用程序内部以及不同应用程序之间通信的桥梁。它是一种用于描述即将执行操作的抽象消息,能够激活Android的三大核心组件:活动(Activity)、服务(Service)和广播...

    Android中的Intent

    后者可以接收返回结果,常用于处理用户交互后需要反馈的情况。 5. 启动Service: `startService(Intent)`用来启动Service,Service将在后台持续运行,直到调用`stopService(Intent)`或`stopSelf()`方法。另外,`...

    Android开发 两个Activity之间通过Intent跳转传值

    在SourceActivity的`onActivityResult()`方法中接收返回的数据: ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && ...

    android Intent的用法

    总结,Intent在Android开发中扮演着至关重要的角色,无论是启动组件、传递数据还是实现组件间的通信,都离不开Intent的使用。理解和熟练掌握Intent的用法,是成为一名合格的Android开发者的基础。通过不断地实践和...

    实验七 使用Intent在Activity间传输数据

    或者,如果需要在返回结果后继续执行原Activity的代码,可以使用`startActivityForResult()`,并覆盖`onActivityResult()`方法来接收返回的数据。 4. 隐式Intent的使用: 对于隐式Intent,我们不需要指定具体的...

Global site tag (gtag.js) - Google Analytics