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
标签: AndroidIntent
好文要顶 关注我 收藏该文  
1
0
 
 
 
posted @ 2014-12-17 19:06 zhouhb 阅读(16663) 评论(0)  编辑 收藏
 
 
发表评论

昵称:

评论内容:
     
 

退出 订阅评论

 

[Ctrl+Enter快捷键提交]

 
 
 
 

(转载)Android学习之Intent使用的更多相关文章

  1. Android学习之 Intent详解

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

  2. android学习五 Intent

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

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

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

  4. (转载)Android学习笔记⑨——android.content.ActivityNotFoundException异常处理

    异常1:Java.lang.ClassNotFoundException 08-13 18:29:22.924: E/AndroidRuntime(1875):Caused by: Java.lang ...

  5. Android学习之Intent传递数据

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

  6. Android学习之Intent使用

    1.使用显示Intent Intent intent = new Intent(FirstActivity.this,SecondActivity.class); startActivity(inte ...

  7. 转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)

      1.什么是蓝牙  Bluetooth是目前使用的最广泛的无线通讯协议之一  主要针对短距离设备通讯(10米)  常用于连接耳机.鼠标和移动通讯设备等 2.发现周围蓝牙设备  BluetoothAd ...

  8. Android学习笔记--Intent

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

  9. Android学习笔记Intent二

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

随机推荐

  1. Memcache相关面试题

    1)memcached的cache机制是怎样的? 懒惰算法 +最近最少使用原则 2)memcached如何实现冗余机制? 冗余:就是有好多好多不经常使用的. 可以不用实现冗余机制,如果非要实现.那就搞 ...

  2. this self指针

    this 和 self指针 为函数提供了运行上下问:为函数提供了当前对象的其实地址,方便函数的对对象的访问.

  3. gbk编码

    GBK 编码   GBK编码范围:8140-FEFE,汉字编码范围见第二节:码位分配及顺序. GBK编码,是对GB2312编码的扩展,因此完全兼容GB2312-80标准.GBK编码依然采用双字节编码方 ...

  4. 第九章 Python之面向对象

    面向对象编程 面向对象编程是一种程序设计思想,它把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数 面向过程的程序设计把计算机程序视为一系列命令的集合,即一组函数的顺序执行.为了简化程序设计 ...

  5. day09-1 列表,元祖的内置方法

    目录 列表类型的内置方法 作用 定义方式 方法 优先掌握 需要掌握 储存一个值or多个值 有序or无序?(有序:有索引, 无序:无索引) 可变or不可变(可变:值变id不变,不可变:值变id也变) 元 ...

  6. 基础——(4)SR Latch(SR锁存器)

    Digital logic gets really interesting when we connect the output of gates back to an input. The SR l ...

  7. bzoj 1814 Fornula 1

    Formula 1 题意 在\(n*m\)的矩阵中,有些格子有树,没有树的格子不能到达,找一条回路,吃完所有的树,求有多少种方法. 解法 因为只要一条回路,所以我们必须维护插头的连通性. 具体的可以参 ...

  8. man帮助命令

    linux系统提供可比较丰富的帮助手册,man是manual(说明书)的缩写,在日常的linux系统管理中经常会用到. 语法: man(选项)(参数) 选项: -a:在所有的man帮助手册中搜索: - ...

  9. linux中一次创建多个目录

    linux中创建目录当然使用命令工具mkdir == (make directory),联想记忆法能让你记得牢固. 如果你要创建几个目录,例如:dir1目录,dir2目录,dir3目录可以这样 mkd ...

  10. django-3-模板变量,过滤器,静态文件的引用

    <<<模板变量>>> (1)定义视图函数 通过context传递参数来渲染模板,context要是个字典 当模板变量为可调用对象的时候,函数不传递参数 (2)配置模 ...