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. QT-解除connect

    前言:解除关联. 一.新建工程 二.新建部件 在ui设计界面拖入一个line edit,一个label以及两个button按钮 右键“关联”按钮转到槽,选择clicked(),添加如下代码: void ...

  2. Jquery 随笔

    jQuery中 遍历 var arr = ['a','b','c'];    $.each(arr,function(k,v){    console.log(k); //键 console.log( ...

  3. X Macro

    30年前我念大学时从一个朋友那里学来的一个技巧. 它是汇编语言的一个宏,但很容易转换为C语言宏. 我一直在使用它,但有意思的是我还从没在别人的代码中看到过.现在该我把这个小技巧传递下去了. 让我们举个 ...

  4. web_测试用例注意点

    测试是一种思维,包括情感思维和智力思维,情感思维主要体现在一句俗语:思想决定行动上(要怀疑一切),智力思维主要体现在测试用例的设计上.具有了这样的思想,就会找出更多的bug.(^_^个人认为,不代表官 ...

  5. selenium基础

    浏览器 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转.输入.点击.下拉等来拿到网页渲染之后的结果,可支持多种浏览器 官网链接:http://selenium-python.re ...

  6. 构造器参数过多时考虑使用构建器(Builder)

    一.静态工厂和构造器的局限性 面对需要大量可选参数才能构建对象时,静态工厂和构造器并不能随着可选参数的增加而合理扩展. 假设创建一个类Person需要使用大量的可选参数,其中两个参数是必填的,剩下的都 ...

  7. 工作流Activiti学习地址

    http://blog.csdn.net/xnf1991/article/details/52610277

  8. luoguP2742 【模板】二维凸包 / [USACO5.1]圈奶牛 二维凸包

    我们知道,纵坐标最小的点一定在凸包上(如果有多个,那它们都会被取到) 随便找一个纵坐标最小的点,将其他所有点按照这个点为原点极角排序,我们发现极角大的会在极角小的后面加入(感性认知一下) 考虑新(加入 ...

  9. [转载] C 陷阱与缺陷( C traps and Pitfalls )

    本文转自 https://www.xuebuyuan.com/1951579.html 自己找工作过程中复习过的书包括<C traps and Pitfalls>,<编程珠玑> ...

  10. python hashlib、configparse、logging

    一.hashlib 1.Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等.     2.摘要算法 通过摘要函数f()对任意长度的数据data计算出固定长度的摘要digest,目 ...