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. android 判断点击的位置是不是在指定的view上

    private boolean inRangeOfView(View view, MotionEvent ev){ int[] location = new int[2]; view.getLocat ...

  2. 一个C#操作Excel类,功能比较全

    using System; using System.Data; using System.Configuration; using System.Web; using Microsoft.Offic ...

  3. mongodb更新数组中的所有匹配项

    假如集合中有如下数据 { "_id" : ObjectId("4b97e62bf1d8c7152c9ccb74"), "title" : & ...

  4. 【小白的CFD之旅】22 好网格与坏网格

    网格疏密网格形状其他的一些问题小白的总结郑重申明 网格的作用如此重要,以至于小白纠结了很久.小白知道网格划分过程很大程度上受制于计算资源的限制,但小白还是不太明白,如果计算资源非常充足,不用顾忌资源限 ...

  5. VS2015终极卸载方法

    今天打开VS2015发现出问题了,总是停止响应,去控制面板里卸载结果像下面这样,卸载出错!于是我有开始折腾了,重新安装一遍然后,还是有问题,在卸载还是出错于是我决定通过安装介质卸载,结果,悲剧的是,启 ...

  6. [Windows Azure] Data Management and Business Analytics

    http://www.windowsazure.com/en-us/develop/net/fundamentals/cloud-storage/ Managing and analyzing dat ...

  7. 【Spring】Spring Session的简单搭建与源码阅读

    搭建一个简单的Spring Session例子 引入依赖包 <dependencies> <dependency> <groupId>org.springframe ...

  8. sql2008修改数据库文件名称

    例如我们有数据库a,需修改成b,包括文件名称也修改 1.备份数据a 选择数据库->右键->任务->备份,备份出a.bak 2.右键->任务->还原->数据库,跳出“ ...

  9. electron 创建右键菜单

    1.引入模块 const Electron = require('electron'); const remote = Electron.remote; const Menu = remote.Men ...

  10. hive一行变多行及多行变一行

    hive一行变多行及多行变一行 场景 name alias zhaoqiansun abc def ghi 处理数据时需要将上表处理成为下面的形式: name alias zhaoqiansun ab ...