Android之旅-Intent与Intent Filter[上]
Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指定组件,至于到底启动哪个组件,取决于Intent的各个属性。
一、显式的Intent
明确指定了要启动的组件的Intent我们称为显式的Intent
例如:
- package com.example.testintent;
- import android.app.Activity;
- import android.content.ComponentName;
- 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 {
- Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button)findViewById(R.id.button1);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- ComponentName comp = new ComponentName(MainActivity.this, SecondActivity.class);
- Intent intent = new Intent();
- intent.setComponent(comp);
- startActivity(intent);
- }
- });
- }
- }
注意在manifest文件里注册SecondActivity
Intent的Componet属性需要接收一个ComponentName对象,ComponetName对象其实是一个指定包和要启动的Activity路径的类,有如下几个构造
- componentName(String pkg, String cls)
- componentName(Context pkg, String cls)
- componentName(Context pkg, Class<?> cls)
componentName(Parcel in)
除了这个属性之外Intent还包含了如下三个方法:
- setClass(Context packageContext, Class<?> cls )
- setClassName(Context packageContext, String className)
- setClassName(String packageName, String className )
上面的代码可以简化为如下代码:
- Intent intent = new Intent(MainActivity.this, SecondActivity.class);
- startActivity(intent);
二、隐式的Intent
没有明确指定要启动的组件的Intent我们称为隐式的Intent
Intent除了上面的Componet属性外还有Action、Category属性
Action代表Intent所要完成的一个抽象动作,而Category则是动作附加的类别信息。
例如:
- package com.example.testintent;
- import android.app.Activity;
- import android.content.ComponentName;
- 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 {
- Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button)findViewById(R.id.button1);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- Intent intent = new Intent();
- intent.setAction("com.example.intent.action.TEST_ACTION");
- startActivity(intent);
- }
- });
- }
- }
manifest文件中配置
- <activity
- android:name=".SecondActivity">
- <intent-filter >
- <action android:name="com.example.intent.action.TEST_ACTION"/>
- <category android:name="android.intent.category.DEFAULT"/>
- </intent-filter>
- </activity>
一个Intent只能指定一个Action属性,可以包含多个Category属性,当程序创建时,默认启动category为DEFAULT的组件。
接下来我们来看看Category属性的用法
- public void onClick(View arg0) {
- Intent intent = new Intent();
- intent.setAction("com.example.intent.action.TEST_ACTION");
- intent.addCategory("android.intent.category.TEST_CATEGERY");
- startActivity(intent);
- }
manifest文件中配置
- <activity
- android:name=".SecondActivity">
- <intent-filter >
- <action android:name="com.example.intent.action.TEST_ACTION"/>
- <category android:name="android.intent.category.DEFAULT"/>
- <category android:name="android.intent.category.TEST_CATEGERY"/>
- </intent-filter>
- </activity>
可以看出其实是根据Action和Category两个属性共同决定启动哪个Activity的,Category可以有多个,只要满足其中的一个即可。
实际上Intent不仅可以启动我们定义的Activity,也可以启动系统和其他应用的Activity
ACTION_MAIN 应用程序入口ACTION_VIEW 显示指定数据ACTION_ATTACH_DATA 指定某块数据将被附加到其他地方ACTION_EDIT 编辑指定数据ACTION_PICK 从列表中选择某项,并返回所选数据ACTION_CHOOSER 显示一个Activity选择器ACTION_GET_CONTENT 让用户选择数据,并返回所选数据ACTION_DIAL 显示拨号面板ACTION_CALL 直接向指定用户打电话ACTION_SEND 向其他人发送数据ACTION_SENDTO 向其他人发送消息ACTION_ANSWER 应答电话ACTION_INSERT 插入数据ACTION_DELETE 删除数据ACTION_RUN 运行数据ACTION_SYNC 执行数据同步ACTION_PICK_ACTIVITY 用于选择ActivityACTION_SEARCH 执行搜索ACTION_WEB_SEARCH 执行web搜索ACTION_FACTORY_TEST 工厂测试的入口点
这里仅列出部分更多关于Action和Category属性请参阅:http://developer.android.com/reference/android/content/Intent.html
三、一个获取通讯录的实例:
- package com.example.testintent;
- import android.app.Activity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.provider.ContactsContract.Contacts;
- import android.support.v4.content.CursorLoader;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private static final String TAG = "MainActivity";
- Button button;
- final int PICK_CONTACT = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button)findViewById(R.id.button1);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_GET_CONTENT);
- intent.setType("vnd.android.cursor.item/phone");
- startActivityForResult(intent, PICK_CONTACT);
- }
- });
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case PICK_CONTACT:
- if(resultCode == Activity.RESULT_OK){
- //获取返回数据
- Uri contactData = data.getData();
- CursorLoader cursorLoader = new CursorLoader(this, contactData, null, null, null, null);
- Cursor cursor = cursorLoader.loadInBackground();
- if(cursor.moveToFirst()){
- String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
- String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
- Log.i(TAG, "姓名:" + name);
- //根据联系人查看详细信息
- Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
- if(phones.moveToFirst()){
- System.out.println("进来了");
- String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- Log.i(TAG, "电话:" + phoneNumber);
- }
- //关闭游标
- phones.close();
- }
- //关闭游标
- cursor.close();
- }
- break;
- default:
- break;
- }
- }
- }
Android之旅-Intent与Intent Filter[上]的更多相关文章
- Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
- Android菜鸟的成长笔记(9)——Intent与Intent Filter(下)
原文:[置顶] Android菜鸟的成长笔记(9)——Intent与Intent Filter(下) 接着上一篇的内容,下面我们再来看看Intent的Data与Type属性. 一.Data属性与Typ ...
- Android基础Activity篇——Intent返回数据给上一个活动
1.如果活动B要将数据返回给活动A,那么需要以下三步: 1.1在活动A中使用startActivityForResult()方法启动活动B. 1.2在活动B中使用setResult()方法传回Iten ...
- 我的Android 4 学习系列之Intent 和 Broadcast Reciever
目录 Intent 简介 使用隐式和显式Intent启动Activity.子Acitivity和Service 使用Linkify 使用Broadcast Intent 广播事件 使用 Pending ...
- Android开发学习笔记:Intent的简介以及属性的详解【转】
一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...
- Android入门(二)Activity-Toast、Intent
原文链接:http://www.orlion.ga/427/ 一.隐藏activity的标题 在activity的java代码的onCreate()方法中入requestWindowFeature(W ...
- Android开发-API指南-常用Intent
Common Intents 英文原文:http://developer.android.com/guide/components/intents-common.html 采集(更新)日期:2014- ...
- Android开发-API指南-Intent和Intent过滤器
Intents and Intent Filters 英文原文:http://developer.android.com/guide/components/intents-filters.html 采 ...
随机推荐
- 自定义控件之万能Repeater源码
using System.ComponentModel; using System.Web.UI; [assembly: TagPrefix("Jinlong.Control", ...
- 微信小程序 - 步骤条组件
<!-- 未激活颜色: uncolor:'#ccc' 激活 active:0 数据源 data:[{},{}] 步骤条类型:type basic detail num more --> & ...
- 008-Go 关于字符串拼接
如果是少量小文本拼接,用 “+” 如果是大量小文本拼接,用 strings.Join 如果是大量大文本拼接,用 bytes.Buffer package main import( "fmt& ...
- nginx获得response自定义的header
Response header send by upstream is $upstream_http_x_uuid http://wiki.nginx.org/HttpUpstreamModule#. ...
- std::thread中获取当前线程的系统id
std::thread不提供获取当前线程的系统id的方法,仅可以获取当前的线程id,但是我们可以通过建立索引表的方式来实现 std::mutex m; std::map<std::thread: ...
- MySQL参数化查询的IN 和 LIKE
https://stackoverflow.com/questions/650455/c-sharp-parameterized-query-mysql-with-in-clausehttps://s ...
- redis实现简单的分布式锁
在分布式系统中多个请求并发对少数资源进行争抢,例如10个人同时秒杀一件商品,如果不用分布式的锁进行处理(当然还有其它的处理方案),则很容易出现多个人抢到一个商品(超卖)的情况,用redis可以比较容易 ...
- 转:CMake快速入门教程-实战
CMake快速入门教程:实战 收藏人:londonKu 2012-05-07 | 阅:10128 转:34 | 来源 | 分享 0. 前言一个多月 ...
- 基于微信小程序的票价和时间选择以及计算总价
发布时间:2018-10-30 技术:wxml+wxss+JS 概述 微信小程序实现选择时间和票价,根据选择的票价和时间实时计算总价,当时间和票价都显示缺货状态时,点击弹出缺货登记,需要选择票 ...
- Android 百度鹰眼轨迹SDK(v2.1.6)
闲聊 看过<鹰眼追击>这部电影的读者一定对"鹰眼"这台巨无霸计算机印象深刻,如今我们能够实现自己的鹰眼. 效果图 本篇为百度地图SDK第三篇博文 第一篇实现:Andro ...