Intent.Intent打开网页的隐式,另一个打开地图上的位置。Intent打开网页的新应用程序。创建项目

创建布局
设置资源
<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>
<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>
此操作使用隐式Intent将给定的 URI 发送到Activity可以处理该隐式Intent的(例如 Web 浏览器)。
定义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) {
}
}
运行

定义openLocation()

编写代码
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!");
}
}
运行
这个应该需要手机上有谷歌地图才行。
换个虚拟设备试试。

果然,调起来的谷歌地图。
共享操作是用户与社交网络和其他应用程序共享应用程序中的项目的一种简单方法。尽管您可以使用隐式在您自己的应用程序中构建共享操作Intent,但 Android 提供了 ShareCompat.IntentBuilder帮助程序类来轻松实现共享。您可以使用ShareCompat.IntentBuilder构建Intent并启动一个选择器,让用户选择要共享的目标应用程序。
ShareCompat.IntentBuilder在此任务中,您使用该类在文本编辑中实现共享一些文本。
定义shareText( )

编写代码
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();
}
运行

新知识get
到目前为止,您已经创建了一个应用程序,它使用隐式Intent来启动其他应用程序的Activity. 在此任务中,您从另一个角度看待问题:允许Activity您的应用程序中的一个响应Intent从其他应用程序发送的隐式发送。
Activity`始终可以使用显式的`Intent`. 要允许 an`Activity`接收隐式,您在应用程序的文件中`Intent`定义一个`Intent` *过滤器*以指示您有兴趣处理`AndroidManifest.xml`哪些类型的隐式。`Intent``Activity
为了将您的请求与设备上安装的特定应用程序相匹配,Android 系统会将您的隐式Intent与表明它们可以执行该操作Activity的过滤器相匹配。Intent如果安装了多个匹配的应用程序,则会向用户显示一个应用程序选择器,让他们选择要使用哪个应用程序来处理该应用程序Intent。
当设备上的应用程序发送一个隐含Intent的 时,Android 系统会将其操作和数据与包含正确过滤器的Intent任何可用的匹配。当过滤器匹配时:Activity``Intent``Intent``Activity``Intent
Activity,Android 让Activity处理Intent自己。在此任务中,您将创建一个非常简单的应用程序,该应用程序接收隐式Intent打开网页的 URI。当被隐式激活时Intent,该应用程序将请求的 URI 显示为TextView.
创建项目和布局

修改布局

修改清单文件

这个是设置默认启动的
添加
<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>
这些行为 定义了一个Intent过滤器Activity,Intent即Activitycan 处理的类型。这个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。 |
处理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);
}
}
}
运行两个应用程序
这之前一定要确保网址,因为设置了过滤器,接收方应用程序有一个非常严格的Intent过滤器,它只匹配精确的 URI 协议 ( http) 和主机 ( developer.android.com)。任何其他 URI 都会在默认 Web 浏览器中打开。

运行过程

新知识get,奈斯。
Intent允许您激活一个,但不是特定的应用程序或将处理该操作的应用程序。Activity``ActivityActivity接收隐式Intent必须在文件中定义匹配一个或多个动作和类别Intent的过滤器。AndroidManifest.xml``IntentIntent和Intent任何可用的过滤器Activity来确定Activity激活哪个。如果有多个可用Activity的,系统会提供一个选择器,以便用户选择一个。ShareCompat.IntentBuilder可以轻松构建Intent用于将数据共享到社交媒体或电子邮件的隐式。