Intent能够说是Android的灵魂,程序跳转和传递数据的时候基本上就是靠Intent了。Intent在Android应用中是相当重要的,理解Intent相应用编程非常有帮助。在Android的官方API文档里边对Intent是这样定义的:An Intent is an abstract description of an operation to be performed。一个Intent就是一次对将要运行的操作的抽象描写叙述(真实够抽象的)。

详细有一下3种形式:

1、通过startActivity方法来启动一个新的Activity。

2、通过Broadcast机制能够将一个Intent发送给不论什么对这个Intent感兴趣的BroadcastReceiver。

3、通过startService(Intent)或bindService(Intent, ServiceConnection, int)来和后台的Service交互。

以下来看一下怎样通过startActivity方法启动一个新的Activity。

Intent最经常使用的用途就是连接一个应用其中的各个Activity,假设我们把Activity比作积木的话。那么Intent就好像是胶水。把不同的积木粘起来,构成我们搭建的房子。在程序其中假设要启动一个Activity的话,通常我们会调用startActivity方法,并把Intent作为參数传进去。例如以下所看到的:

startActivity(intent);

这个Intent或者指定了一个Activity,或者里边仅仅包括了选定Activity的信息。可是详细启动哪个Activity是由系统去决定的,Android系统负责挑选一个最满足匹配挑选条件的Activity。



实例:IntentDemo

执行效果:





代码清单:

AndroidManifest.xml

<?

xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rainsong.intentdemo"
android:versionCode="1"
android:versionName="1.0"> <uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="OtherActivity"
android:label="OtherActivity">
</activity>
</application>
</manifest>

布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到还有一个Activity"
/>
</LinearLayout>

布局文件:other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OtherActivity"
/>
</LinearLayout>

Java源码文件:MainActivity.java

package com.rainsong.intentdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity
{
OnClickListener listener1 = null;
Button button1; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); listener1 = new OnClickListener() {
public void onClick(View v) {
Intent intent1= new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent1);
}
}; button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(listener1);
}
}

Java源码文件:OtherActivity.java

package com.rainsong.intentdemo;

import android.app.Activity;
import android.os.Bundle; public class OtherActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
}

Android之Intent和Activity的更多相关文章

  1. Android - 通过Intent启动Activity

    通过Intent启动Activity 本文地址: http://blog.csdn.net/caroline_wendy 为了动态关联Activity界面,使用Intent启动.能够灵活绑定. 在In ...

  2. Android笔记---Intent实现Activity跳转

    学了之前的Android控件以及布局,我们就能够做一些UI的设计了,这里我结合之前的知识.以一个小的登录项目来解说下Activity之间跳转. 先看下效果图: 1.登录界面: 2.点击登录按钮跳转到另 ...

  3. Android四大组件之Activity一(组件的概念、Intent、监听)

    前言知识补充:  什么是组件?   1.它的类必须实现特定接口或继承特定类   2.需要在配置文件中配置其全类名   3.它的对象不是通过new来创建的, 而是系统自动创建的   4.它的对象具有一定 ...

  4. Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]

    http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...

  5. Android开发之bug-No Activity found to handle Intent

    android.content.ActivityNotFoundException: No Activity found to handle Intent 做Android开发中,使用隐式intent ...

  6. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  7. 【Android基础】利用Intent在Activity之间传递数据

    前言: 上一篇文章给大家聊了Intent的用法,如何用Intent启动Activity和隐式Intent,这一篇文章给大家聊聊如何利用Intent在Activity之间进行沟通.   从一个Activ ...

  8. Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解

    前言 大家好,给大家带来Android开发工程师文集-Activity生命周期,启动方式,Intent相关介绍,Activity详细讲解的概述,希望你们喜欢 Activity是什么 作为一个Activ ...

  9. Android(java)学习笔记73:Intent启动Activity

    1. Intent启动Activity案例 (1)首先是main.xml和other.xml文件如下: main.xml文件: <?xml version="1.0" enc ...

随机推荐

  1. react-router 4.0版本学习笔记

    Router 所有路由组件的底层接口,一般情况都不使用,而是使用更加高级的路由. 最常用的有两种<BrowserRouter>.<HashRouter> <Browser ...

  2. iOS中block 静态全局局部变量block变量,以及对象,详解!

    //最近总是犯迷糊,关于block对外部变量的引用,今天有时间就写了一下,加深自己的理解,巩固基础知识 1 #import <Foundation/Foundation.h> ; int ...

  3. BZOJ 3384 上帝与集合的正确用法

    上帝与集合的正确用法 [问题描述] [输入格式] 第一行一个T,接下来T行,每行一个正整数p,代表你需要取模的值. [输出格式] T行,每行一个正整数,为答案对p取模后的值. [样例输入] 3236 ...

  4. 十步完全理解 SQL(转载)

    1 SQL是一种声明式语言 SQL 语言是为计算机声明了一个你想从原始数据中获得什么样的结果的一个范例,而不是告诉计算机如何能够得到结果.学好SQL要改变传统函数式编程思想,例如用变量传参.使用循环语 ...

  5. ubuntu下使用OBS开斗鱼直播

    系统环境:ubuntu 15.10,OBS Studio 0.13.1 OBS是可以在linux,windows,mac下直播的开源软件,官方地址:https://obsproject.com/ 斗鱼 ...

  6. 《HelloGitHub》第 38 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  7. 洛谷——P2383 狗哥玩木棒

    P2383 狗哥玩木棒 题目背景 狗哥又趁着语文课干些无聊的事了... 题目描述 现给出一些木棒长度,那么狗哥能否用给出的木棒(木棒全用完)组成一个正方形呢? 输入输出格式 输入格式: 输入文件中的第 ...

  8. 10.1综合强化刷题 Day2 afternoon

    最大值(max) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目: 这里有一个长度为n的 ...

  9. 2016集训测试赛(十九)Problem C: 无聊的字符串

    Solution 傻X题 我的方法是建立后缀后缀树, 然后在DFS序列上直接二分即可. 关键在于如何得到后缀树上每个字符对应的字节点: 我们要在后缀自动机上记录每个点在后缀树上对应的字母. 考虑如何实 ...

  10. windows 下使用github客户端报错:Failed to publish this branch

    在windows系统下使用github客户端同步的时候报错“Failed to publish this branch”,查找原因,发现结果是安装vscode的时候没有检查到git,然后安装git后库 ...