Android intent 笔记
学习android的intent,将其中的一些总结,整理的笔记记录于此。
intent是一个消息传递对象,可以在不同组件间传递数据。Activity,Service,Broadcast Receiver中都会用到。
参考链接
https://developer.android.com/guide/components/intents-filters.html?hl=zh-cn#Receiving
https://www.tutorialspoint.com/android/android_intents_filters.htm
http://www.cnblogs.com/skynet/archive/2010/07/20/1781644.html
http://www.cnblogs.com/feisky/archive/2010/01/16/1649081.html
intent主要信息
在介绍intent之前,需要了解intent中包含的信息。主要如下:
- componentName
要启动的目标组件名称。例如com.example.ExampleActivity。可以使用setComponent(),
- action
表示要执行的动作
- data
执行动作要操作的数据,android采用指向数据的URI和MIME类型来表示。
例如:
ACTION_MAIN 运行一个任务的第一个启动的activity
ACTION_VIEW content://contracts/people/1 ,显示ID是1的people信息。contentprovider使用
ACTION_DAIL tel:123 , 拨打电话号码123
- category
表示要处理intent的组件类型信息。这里指的是接收intent的对象的类型信息。
常见如下:
CATEGORY_LAUNCHER 表示activity是任务出事的activity。第一个启动的activity。
CATEGORY_BROWSABLE 目标Activity允许本省通过网络浏览器启动。
- extra
额外的信息,以键值对的形式保存。可以保存数据到intent中,在接收的地方读取出来。
- flag
可以指示android如何启动activity,以及启动后如何处理。
例如:
FLAG_ACTIVITY_CLEAR_TASK,在activity启动前清楚与它相关的activity,之后再启动。
Intent的类型
Intent有两种类型,显示意图explicit intent和隐式意图implicit intent。下面会分别介绍。
- explicit intents
显式的intents,在使用时,明确指定激活的组件名称。例如:
Intent intent = new Intent(this,Activity2.class);
startActivity(intent);
启动名称是Activity2的组件。
- implicit intents
隐式intents。没有明确指定组件名称。根据intent中的action,category,数据(URI/MIME)等,找到最合适的组件。
intent-filter
活动,服务,广播接收者为了告知系统能够处理那些隐式的intent,就将要接受的隐式intent信息写在intent-filter标签中。
app发出的intent之后,android系统就会根据intent-filter中的action,category等信息。只有匹配成功之后,接受到的intent才会被处理。
example
android中创建工程,名称是"IntentTest"。
- 布局文件
设置三个按键,分别用于explicit intent,implicit intent和intent-filter测试。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.intenttest.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/explicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="explicit test"/>
<Button
android:id="@+id/implicit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="implicit test"/>
<Button
android:id="@+id/intentFilter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="intent filter test" />
</LinearLayout>
</RelativeLayout>
- 新建activity
在Project窗口中,右键点击 app 文件夹并选择 New > Activity > Empty Activity。
activity名称Activity2。
android studio会新建一个Activity.java的文件,并在AndroidManifest.xml中添加activity.
Activity2.java如下所示:
package com.example.intenttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
- AndroidManifest.xml
清单文件如下所示
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2">
</activity>
</application>
</manifest>
现在的清单文件中已经有intent-filter标签。
ACTION_MAIN 操作指示这是主要入口点,且不要求输入任何 Intent 数据。
CATEGORY_LAUNCHER 类别指示此 Activity 的图标应放入系统的应用启动器。
所以一开始运行app时,第一个启动的是MainActivity,而不是新建的Activity2。
- MainActivity
MainActivity.java如下所示:
package com.example.intenttest;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button btnExplicit;
public Button btnImplicit;
public Button btnIntentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnExplicit = (Button) findViewById(R.id.explicit);
btnImplicit = (Button) findViewById(R.id.implicit);
btnIntentFilter = (Button) findViewById(R.id.intentFilter);
// 打开activity2
btnExplicit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 根据指定类型开启组件
Intent intent = new Intent(MainActivity.this, Activity2.class);
// 除了通过指定类型开启组件,还可以根据组件包名、全路径来指定开启组件。
/*
Intent intent = new Intent();
intent.setClassName("com.example.intenttest","com.example.intenttest.Activity2");
*/
startActivity(intent);
}
});
// 打开浏览器,访问网址
btnImplicit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
// 打开相机
btnIntentFilter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("android.media.action.IMAGE_CAPTURE");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
});
}
}
将app安装到手机上。
点击第一个按键“EXPLICIT TEST”将会切换到Activity2的界面,是一个空白的界面。
点击第一个按键“IMPLICIT TEST”将会打开手机的浏览器,并访问"www.google.com",但由于goole被墙,访问失败。
点击第三个按键“INTENT FILTER TEST”将会直接带卡手机的相机。
ui如下:
- intent-filter测试
下面做一些修改,用于测试intent-filter。
在AndroidMainfest.xml中为Activity2添加标签。
android.media.action.IMAGE_CAPTURE和打开相机的action是相同的。
如下为新的AndroidManifest.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intenttest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2">
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
从新运行更改之后的app。当点击第三个按键“INTENT FILTER TEST”时,会弹出选框。选择相机还是IntentTest。
点击IntentTest,会直接跳转到Activity2的界面。说明设置的intent-filter生效了。如下所示:
Tony Liu
2017-3-8, Shenzhen
Android intent 笔记的更多相关文章
- Android学习笔记(三)——初探Intent
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Intent 是 Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作 ...
- Pro Android学习笔记(十一):了解Intent(中)
Intent的构成 Intent能够带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个inte ...
- Android学习笔记-Intent(一)
Intent对象在Android官方API这样描述:It is a passive data structure holding an abstract description of an opera ...
- 【转】Pro Android学习笔记(十二):了解Intent(下)
解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...
- 【转】Pro Android学习笔记(十一):了解Intent(中)
Intent的构成 Intent可以带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个inte ...
- 【转】Pro Android学习笔记(十):了解Intent(上)
目录(?)[-] Intent基本含义 系统的Intent Android引入了Intent的概念来唤起components,component包括:1.Activity(UI元件) 2.Servic ...
- Android复习笔记--Intent
Intent是Android中各组件跳转的重要方式,一般可悲用于启动活动.启动服务.以及发送广播等场景. #显示Intent 主要主要用于启动已知的组件 //发送方 Intent intent = ...
- android学习笔记29——Intent/IntentFilter
Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...
- Android(java)学习笔记121:android.intent.action.MAIN 与 android.intent.category.LAUNCHER 理解
先看看网路上的说法: android.intent.action.MAIN决定应用程序最先启动的 Activity android.intent.category.LAUNCHER决定应用程序是否显示 ...
随机推荐
- php session 生命周期代码实例
php session 生命周期代码实例 我们为什么需要Session,就是因为我们需要存储各个用户的状态数据.那么试问,如果由你来设计解决这个需求的方案,那么也许你会设置这样一个数据 ...
- Delphi中解析Xml的控件-SimDesign NativeXml
Delphi中解析Xml的控件-SimDesign NativeXml 正在学习,感觉应用很方便.无源代码的版本还是免费的. SimDesign.NativeXml是一个delphi和bcb的XML控 ...
- 【angularjs】【学习心得】路由继续研究篇
原文:http://www.imooc.com/wenda/detail/236998 其实路由的功能是比较复杂的,我们实际应用中页面的状态也是非常多的,上面简单的路由是肯定不能满足我们的需求的,所以 ...
- 推荐系统中的Graph Model
转自:http://www.cnblogs.com/wentingtu/archive/2012/05/28/2521166.html 推荐中对graph model的研究主要有两个方面,一个是如何构 ...
- HDU-1862-EXCEL排序
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1862 这个题考的就是你对sort函数的掌握:会用sort函数对字符串,数字排序,只要懂得话题目很简单 ...
- Maven与Antx(整理)
http://blog.csdn.net/ghost_t/article/details/5709640 一.Maven与Antx概况: Antx简介 在讲为什么使用maven之前我想说一下,an ...
- Java8 Lumbda表达式 初步
Java8 Lumbda表达式 初步 package com.stono.test; import java.util.function.BinaryOperator; public class Te ...
- 搭建Minisipserve服务器实现局域网内IOS客户端idoubs的通信
idoubs是IOS设备开发的第一款全功能并开放源码的3GPP IMS客户端,它同时专为IOS平台开发设计的voIP测试版客户端,以doubango为框架,能实现当前最先进的多媒体功能,主要功能有:语 ...
- TCP/IP协议族(一) HTTP简介、请求方法与响应状态码
接下来想系统的回顾一下TCP/IP协议族的相关东西,当然这些东西大部分是在大学的时候学过的,但是那句话,基础的东西还是要不时的回顾回顾的.接下来的几篇博客都是关于TCP/IP协议族的,本篇博客就先简单 ...
- C++编程练习(1)----“实现简单的线性表的顺序存储结构“
线性表的顺序存储结构,指的是用一段地址连续的存储单元依次存储线性表的数据元素. 故可以用数组来实现顺序存储结构. 用C++编写的利用数组实现简单的读取.插入和删除功能的线性表. #include< ...