1. Intent启动器

1.1. Intent的用途

1. 启动Activity

  • startActivity()
  • startActivityForResult():希望返回结果

2. 启动服务

启动一个不适用用户界面而在后台执行操作的组件

  • startService():下载文件等,可携带任何必要的数据
  • bindService(): 使用客户端-服务器接口,从其他组件绑定到此服务

3. 传递广播

广播是任何应用均可接收的消息。系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播

  • sendBroadcast()
  • sendOrderedBroadcast()
  • sendStickyBroadcast()

1.2. Intent类型

1.显示Intent

按名称(完全限定类名)指定要启动的组件

  • 系统将立即启动 Intent 对象中指定的应用组件
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);

2.隐式Intent

不会指定特定的组件,而是声明要执行的常规操作,从而允许其他应用中的组件来处理它。

  • Android 系统通过将 Intent 的内容与在设备上其他应用的清单文件中声明的 Intent 过滤器进行比较,从而找到要启动的相应组件。 如果 Intent 与 Intent 过滤器匹配,则系统将启动该组件,并向其传递 Intent 对象。 如果多个 Intent 过滤器兼容,则系统会显示一个对话框,支持用户选取要使用的应用。
  • Intent 过滤器是应用清单文件中的一个表达式,它指定该组件要接收的 Intent 类型。
// Create the text message with a string
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain"); // Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}

2. 构建Intent

Intent包含的主要信息如下:

  • 组件名称-用于显示Intent
  • 操作:指定用于执行的通用操作-用于隐式Intent
    • ACTION_VIEW
    • ACTION_SEND
  • 数据
  • 类别:
  • Extra: putExtra()
  • 标志

3. 隐式Intent

<activity android:name="MainActivity">
<!-- This activity is the main entry, should appear in app launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <activity android:name="ShareActivity">
<!-- This activity handles "SEND" actions with text data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<action android:name="android.intent.action.SEND_MULTIPLE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="application/vnd.google.panorama360+jpg"/>
<data android:mimeType="image/*"/>
<data android:mimeType="video/*"/>
</intent-filter>
</activity>
  • 第一个 Activity MainActivity 是应用的主要入口点。当用户最初使用启动器图标启动应用时,该 Activity 将打开:

    • ACTION_MAIN 操作指示这是主要入口点,且不要求输入任何 Intent 数据。

      CATEGORY_LAUNCHER 类别指示此 Activity 的图标应放入系统的应用启动器。 如果 元素未使用 icon 指定图标,则系统将使用 元素中的图标。

      这两个元素必须配对使用,Activity 才会显示在应用启动器中。
  • 第二个 Activity ShareActivity 旨在便于共享文本和媒体内容。 尽管用户可以通过从 MainActivity 导航进入此 Activity,但也可以从发出隐式 Intent(与两个 Intent 过滤器之一匹配)的另一应用中直接进入 ShareActivity

Android Studio教程10-Intent的详细使用的更多相关文章

  1. Android studio教程

    Android studio教程: http://jingyan.baidu.com/season/44062

  2. Android Studio 教程

    Android Studio 超详细安装教程 http://dkylin.com/archives/2019/android-studio-installation.html Android Stud ...

  3. Android Studio教程从入门到精通

    最新2.0系列文章参考: Android Studio2.0 教程从入门到精通Windows版 - 安装篇Android Studio2.0 教程从入门到精通Windows版 - 入门篇Android ...

  4. Android studio教程:[6]创建多个Activity

    通常来说,一个android应用程序不止一个Activity(活动),更不止一个界面.于是需要创建多个Activity来满足应用程序的要求,这里我将告诉大家如何添加新的Activity,并实现Acti ...

  5. Android Studio教程01-的工程和目录结构解析

    目录 1.主目录 1.1. app目录 1.2.项目资源文件夹res 2. 理解build.gradle文件 2.1. 外部build.gradle 2.2. app文件下的build.gradle ...

  6. Ubuntu1404配置jdk-12.0.2并安装Android Studio教程

    最近在学习Android Studio 移动应用程序开发,但Android Studio好像对win10不太友好,所以小帅想在Ubuntu上安装Android Studio.为此小帅还去网上找了相关教 ...

  7. Android Studio教程--给Android Studio安装Genymotion插件

    打开Android Studio,依次[File]-[Settings] 在打开的settings界面里找到plugins设置项,点击右侧的“Browser..”按钮 在搜索栏里输入genymotio ...

  8. Android Studio教程--Android Studio 2.1安装与配置

    1.下载Android Studio 去官网https://developer.android.com/studio/index.html下载最新版的Android Studio2.1(自备梯子) 或 ...

  9. Mac下载安装Android Studio教程

    今天把公司闲置的一台Mac-mini重装了下系统感觉用着速度还不错,平时上班用的机器USB有些问题,所以打算用这台Mac.以往开发用Intellij Idea就够用,但是这次项目引用的jar包太多,遭 ...

随机推荐

  1. 从EventLoop到宏任务与微任务

    1.javascript是单线程的 javascript是单线程的,意思是javascript在同一时间内只能做一件事情. 为什么是单线程的? 因为js的主要用途是用于用户交互和操作DOM,如果是多线 ...

  2. Mac OS X中开启或关闭显示隐藏文件命令

    前言:之前一直用的都是 windows 系统的电脑,刚接触 Mac ,很多功能都不熟悉,写下博客记录一下,以防以后忘记,也给后来者提供方便. 命令行方式:显示隐藏文件: defaults write ...

  3. Ntaub表单开发入门系列 (一)

    此系列文章通过虚构场景介绍Ntaub表格开发流程.示例假设某公司人力部门要制定招聘计划,要求各部门按月提交招聘需求,招聘需求需经人力总监和公司总经理审批. 软件可以从http://www.ntaub. ...

  4. TOTP 介绍及基于C#的简单实现

    TOTP 介绍及基于C#的简单实现 Intro TOTP 是基于时间的一次性密码生成算法,它由 RFC 6238 定义.和基于事件的一次性密码生成算法不同 HOTP,TOTP 是基于时间的,它和 HO ...

  5. mysql 服务【安装】【启动】【停止】【卸载】【重置密码】

    windows安装:一.一机安装多个服务时,重复步骤,修改服务名即可: 1.mysqld install mysql_12336 --defaults-file="MYSQL_HOME\my ...

  6. pytest之收集用例规则与运行指定用例

    前言 上篇文章相信大家已经了解了pytest在cmd下结合各种命令行参数如何运行测试用例,并输出我们想要看到的信息.那么今天会讲解一下pytest是如何收集我们写好的用例?我们又有哪些方式来运行单个用 ...

  7. 计算器模拟器中的情怀——Free42简介

    说到情怀,我首先想聊几句电子计算器的历史.电子计算器这种东西,在最近这几十年的人类发展中,曾经起到过相当重要的作用,尤其是在七十年代到九十年代初这个时期,大型的全功能电脑贵得要命,有钱有时也买不到,而 ...

  8. python 基础语法梳理

    最近涉及到python的东西比较多,抽一点时间把基础语法规整下. 1.面向对象 #coding=utf-8 def _class_test_01(): s = squire(3,4) print(&q ...

  9. Netty3:分隔符和定长解码器

    回顾TCP粘包/拆包问题解决方案 上文详细说了TCP粘包/拆包问题产生的原因及解决方式,并以LineBasedFrameDecoder为例演示了粘包/拆包问题的实际解决方案,本文再介绍两种粘包/拆包问 ...

  10. 客户端和服务端(C#) 时间戳的生成和转换

    C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳: ...