Fork me on GitHub

Android Studio教程10-Intent的详细使用

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

posted @ 2019-01-21 15:26  Bricker666  阅读(1269)  评论(0编辑  收藏  举报