伴随着Android Developers 的开发,再也不用翻墙了,这意味着Android 对中国学习者有着越来越多的官方学习资料,学习起来有更明确的方向和目标。

  Android Developer 官网:https://developer.android.google.cn/index.html

  之前的博客用过Intent ,但没有深讲, 今天就来讲一下Intent 和 putExtra()。

一、什么是Intent

1、Intent的概念:  

  • Android中提供了Intent机制来协助应用间的交互与通讯,或者采用更准确的说法是,Intent不仅可用于应用程序之间,也可用于应用程序内部的activity, service和  broadcast receiver之间的交互。Intent这个英语单词的本意是“目的、意向、意图”。
  • Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来响应。

  activity、service和broadcast receiver之间是通过Intent进行通信的,而另外一个组件Content Provider本身就是一种通信机制,不需要通过Intent。我们来看下面这个图就知道了:

  

如果Activity1需要和Activity2进行联系,二者不需要直接联系,而是通过Intent作为桥梁。通俗来讲,Intnet类似于中介、媒婆的角色。

2、对于向这三种组件发送intent有不同的机制:  

  • 使用Context.startActivity() 或 Activity.startActivityForResult(),传入一个intent来启动一个activity。使用 Activity.setResult(),传入一个intent来从activity中返回结果。
  • 将intent对象传给Context.startService()来启动一个service或者传消息给一个运行的service。将intent对象传给 Context.bindService()来绑定一个service。
  • 将intent对象传给 Context.sendBroadcast(),Context.sendOrderedBroadcast(),或者Context.sendStickyBroadcast()等广播方法,则它们被传给 broadcast receiver。

二、Intent的相关属性:

  • component(组件):目的组件
  • action(动作):用来表现意图的行动
  • category(类别):用来表现动作的类别
  • data(数据):表示与动作要操纵的数据
  • type(数据类型):对于data范例的描写
  • extras(扩展信息):扩展信息
  • Flags(标志位):期望这个意图的运行模式

三、putExtra()

  putExtra("A",B)中,AB为键值对,第一个参数为键名,第二个参数为键对应的值。顺便提一下,如果想取出Intent对象中的这些值,需要在你的另一个Activity中用getXXXXXExtra方法,注意需要使用对应类型的方法,参数为键名.

四、看实例:  

package com.example.inter_activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView; public class MainActivity extends Activity { private ListView lv_items;
private BaseAdapter baseAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_items = (ListView) findViewById(R.id.lv_items);
baseAdapter = new BaseAdapter() { @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view ;
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
view =inflater.inflate(R.layout.items, null);
}else{
view = convertView;
}
return view; } @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
};
lv_items.setAdapter(baseAdapter);
lv_items.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(MainActivity.this,ItemsActivity.class);
intent.putExtra("name", "张三"+position);
intent.putExtra("id", "16"+position);
startActivity(intent);
}
});
} }

MainActivity

package com.example.inter_activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView; public class ItemsActivity extends Activity{
private TextView tv_name;
private TextView tv_age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
tv_name = (TextView) findViewById(R.id.name);
tv_age = (TextView) findViewById(R.id.age);
Intent intent = getIntent(); String name =intent.getStringExtra("name");
String id = intent.getStringExtra("id");
Log.i("items", name+"......."+id);
tv_name.setText(name);
tv_age.setText(id);
}
}

ItemsActivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.inter_activity.MainActivity" > <ListView
android:id="@+id/lv_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView> </RelativeLayout>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/iv_tuo"
android:layout_width="48dp"
android:layout_height="50dp"
/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="20dp"
android:text="title"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="15sp"
android:text="content" />
</LinearLayout> </LinearLayout>

items

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
/>
<TextView
android:id="@+id/age"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

view

  谢谢大家的关注,别忘了注册ItemsActivity哦0.0

  You will have it if it belongs to you,whereas you don't kvetch for it if it doesn't appear in your life.    ----命里有时钟需有 命里无时莫强求。

Android --差缺补漏之 Intent&putExtra()的更多相关文章

  1. Android查缺补漏(View篇)--事件分发机制源码分析

    在上一篇博文中分析了事件分发的流程及规则,本篇会从源码的角度更进一步理解事件分发机制的原理,如果对事件分发规则还不太清楚的童鞋,建议先看一下上一篇博文 <Android查缺补漏(View篇)-- ...

  2. Android查缺补漏(IPC篇)-- 进程间通讯基础知识热身

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8479282.html 在Android中进程间通信是比较难的一部分,同时又非常 ...

  3. Android查缺补漏(IPC篇)-- Bundle、文件共享、ContentProvider、Messenger四种进程间通讯介绍

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8387752.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  4. Android查缺补漏(IPC篇)-- 款进程通讯之AIDL详解

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  5. Android查缺补漏(IPC篇)-- 进程间通讯之Socket简介及示例

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8425736.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  6. Android查缺补漏(线程篇)-- IntentService的源码浅析

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8975114.html 在Android中有两个比较容易弄混的概念,Servic ...

  7. Android查缺补漏(IPC篇)-- 进程间通讯之AIDL详解

    本文作者:CodingBlock 文章链接:http://www.cnblogs.com/codingblock/p/8436529.html 进程间通讯篇系列文章目录: Android查缺补漏(IP ...

  8. Android查缺补漏(View篇)--自定义 View 的基本流程

    View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...

  9. Android查缺补漏--BroadcastReceiver的类型与使用

    Broadcast 是一种被用于应用内和应用之间传递信息的机制.一个广播可以对应多个接受者.一个完整的广播机制,需要具有以下三个要素: 发送广播的Broadcast 接受广播的BroadcastRec ...

随机推荐

  1. OneProxy自动剔除延迟节点

    在多种情况下,MySQL主从节点之间可能存在延迟.比如,主库的写入能力强于从库的写入能力.从库单线程复制.从库复制出错导致相关进程停止.为了保证数据的时效性,OneProxy提供了复制时效性检查,用于 ...

  2. linux centos 安装mysql

    安装步骤 http://www.cnblogs.com/gaojupeng/p/5727069.html 下面这个报错 主要还是在 题啊加软连接的   命令出了问题 1.启动  报错 mysqld_s ...

  3. python3 实现简单的信用卡还款,取款转账功能V2

    仅实现还款,取款,转账,信息查询功能 程序结构: atm(函数主执行程序): #Author by Andy #_*_ coding:utf-8 _*_ import os,sys Father_pa ...

  4. 个人学习随笔(psi-blast随笔)

    psi-blast学习 最近自己学习了一些新工具,最近在学习关于蛋白质相互作用位点的预测,在学习中,接触了几个新的工具,下面说说自己正在学习的psi-blast. 首先要说我用psi-blast用来做 ...

  5. 虚拟机VM安装linux系统

    废话不多说,直接上图文过程: 1.首先是下载linux镜像文件了(CentOS,Ubuntu等,根据自己的实际需求下载) linux镜像下载(提供几个32位的linux镜像下载,如有其他需求请自行百度 ...

  6. android studio gradle结构项目引入本地代码

    1.首先需要用eclipse打开目标项目,file export,选择gradle file. 2.拷贝文件到as项目的根目录[可选] 3.找到as项目的根目录下 .idea目录,下面有个module ...

  7. 编写自定义Yeoman生成器

    转载自JSCON-简时空:<自定义Yeoman生成器> 1.Getting Started 1.1.设置Node模块 Yeoman提供了generator-generator方便快速编写自 ...

  8. 关于ADO.NET 超时的问题

    前几天超时问题困扰我很头疼. 为什么我设置了链接字符串的超时时间很长,可是等了一小会就报错Timeout了? connectionString="Data Source=.;Initial ...

  9. JavaScript 面向对象(三) —— 高级篇

    JavaScript 面向对象(一) —— 基础篇 JavaScript 面向对象(二) —— 案例篇 一.json方式的面向对象 首先要知道,js中出现的东西都能够放到json中.关于json数据格 ...

  10. Android随

    Android的UI也是线程不安全的,如果想要更新应用程序里的UI元素,则必须在主线程中进行,否则就会出现异常.   Android中的异步消息处理机制主要由四个部分组成,Message,Handle ...