`

Android Intents

阅读更多
Android Intent 是 Android 进程间交互,进程内组件间交互的中介.

Intent 分为两种:
显式Intent:与具体组件关联.
隐式Intent:声明关联Action.

隐式Intent:
1.
Activity 声明接收 Intent 类型
<intent-filter>
<action android:name="公司名.intent.action.ActionName" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>

2.
调用 Activity
Intent intent = new Intent("公司名.intent.action.ActionName");
intent.setData(null);
parentActivity.startActivity(intent);

3.
Android 预定义的 Intent
Intent.ACTION_VIEW
Intent.ACIION_EDIT
Intent.ACTION_PICK
Intent.ACTION_WEB_SERACH
Intent.ACIION_DIAL
Intent.ACTION_CALL

4.
action 名字相同的 Intent,Andriod OS 如何决定调用哪个 Activity?
a.根据 schema.URI 的协议匹配.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:schema="http" />
<data android:schema="https" />
</intent-filter>

5.
数据子元素:
<host>
<mimeType>
<path>
<pathPattern>
<pathPrefix>
<port>
<schema>

<mimeType>经常用到:
匹配多个记录
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="vnd.android.cursor.dir/vnd.tl.android.note" />
</intent-filter>

匹配一条记录
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="vnd.android.cursor.item/vnd.tl.android.note"/>
</intent-filter>


6.extra

除了data属性外,extra也可以附加数据.
数据以键值对方式存储.键是字符串类型 ,而值是基本类型或实现Parcelable接口的对象.
extra 代表 android.os.Bundle.
Bundle bundle = intent.getExtras();
Bundel anotherBundle = new Bundle();
intent.putExtras(anotherBundle); //复制extra 数据到anotherBundle.


7.category
用于设定Activity放置的位置.
嵌入lanucher
嵌入home
嵌入Activity
嵌入菜单
嵌入Preferences
default

8.获取运行程序列表
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List myApps = getPackggetManager().queryIntentActivities(intent, 0);




显式Intent:

Intent intent = new Intent(parentActivity, activity.class);
parentActivity.startActivity(intent);



Intent 如何解析:

如果Intent指定关联的组件名,则忽略其它属性.只对关联的组件有效.
否则
根据action属性查找,若intent-filter中指定action且没有其它属性,则调用组件.
若有多个组件匹配,android os 会出现一个组件选择器,供用户选择.
然后
根据data属性匹配.先根据schema匹配,然后根据URI和URI Data 类型.
最后
根据category.
查找 intent-filter 中声明的 category.匹配则调用.只用于ACITON_PICK.


调用 Activity 并返回结果:
调用:
startActivityForResult(Intent intent, int requestCode);

结果回调:
onActivityResult(int requestCode, int resultCode, Intent data);

示例:
public static void invokePick(Activity activity){
    Intent intent = new Intent(Intent.ACTION_PICK);
    int requestCode = 1;
    intent.setData(Uri.parse("contetn://tl.android.provider.NotePad/notes"));
    activity.startActivityForResult(intent, requestCode);
}


protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    parseResult(this, requestCode, resultCode, data);
}

public static void parseResult(Activity activity, int requestCode, int resultCode, Intent data){
    if(requestCode != 1){
        Log.d(TAG, "Some one else called this, no us!");
        return;
    }
    if(resultCode != Activity.RESULT_OK){
        Log.d(TAG, "Result code is not ok! " + resultCode);
        return;
    }
    Log.d(TAG, "Result code is " + resultCode);

    Uri uri = data.getData();
    Log.d(TAG, "URI is " + uri.toString());

    data.setAction(Intent.View);
    startActivity(data);
}

















分享到:
评论

相关推荐

    Learning Android Intents

    ### 学习Android Intents:理解与应用Intents的力量 #### 概述 在深入学习Android Intents之前,我们先来了解下什么是Intents以及它在Android开发中的重要性。Intents是Android平台中用于应用程序间通信的核心机制...

    Android Intents and Intent Filters(一)

    Android Intents and Intent Filters(一) 对应博客地址:http://blog.csdn.net/michael__li/article/details/6947545

    Android Intents and Intent Filters(二)源代码

    Android Intents and Intent Filters(二)源代码 对应博客 http://blog.csdn.net/michael__li/article/details/6950127

    Android-Intents-3

    ### Android Intents 深入理解:第三部分——使用Tab #### 一、引言 在Android开发过程中,Intents是实现组件间通信的核心机制之一。通过Intents,应用程序的不同部分(如活动、服务等)可以相互通信,传递数据或...

    Android-Intents-1

    ### Android Intents 知识点详解 #### 一、引言与背景介绍 在深入探讨 Android Intents 及其相关概念之前,我们首先简要回顾一下 Android 的基本哲学及其对应用设计的影响。根据 Marty Hall 所提供的培训资料,...

    Android-Intents-2.

    ### Android Intents 进阶应用:使用 URI 调用 Activity #### 一、引言 在 Android 开发中,`Intents` 是一个非常重要的概念。它允许开发者定义应用程序内部以及不同应用程序之间的通信方式。在前一部分中,我们...

    McGraw.Hill.Android.A.Programmers.Guide

    * Trigger actions using Android Intents, Filters, and Receivers * Implement GPS, Google Maps, Google Earth, and GTalk * Build interactive SQLite databases, calendars, and notepads * Test ...

    Android应用源码使用ZXing识别条码二维码(简单的实现).zip

    Android Intents是针对Android平台的一个轻量级封装,它允许通过Intent调用系统中已经安装的ZXing应用(如 Barcode Scanner)或者直接在你的应用内集成ZXing的功能。 集成ZXing到Android应用的第一步是将ZXing的...

    android api

    8. **Android Intents**:作为Android系统中的消息传递机制,Intent用于启动活动、传递数据、触发广播等操作。 9. **Android Services**:后台运行的服务,可以在没有用户界面的情况下执行任务,如音乐播放、后台...

    Android代码-不要再一遍 一遍的写那些 Intents 了。

    Android Intents A small library which will save you from writing the same intent creation code again and again for the most simple tasks. I found myself writing my own library to create some common ...

    android email 源代码

    1. **Android Intents与Intent Filters** 在Android中,应用程序之间通过Intent进行通信。邮件应用通常会注册对`ACTION_SEND`或`ACTION_SENDTO` Intent的监听,以便其他应用能够通过Intent调用来发送邮件。开发者...

    intent:一个简单的Flutter插件,用于处理Android Intent,使用编写

    意图一个简单的flutter插件来处理Android Intents-您编写的Android Intents一站式解决方案,使用 :red_heart: 。 显示一些 :red_heart: 通过把 :star: intent尝试帮助您使用Android Intents启动另一个android活动。 ...

    zxing-library.rar

    开发者可以通过ZXing的Android Intents接口,轻松地启动系统相机来扫描条码,而无需编写复杂的图像处理代码。 除了Android,ZXing也支持Java SE环境,这意味着它可以在桌面应用或服务器环境中使用。通过ZXing的Core...

    使用ZXing识别条码二维码(简单的实现).zip

    在安卓平台上,ZXing提供了AndroidIntents接口,使得集成到安卓应用中变得简单便捷。 首先,我们需要在项目的build.gradle文件中添加ZXing的依赖。通常,我们通过添加如下代码来引入ZXing的安卓库: ```groovy ...

    Android Espresso Test Intents and Webview

    在Android应用开发中,测试是确保产品质量的关键环节。Espresso是一个强大的UI测试框架,它使得开发者可以编写直接、简洁的测试代码,以验证用户界面的行为。本篇文章将深入探讨Espresso如何与Intent和Webview协同...

    Android开发之旅 Intents和Intent Filters(实例部分)(免费)

    ### Android开发之旅:深入理解Intents与Intent Filters 在Android开发中,`Intents`与`Intent Filters`是实现组件间通信的关键技术。通过这两项技术,开发者可以让应用程序中的不同组件,甚至不同的应用程序之间...

    Android-Intents-al

    转到app/src/main/java/com.flatironschool.intents/activities ,您会发现三个活动FirstActivity SecondActivity和ThirdActivity 。 打开FirstActivity ,您会发现需要实现的空方法onContinueButtonClick 。 在...

Global site tag (gtag.js) - Google Analytics