• Android学习笔记 73. 隐式intent


    Android学习笔记

    Android 开发者基础知识 (Java) —— Google Developers 培训团队

    第1单元 入门

    第2课 activity和intent

    73. 隐式intent

    你会做什么
    • 创建一个新应用来试验隐式Intent.
    • 实现一个Intent打开网页的隐式,另一个打开地图上的位置。
    • 实施一个操作以共享一段文本。
    • 创建一个可以接受隐式Intent打开网页的新应用程序。
    73.1 创建项目和布局
    1. 创建项目

      在这里插入图片描述

    2. 创建布局

      设置资源

      <string name="edittext_uri">http://www.baidu.comstring>
          <string name="button_uri">Open Websitestring>
      
          <string name="edittext_loc">Golden Gate Bridgestring>
          <string name="button_loc">Open Locationstring>
      
          <string name="edittext_share">\'Twas brillig and the slithy tovesstring>
          <string name="button_share">Share This Textstring>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="16dp"
          tools:context=".MainActivity">
      
          <EditText
              android:id="@+id/website_edittext"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/edittext_uri"/>
      
          <Button
              android:id="@+id/open_website_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="24dp"
              android:text="@string/button_uri"
              android:onClick="openWebsite"/>
      
          <EditText
              android:id="@+id/location_edittext"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/edittext_loc"/>
      
          <Button
              android:id="@+id/open_location_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="24dp"
              android:text="@string/button_loc"
              android:onClick="openLocation"/>
      
          <EditText
              android:id="@+id/share_edittext"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="@string/edittext_share"/>
      
          <Button
              android:id="@+id/share_text_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="24dp"
              android:text="@string/button_share"
              android:onClick="shareText"/>
      
      LinearLayout>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
    73.2 实现打开网站按钮

    此操作使用隐式Intent将给定的 URI 发送到Activity可以处理该隐式Intent的(例如 Web 浏览器)。

    1. 定义openWebsite()并设置

      package com.dingjiaxiong.implicitintents;
      
      import androidx.appcompat.app.AppCompatActivity;
      
      import android.content.Intent;
      import android.net.Uri;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.View;
      import android.widget.EditText;
      
      public class MainActivity extends AppCompatActivity {
      
          private EditText mWebsiteEditText;
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              mWebsiteEditText = findViewById(R.id.website_edittext);
          }
      
          public void openWebsite(View view) {
      
              String url = mWebsiteEditText.getText().toString();
      
              Uri webpage = Uri.parse(url);
      
              Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
      
              if (intent.resolveActivity(getPackageManager()) != null) {
                  startActivity(intent);
              } else {
                  Log.d("ImplicitIntents", "Can't handle this intent!");
              }
      
      
          }
      
          public void openLocation(View view) {
          }
      
          public void shareText(View view) {
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
    2. 运行

      在这里插入图片描述

    73.3 实现打开位置按钮
    1. 定义openLocation()

      在这里插入图片描述

    2. 编写代码

    public void openLocation(View view) {
    
        String loc = mLocationEditText.getText().toString();
    
        Uri addressUri = Uri.parse("geo:0,0?q=" + loc);
    
        Intent intent = new Intent(Intent.ACTION_VIEW,addressUri);
    
        if (intent.resolveActivity(getPackageManager()) != null){
            startActivity(intent);
        }else{
            Log.d("ImplicitIntents", "Can't handle this intent!");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 运行

      这个应该需要手机上有谷歌地图才行。

      换个虚拟设备试试。

      在这里插入图片描述

      果然,调起来的谷歌地图。

    73.4 实现Share This Text按钮

    共享操作是用户与社交网络和其他应用程序共享应用程序中的项目的一种简单方法。尽管您可以使用隐式在您自己的应用程序中构建共享操作Intent,但 Android 提供了 ShareCompat.IntentBuilder帮助程序类来轻松实现共享。您可以使用ShareCompat.IntentBuilder构建Intent并启动一个选择器,让用户选择要共享的目标应用程序。

    ShareCompat.IntentBuilder在此任务中,您使用该类在文本编辑中实现共享一些文本。

    1. 定义shareText( )

      在这里插入图片描述

    2. 编写代码

      public void shareText(View view) {
      
          String txt = mShareTextEditText.getText().toString();
          String mimeType = "text/plain";
      
          ShareCompat.IntentBuilder
                  .from(this)
                  .setType(mimeType)
                  .setChooserTitle("Share this text with: ")
                  .setText(txt)
                  .startChooser();
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    3. 运行

      在这里插入图片描述

      新知识get

    73.5 接收一个隐含的Intent

    到目前为止,您已经创建了一个应用程序,它使用隐式Intent来启动其他应用程序的Activity. 在此任务中,您从另一个角度看待问题:允许Activity您的应用程序中的一个响应Intent从其他应用程序发送的隐式发送。

    Activity`始终可以使用显式的`Intent`. 要允许 an`Activity`接收隐式,您在应用程序的文件中`Intent`定义一个`Intent` *过滤器*以指示您有兴趣处理`AndroidManifest.xml`哪些类型的隐式。`Intent``Activity
    
    • 1

    为了将您的请求与设备上安装的特定应用程序相匹配,Android 系统会将您的隐式Intent与表明它们可以执行该操作Activity的过滤器相匹配。Intent如果安装了多个匹配的应用程序,则会向用户显示一个应用程序选择器,让他们选择要使用哪个应用程序来处理该应用程序Intent

    当设备上的应用程序发送一个隐含Intent的 时,Android 系统会将其操作和数据与包含正确过滤器的Intent任何可用的匹配。当过滤器匹配时:Activity``Intent``Intent``Activity``Intent

    • 如果只有一个匹配Activity,Android 让Activity处理Intent自己。
    • 如果有多个匹配项,Android 会显示一个应用选择器,以允许用户选择他们希望执行该操作的应用。

    在此任务中,您将创建一个非常简单的应用程序,该应用程序接收隐式Intent打开网页的 URI。当被隐式激活时Intent,该应用程序将请求的 URI 显示为TextView.

    1. 创建项目和布局

      在这里插入图片描述

      修改布局

      在这里插入图片描述

    2. 修改清单文件

      在这里插入图片描述

      这个是设置默认启动的

      添加

      <intent-filter>
          
          <action android:name="android.intent.action.VIEW"/>
          <category android:name="android.intent.category.DEFAULT"/>
          <category android:name="android.intent.category.BROWSABLE"/>
          <data android:scheme="http" android:host="developer.android.com"/>
          
      intent-filter>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8

      这些行为 定义了一个Intent过滤器ActivityIntentActivitycan 处理的类型。这个Intent过滤器声明了这些元素:

      过滤器类型价值火柴
      行动"android.intent.action.VIEW"任何Intent具有视图操作的。
      类别"android.intent.category.DEFAULT"任何隐含Intent的 . 必须包含此类别才能Activity接收任何隐含Intent的 .
      类别"android.intent.category.BROWSABLE"来自网页、电子邮件或其他来源的可浏览链接请求。
      数据android:scheme="http" android:host="developer.android.com"包含 的方案http 主机名的URI developer.android.com
    3. 处理intent

      package com.dingjiaxiong.implicitintentsreceiver;
      
      import androidx.appcompat.app.AppCompatActivity;
      
      import android.content.Intent;
      import android.net.Uri;
      import android.os.Bundle;
      import android.widget.TextView;
      
      public class MainActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
      
              Intent intent = getIntent();
      
              Uri uri = intent.getData();
      
              if (uri != null) {
                  String uri_string = "URI: " + uri.toString();
                  TextView textView = findViewById(R.id.text_uri_message);
                  textView.setText(uri_string);
              }
      
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
    4. 运行两个应用程序

      这之前一定要确保网址,因为设置了过滤器,接收方应用程序有一个非常严格的Intent过滤器,它只匹配精确的 URI 协议 ( http) 和主机 ( developer.android.com)。任何其他 URI 都会在默认 Web 浏览器中打开。

      在这里插入图片描述

      运行过程

      在这里插入图片描述

      新知识get,奈斯。

    73.6 小结
    • 如果您知道该操作,则隐式Intent允许您激活一个,但不是特定的应用程序或将处理该操作的应用程序。Activity``Activity
    • 可以Activity接收隐式Intent必须在文件中定义匹配一个或多个动作和类别Intent的过滤器。AndroidManifest.xml``Intent
    • Android系统匹配一个隐式的内容IntentIntent任何可用的过滤器Activity来确定Activity激活哪个。如果有多个可用Activity的,系统会提供一个选择器,以便用户选择一个。
    • 该类ShareCompat.IntentBuilder可以轻松构建Intent用于将数据共享到社交媒体或电子邮件的隐式。
  • 相关阅读:
    解决 Can‘t connect to MySQL server on ‘x‘ (10048) 问题
    毕业三年跳槽八次,换来惨痛的后果,资深架构师给出的一些建议!
    transformers AutoModelForMaskedLM简单使用
    mac装不了python3.7.6
    学习笔记——指针!
    一文吃透JavaScript中的DOM知识及用法
    torch.cuda.is_available() 解决方案
    Java—异常
    如何将PDF文件转换成Excel呢?
    使用Ubuntu演示介绍,Linux下安装和配置Redis、配置远程连接redis(保姆级教学)
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126358281