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使用的更多相关文章

  1. Android学习之 Intent详解

    一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...

  2. (转载)Android学习之Intent使用

    ndroid学习之Intent使用   1.使用显示Intent Intent intent = new Intent(FirstActivity.this,SecondActivity.class) ...

  3. android学习五 Intent

    1.Intent是组件间调用的桥梁. 2.Android系统定义了很多Intent    http://developer.android.com/guide/components/intents-c ...

  4. Android学习笔记-Intent(一)

    Intent对象在Android官方API这样描述:It is a passive data structure holding an abstract description of an opera ...

  5. Android学习之Intent传递数据

    Intent在Activity中的作用主要是有两个: 1.启动目标Activity 2.传递数据 Intent在传递数据时分两种情况:向下一个Activity传递数据和从下一个Activity返回数据 ...

  6. Android学习笔记--Intent

    Intent是android四大组件之间交互的一种重要方式.Intent可以指明当前要执行的动作,也可以指明要传递的数据.Intent可以用来启动活动,启动服务,发送广播. Intent分为两种:1. ...

  7. Android学习笔记Intent二

    上篇随笔大概写了了Intent学习的大纲,这篇通过代码理解下Intent的ComponentName属性的使用 ComponentName,中文意思是组件名称,通过Intent的setsetCompo ...

  8. android学习相关intent和fragment的先关知识点

    对于使用intent,主要是用来进行活动之间的跳转,然后就是通过intent向下一个活动传递数据,还有就是想上一个活动传递数据. 实例: 先添加按钮的点击事件,当点击按钮时进行事件的触发,主要代码是 ...

  9. android学习日记05--Activity间的跳转Intent实现

    Activity间的跳转 Android中的Activity就是Android应用与用户的接口,所以了解Activity间的跳转还是必要的.在 Android 中,不同的 Activity 实例可能运 ...

随机推荐

  1. iOS-一个弹出菜单动画视图开源项目分享

    相似于Tumblr公布button的弹出视图 使用非常easy: 初始化: @property (nonatomic, strong) XWMenuPopView *myMenuPopView; - ...

  2. Android-Cannot merge new index 66195 into a non-jumbo instruction的解决办法

    转载请注明来源:http://blog.csdn.net/goldenfish1919/article/details/33729679 用eclispe打包的时候报错: [2014-06-23 13 ...

  3. 如何从angular2中的url获取查询参数?

    问题描述 我使用angular2.0.0-beta.7.当组件在像”/path?query=value1″这样的路径上加载时,它被重定向到”/path”.为什么删除了GET参数?如何保留参数? 路由器 ...

  4. rpm_快速安装saltstake

    安装EPEL源:(mast和minion都需要安装) [root@c02 src]# wget http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-re ...

  5. mongoose修改数组中某个特定的值

    写博客的时候有一个这样的业务,一个标签集合和一个文章集合,它们是多对多的关系,文章集合中tags字段包含它对应的标签,现在修改标签集合中某条标签记录的名字,文章集合中所有包含这个标签的tags字段的值 ...

  6. Apache Flink Training and sample code

    http://training.data-artisans.com/ https://github.com/dataArtisans/blog-post-code-samples https://gi ...

  7. Python Pycharm连接Ubantu Python环境

    由于我习惯在window下开发,但是代码环境布局在Ubantu.使用Python,为了方便程序的调试,尝试在Windows下的Pycharm远程连接到Ubantu虚拟机下的Python环境. 1.准备 ...

  8. THP Transparent HugePages 相关知识与关闭【转】

    最近遇到个LINUX系统内存比较大,未开 HugePages,业务有变化导致ORACLE连接数剧增至上千个,PageTables达到上百G,导致内存不足系统HANG住的案例. 因此需要开启 HugeP ...

  9. 一、图解Java中String不可变性

    这里有一堆例子来说明Java的String的不可变性. 1.声明一个String String s = "abcd"; s 变量保存string对象的引用,下面的箭头解释成保存了哪 ...

  10. 【ARM】定时器

    PWM定时器 PWN:脉冲宽度调制 每个定时器都有一个专用的由定时器时钟驱动的16位递减计数器.当递减计数器的计数值达到0的时候,就会产生定时中断请求来通知CPU定时器操作完成.当定时器递减计数器达到 ...