(转载)Android学习之Intent使用
ndroid学习之Intent使用
1、使用显示Intent
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);
上述代码的作用是打开活动SecondActivity
2、使用隐式Intent
首先打开AndroidManifest.xml,添加代码:
<activity
android:name="com.example.activitytest.SecondActivity"
android:label="@string/title_activity_second" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>
然后修改FirstActivity中按钮的点击事件:
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent("com.example.activitytest.ACTION_START");
intent.addCategory("com.example.activitytest.MY_CATEGORY");
startActivity(intent);
}
});
还可以使用隐式Intent,启动其他程序的活动。
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.cnblogs.com/zhouhb/"));
//Intent intent=new Intent(Intent.ACTION_DIAL);
//intent.setData(Uri.parse("tel:12345"));
3、使用Intent在Activity之间传递数据
3.1 从FirstActivity向SecondActivity传递数据
String s="from first";
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("data", s);
startActivityForResult(intent, 1);
3.2 SecondActivity接收传递来的数据
Button btn=(Button)findViewById(R.id.button2);
Intent intent=getIntent();
String string=intent.getStringExtra("data");
btn.setText(string);
3.3 SecondActivity向FirstActivity返回数据
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
returnData();
}
});
private void returnData() {
Intent intent=new Intent();
intent.putExtra("returnData", "from Second");
setResult(RESULT_OK,intent);
finish();
}
//如果用户不是通过点击按钮,而是按下Back键回到FirstActivity,则重写 onBackPressed
public void onBackPressed() {
// TODO Auto-generated method stub
returnData();
}
3.4 在FirstActivity中重写onActivityResult得到SecondActivity返回的数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case 1:
if(resultCode==RESULT_OK){
String string=data.getStringExtra("returnData");
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
(转载)Android学习之Intent使用的更多相关文章
- Android学习之 Intent详解
一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...
- android学习五 Intent
1.Intent是组件间调用的桥梁. 2.Android系统定义了很多Intent http://developer.android.com/guide/components/intents-c ...
- Android学习笔记-Intent(一)
Intent对象在Android官方API这样描述:It is a passive data structure holding an abstract description of an opera ...
- (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理
异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...
- Android学习之Intent传递数据
Intent在Activity中的作用主要是有两个: 1.启动目标Activity 2.传递数据 Intent在传递数据时分两种情况:向下一个Activity传递数据和从下一个Activity返回数据 ...
- Android学习之Intent使用
1.使用显示Intent Intent intent = new Intent(FirstActivity.this,SecondActivity.class); startActivity(inte ...
- 转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)
1.什么是蓝牙 Bluetooth是目前使用的最广泛的无线通讯协议之一 主要针对短距离设备通讯(10米) 常用于连接耳机.鼠标和移动通讯设备等 2.发现周围蓝牙设备 BluetoothAd ...
- Android学习笔记--Intent
Intent是android四大组件之间交互的一种重要方式.Intent可以指明当前要执行的动作,也可以指明要传递的数据.Intent可以用来启动活动,启动服务,发送广播. Intent分为两种:1. ...
- Android学习笔记Intent二
上篇随笔大概写了了Intent学习的大纲,这篇通过代码理解下Intent的ComponentName属性的使用 ComponentName,中文意思是组件名称,通过Intent的setsetCompo ...
随机推荐
- Python multiprocessing.Manager介绍和实例(进程间共享数据)
Python中进程间共享数据,处理基本的queue,pipe和value+array外,还提供了更高层次的封装.使用multiprocessing.Manager可以简单地使用这些高级接口. Mana ...
- 基础apache命令
在启动Apache服务之前,可以使用下面的命令来检查配置文件的正确性. C:\Apache2.2\bin> httpd -n Apache2.2 -t 还可以通过命令行控制Apache服务 ...
- golden gate的DDL配置
DDL复制的配置 目前只支持oracle和teradata的ddl复制 oracle能复制除了系统对象之外的所有对象 两种配置方法: 基于trigger的DDL:对于生产库有一定影响. 原理: 源库建 ...
- oracle导入导出操作
1,获取oracle导入导出帮助: imp help=y 2,导出命令 exp 用户名/密码@数据库实例 file=文件路径名 如: exp sys/password@orcl file=d:\dat ...
- jsonp模仿了得一个百度搜索框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 史上最低,低到尘埃,CDR邀你一起嗨购618
盼呀盼,望穿秋水~盼呀盼,何时降价~ 6.4开始,CDR X6全民狂欢618放价活动全面开启 力度之大,范围之广,时间之久,价格之低,都是前所未有的 不负众望,这个618,CDR真的做到一降到底,没有 ...
- HDU 1047 Integer Inquiry( 高精度加法水 )
链接:传送门 思路:高精度水题 /************************************************************************* > File ...
- fflush()函数总结
1. 概述 函数名: fflush() 功 能: 清除读写缓冲区,需要立即把输出缓冲区的数据进行物理写入时 头文件: stdio.h 原型: int fflush(FILE *stream),其中st ...
- python的父类和子类中关于继承的不同版本的写法
Python 2.7中的继承 在Python 2.7中,继承语法稍有不同,ElectricCar 类的定义类似于下面这样: class Car(object): def __init__(self, ...
- PHP下的异步尝试二:初识协程
PHP下的异步尝试系列 如果你还不太了解PHP下的生成器,你可以根据下面目录翻阅 PHP下的异步尝试一:初识生成器 PHP下的异步尝试二:初识协程 PHP下的异步尝试三:协程的PHP版thunkify ...



