课程表

Android Studio

Android SDK

工具箱
速查手册

安卓 意图和过滤器

当前位置:免费教程 » 移动开发 » Android

Android 意图(Intent)是要执行的操作的抽象描述,它可以与 startActivity 一起使用来启动一个Activity, broadcastIntent 将它发送给任何感兴趣的BroadcastReceiver组件,并且 startService(Intent)或 bindService(Intent,ServiceConnection,int)与后台服务进行通信。

意图本身是一个Intent对象,是一个包含抽象的被动数据结构要执行的操作的描述。

例如,假设您有一个活动需要启动电子邮件客户端并使用您的Android设备发送电子邮件。为此,您的活动会将ACTION_SEND以及相应的选择器发送到Android Intent Resolver。指定的选择器为用户提供了选择如何发送电子邮件数据的正确界面。

  1. Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
  2. email.putExtra(Intent.EXTRA_EMAIL, recipients);
  3. email.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
  4. email.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
  5. startActivity(Intent.createChooser(email, "Choose an email client from..."));

上面的语法是调用startActivity方法来启动电子邮件活动,结果应如下所示 :

1.jpg

例如,假设您有一个需要在Android设备上的Web浏览器中打开URL的活动。为此,您的活动将ACTION_WEB_SEARCH Intent发送到Android Intent Resolver以在Web浏览器中打开给定的URL。Intent Resolver解析一系列活动,并选择最适合您的Intent的一个,在本例中为Web Browser Activity.然后,Intent Resolver会将您的网页传递到Web浏览器并启动Web浏览器活动。

  1. String q = "it1352";
  2. Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
  3. intent.putExtra(SearchManager.QUERY, q);
  4. startActivity(intent);

以上示例将在Android搜索引擎上搜索 w3xue,并在您的活动中提供w3xue的结果

有各种机制可以为每种类型的组件提供意图或取消活动、服务和广播接收器。


序号方法&描述
1

Context.startActivity()

将Intent对象传递给此方法以启动新活动或获取现有活动做新事物.

2

Context.startService()

意图object被传递给此方法以启动服务或向正在进行的服务发送新指令.

3

Context.sendBroadcast()

将Intent对象传递给此方法,以将消息传递给所有感兴趣的广播接收者.


意图对象

Intent对象是一组信息,被接受intent 的组件、还有安卓系统本身使用。

Intent对象可以根据通信或执行的内容包含以下组件 :

动作

这是Intent对象的必需部分,是一个字符串,用于命名要执行的操作 - 或者,对于广播意图,发生并正在报告的操作.该操作很大程度上决定了意图对象的其余部分的结构. Intent类定义了许多与不同意图相对应的动作常量.

Intent对象中的操作可以是由setAction()方法设置并由getAction()读取.

数据

向意图过滤器添加数据规范.规范可以只是数据类型(mimeType属性),只是URI,或者是数据类型和URI. URI由每个部分的单独属性指定 :

这些指定URL格式的属性是可选的,但也是相互依赖的 :

如果没有为intent过滤器指定方案,则忽略所有其他URI属性。

如果未指定主机对于过滤器,将忽略port属性和所有路径属性。

setData()方法仅将数据指定为URI,setType()指定它仅作为MIME类型,并且setDataAndType()将其指定为URI和MIME类型. URI由getData()读取,类型由getType()读取。

动作/数据对的一些示例是 :

No.行动/数据对&描述
1

ACTION_VIEW content://contacts/people/1

显示标识符为"1"的人的信息.

2

ACTION_DIAL content://contacts/people/1

与此人一起显示电话拨号器填写.

3

ACTION_VIEW tel:123

显示给定的电话拨号器填写的数字.

4

ACTION_DIAL tel:123

显示填写了给定号码的电话拨号器。

5

ACTION_EDIT content://contacts/people/1

编辑有关标识符为"1"的人的信息".

6

ACTION_VIEW content://contacts/people/

显示列表用户可以浏览的人.

7

ACTION_SET_WALLPAPER

显示设置选择壁纸

8

ACTION_SYNC

它将与数据同步,Constant Value is android.intent.action.SYNC

9

ACTION_SYSTEM_TUTORIAL

它将启动平台定义教程(默认教程或启动教程)

10

ACTION_TIMEZONE_CHANGED

它暗示时间区域已更改

11

ACTION_UNINSTALL_PACKAGE

它用于运行默认卸载程序

类别

该类别是可选部分Intent对象,它是一个字符串,包含有关应处理意图的组件类型的其他信息. addCategory()方法将一个类别放在Intent对象中,removeCategory()删除以前添加的类别,getCategories()获取当前在该对象中的所有类别的集合。

您可以查看意图过滤器的详细信息在下面的部分中,我们将了解如何使用类别来选择与Intent相对应的适当活动。

Extras

这将是键值对以获取应传递给处理意图的组件的其他信息.可以分别使用putExtras()和getExtras()方法设置和读取附加内容。

标志

这些标志是Intent对象的可选部分,用于指示Android系统如何启动活动,以及如何在启动活动后对其进行处理等。

No描 述
1

FLAG_ACTIVITY_CLEAR_TASK

如果在传递给Context.startActivity()的Intent中设置,此标志将导致任何现有任务这将与活动开始前要清除的活动相关联.也就是说,活动成为否则为空任务的新根,并且任何旧活动都已完成.这只能与FLAG_ACTIVITY_NEW_TASK一起使用.

2

FLAG_ACTIVITY_CLEAR_TOP

如果设置,并且正在启动的活动已经在当前任务中运行,然后不是启动该活动的新实例,而是关闭其上的所有其他活动,并且此意图将被传递到(现在在顶部)旧的活动作为新的意图.

3

FLAG_ACTIVITY_NEW_TASK

此标志通常用于活动想要呈现"启动器"样式的行为:它们为用户提供了可以完成的单独事物的列表,否则完全独立于启动它们的活动.

组件名称

此可选字段是andr oid ComponentName 对象,表示Activity,Service或BroadcastReceiver类.如果已设置,则Intent对象将传递到指定类的实例,否则Android将使用Intent对象中的其他信息来定位合适的目标。

组件名称由setComponent设置(),setClass()或setClassName()并由getComponent()读取。

意图类型

以下两种类型的意图Android支持:

2.jpg

显式意图

显式意图将连接应用程序的内部世界,假设您想要将一个活动连接到另一个活动,我们可以通过显式意图执行此引用,下面的图像通过单击按钮将第一个活动连接到第二个活动。

3.jpg

这些意图通过名称和目标组件指定目标组件它们通常用于应用程序内部消息 - 例如激活开始从属服务或启动姐妹活动。例如:

  1. // Explicit Intent by specifying its class name
  2. Intent i = new Intent(FirstActivity.this, SecondActivity.class);
  3.  
  4. // Starts TargetActivity
  5. startActivity(i);

隐含意图

这些意图没有命名目标,组件名称的字段留空.隐式意图通常用于激活其他应用程序中的组件。例如 :

  1. Intent read1=new Intent();
  2. read1.setAction(android.content.Intent.ACTION_VIEW);
  3. read1.setData(ContactsContract.Contacts.CONTENT_URI);
  4. startActivity(read1);

上面的代码将给出如下所示的结果:

4.jpg

接收意图的目标组件可以使用 getExtras()方法获取源组件发送的额外数据。例如 :

  1. // Get bundle object at appropriate place in your code
  2. Bundle extras = getIntent().getExtras();
  3.  
  4. // Extract data using passed keys
  5. String value1 = extras.getString("Key1");
  6. String value2 = extras.getString("Key2");

示例

以下示例显示了Android Intent启动各种Android内置应用程序的功能。

Step描述
1您将使用Android studio IDE创建一个Android应用程序,并将其命名为 com.example.saira_000.myapplication 下的我的应用程序 .
2修改 src/main/java/MainActivity.java 文件并添加代码以定义两个对应的两个按钮即ie.启动浏览器并启动手机.
3修改布局XML文件 res/layout/activity_main.xml 以线性布局添加三个按钮.
4运行应用程序以启动Android模拟器并验证应用程序中所做更改的结果.

以下是修改后的主要活动文件的内容 src/com.example.My Application/MainActivity.java

  1. package com.example.saira_000.myapplication;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. public class MainActivity extends AppCompatActivity {
  9.    Button b1,b2;
  10.  
  11.    @Override
  12.    protected void onCreate(Bundle savedInstanceState) {
  13.       super.onCreate(savedInstanceState);
  14.       setContentView(R.layout.activity_main);
  15.  
  16.       b1=(Button)findViewById(R.id.button);
  17.       b1.setOnClickListener(new View.OnClickListener() {
  18.  
  19.          @Override
  20.          public void onClick(View v) {
  21.             Intent i = new Intent(android.content.Intent.ACTION_VIEW, 
  22.                Uri.parse("http://www.example.com"));
  23.             startActivity(i);
  24.          }
  25.       });
  26.  
  27.       b2=(Button)findViewById(R.id.button2);
  28.       b2.setOnClickListener(new View.OnClickListener() {
  29.          @Override
  30.          public void onClick(View v) {
  31.             Intent i = new Intent(android.content.Intent.ACTION_VIEW,
  32.                Uri.parse("tel:9510300000"));
  33.             startActivity(i);
  34.          }
  35.       });
  36.    }}

以下是 res/layout/activity_main.xml 的内容 :

  1. <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.    xmlns:tools="http://schemas.android.com/tools" 
  3.    android:layout_width="match_parent"
  4.    android:layout_height="match_parent" 
  5.    android:paddingLeft="@dimen/activity_horizontal_margin"
  6.    android:paddingRight="@dimen/activity_horizontal_margin"
  7.    android:paddingTop="@dimen/activity_vertical_margin"
  8.    android:paddingBottom="@dimen/activity_vertical_margin" 
  9.    tools:context=".MainActivity">
  10.    
  11.    <TextView
  12.       android:id="@+id/textView1"
  13.       android:layout_width="wrap_content"
  14.       android:layout_height="wrap_content"
  15.       android:text="Intent Example"
  16.       android:layout_alignParentTop="true"
  17.       android:layout_centerHorizontal="true"
  18.       android:textSize="30dp" />
  19.       
  20.    <TextView
  21.       android:id="@+id/textView2"
  22.       android:layout_width="wrap_content"
  23.       android:layout_height="wrap_content"
  24.       android:text="Tutorials point"
  25.       android:textColor="#ff87ff09"
  26.       android:textSize="30dp"
  27.       android:layout_below="@+id/textView1"
  28.       android:layout_centerHorizontal="true" />
  29.       
  30.    <ImageButton
  31.       android:layout_width="wrap_content"
  32.       android:layout_height="wrap_content"
  33.       android:id="@+id/imageButton"
  34.       android:src="@drawable/abc"
  35.       android:layout_below="@+id/textView2"
  36.       android:layout_centerHorizontal="true" />
  37.       
  38.    <EditText
  39.       android:layout_width="wrap_content"
  40.       android:layout_height="wrap_content"
  41.       android:id="@+id/editText"
  42.       android:layout_below="@+id/imageButton"
  43.       android:layout_alignRight="@+id/imageButton"
  44.       android:layout_alignEnd="@+id/imageButton" />
  45.       
  46.    <Button
  47.       android:layout_width="wrap_content"
  48.       android:layout_height="wrap_content"
  49.       android:text="Start Browser"
  50.       android:id="@+id/button"
  51.       android:layout_alignTop="@+id/editText"
  52.       android:layout_alignRight="@+id/textView1"
  53.       android:layout_alignEnd="@+id/textView1"
  54.       android:layout_alignLeft="@+id/imageButton"
  55.       android:layout_alignStart="@+id/imageButton" />
  56.       
  57.    <Button
  58.       android:layout_width="wrap_content"
  59.       android:layout_height="wrap_content"
  60.       android:text="Start Phone"
  61.       android:id="@+id/button2"
  62.       android:layout_below="@+id/button"
  63.       android:layout_alignLeft="@+id/button"
  64.       android:layout_alignStart="@+id/button"
  65.       android:layout_alignRight="@+id/textView2"
  66.       android:layout_alignEnd="@+id/textView2" /></RelativeLayout>

以下将是 res/values/strings.xml 的内容来定义两个新的常量 :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.    <string name="app_name">My Applicaiton</string>
  4. </resources>

以下是 AndroidManifest.xml 的默认内容 :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="com.example.saira_000.myapplication">
  4.  
  5.    <application
  6.       android:allowBackup="true"
  7.       android:icon="@mipmap/ic_launcher"
  8.       android:label="@string/app_name"
  9.       android:supportsRtl="true"
  10.       android:theme="@style/AppTheme">
  11.       <activity android:name=".MainActivity">
  12.          <intent-filter>
  13.             <action android:name="android.intent.action.MAIN" />
  14.             <category android:name="android.intent.category.LAUNCHER" />
  15.          </intent-filter>
  16.       </activity>
  17.    </application>
  18. </manifest>

下面我们来跑一下程序,这里假设你已经安装了 AVD。在 Android Studio中打开一个 activity 文件,并点击工具栏的运行按钮  。安卓模拟器中会出现如下的界面:

intent5.png

现在点击启动浏览器按钮,这将启动配置的浏览器并显示http://www.example.com如下图所示 :

android_intent_browser.png

类似地,您可以使用"开始电话"按钮启动电话界面,这样您就可以拨打已经给定的电话号码.

意图过滤器

您已经看到如何使用Intent来调用另一个活动。Android操作系统使用过滤器来查明可以通过指定的一组操作,类别,与Intent关联的数据方案来处理Intent的活动,服务和广播接收器集.您将使用清单文件中的< intent-filter> 元素列出与任何活动,服务或广播接收器关联的操作,类别和数据类型。

以下是 AndroidManifest.xml 文件的一部分示例,用于指定活动 com.example.MyApplication.CustomActivity :

  1. <activity android:name=".CustomActivity"
  2.    android:label="@string/app_name">
  3.    
  4.    <intent-filter>
  5.       <action android:name="android.intent.action.VIEW" />
  6.       <action android:name="com.example.My Application.LAUNCH" />
  7.       <category android:name="android.intent.category.DEFAULT" />
  8.       <data android:scheme="http" />
  9.    </intent-filter>
  10.    </activity>

一旦此活动与上述过滤器一起定义,其他活动将能够使用 android.intent.action调用此活动.查看,或使用 com.example.My Application.LAUNCH 操作,前提是他们的类别是 android.intent.category.DEFAULT。

< data> 元素指定要调用的活动所期望的数据类型,并且对于上面的示例,我们的自定义活动期望数据以"http://"开始。

可能存在意图可以通过多个活动或服务的过滤器的情况,可以询问用户要激活哪个组件.如果找不到目标,则会引发异常。

在调用活动之前有以下测试Android检查 :

过滤器< intent-filter>可以列出多个动作,如上所示,但此列表不能为空;过滤器必须包含至少一个< action>元素,否则会阻止所有意图.如果提到了多个操作,则Android会在调用活动之前尝试匹配上述操作之一.

过滤器< intent-filter>可以列出零个,一个或多个类别.如果没有提到的类别,那么Android总是通过此测试,但如果提到了多个类别,那么对于通过类别测试的意图,Intent对象中的每个类别必须与过滤器中的类别匹配.

每个< data> element可以指定URI和数据类型(MIME媒体类型).对于URI的每个部分,都有单独的属性,如 scheme,host,port 和 path .只有当类型与过滤器中列出的类型匹配时,包含URI和数据类型的Intent对象才会传递测试的数据类型部分.

示例

以下示例是对上述示例的修改.在这里,我们将看到Android解决冲突如果一个intent调用了两个定义的活动,接下来如何使用过滤器调用自定义活动,第三个是如果Android没有为intent定义适当的活动的异常情况.

Step描述
1您将使用android studio创建一个Android应用程序,包名为 com.example.it13527.myapplication;  名字为My Application
2修改 src/Main/Java/MainActivity.java 文件并添加代码以定义与布局文件中定义的三个按钮对应的三个侦听器.
3添加新的 src/Main/Java/CustomActivity.java 档案o有一个自定义活动将由不同的意图调用.
4修改布局XML文件 res/layout/activity_main.xml 以线性布局添加三个按钮.
5添加一个布局XML文件 res/layout/custom_view.xml 添加一个简单的< TextView>通过意图显示传递的数据.
6修改 AndroidManifest.xml 以添加< intent-filter>定义调用自定义活动的意图的规则.
7运行应用程序以启动Android模拟器并验证应用程序中所做更改的结果.

以下是修改后的主要活动文件 src/MainActivity.java 的内容:

  1. package com.example.it13527.myapplication;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9.  
  10. public class MainActivity extends AppCompatActivity {
  11.    Button b1,b2,b3;
  12.    @Override
  13.    protected void onCreate(Bundle savedInstanceState) {
  14.       super.onCreate(savedInstanceState);
  15.       setContentView(R.layout.activity_main);
  16.       b1=(Button)findViewById(R.id.button);
  17.       b1.setOnClickListener(new View.OnClickListener() {
  18.       
  19.          @Override
  20.          public void onClick(View v) {
  21.             Intent i = new Intent(android.content.Intent.ACTION_VIEW,
  22.                Uri.parse("http://www.example.com"));
  23.             startActivity(i);
  24.          }
  25.       });
  26.  
  27.       b2 = (Button)findViewById(R.id.button2);
  28.       b2.setOnClickListener(new View.OnClickListener() {
  29.          @Override
  30.          public void onClick(View v) {
  31.             Intent i = new Intent("com.example.it13527.myapplication.LAUNCH",Uri.parse("http://www.example.com"));
  32.             startActivity(i);
  33.          }
  34.       });
  35.  
  36.       b3 = (Button)findViewById(R.id.button3);
  37.       b3.setOnClickListener(new View.OnClickListener() {
  38.          @Override
  39.          public void onClick(View v) {
  40.             Intent i = new Intent("com.example.
  41.                My Application.LAUNCH",
  42.                   Uri.parse("https://www.example.com"));
  43.             startActivity(i);
  44.          }
  45.       });
  46.    }
  47. }

以下是修改后的主要活动文件的内容 src/com.example.MyApplication/CustomActivity.java

  1. package com.example.it13527.myapplication;
  2.  
  3. import android.app.Activity;
  4. import android.net.Uri;
  5. import android.os.Bundle;
  6. import android.widget.TextView;
  7.  
  8.  
  9. public class CustomActivity extends Activity {
  10.    @Override
  11.    public void onCreate(Bundle savedInstanceState) {
  12.       super.onCreate(savedInstanceState);
  13.       setContentView(R.layout.custom_view);
  14.       TextView label = (TextView) findViewById(R.id.show_data);
  15.       Uri url = getIntent().getData();
  16.       label.setText(url.toString());
  17.    }
  18. }

以下是 res/layout/activity_main.xml 文件的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout 
  3.    xmlns:android="http://schemas.android.com/apk/res/android"
  4.    xmlns:tools="http://schemas.android.com/tools"
  5.    android:layout_width="match_parent"
  6.    android:layout_height="match_parent"
  7.    android:paddingBottom="@dimen/activity_vertical_margin"
  8.    android:paddingLeft="@dimen/activity_horizontal_margin"
  9.    android:paddingRight="@dimen/activity_horizontal_margin"
  10.    android:paddingTop="@dimen/activity_vertical_margin"
  11.    tools:context="com.example.it13527.myapplication.MainActivity">
  12.  
  13.    <TextView
  14.       android:id="@+id/textView1"
  15.       android:layout_width="wrap_content"
  16.       android:layout_height="wrap_content"
  17.       android:text="Intent Example"
  18.       android:layout_alignParentTop="true"
  19.       android:layout_centerHorizontal="true"
  20.       android:textSize="30dp" />
  21.  
  22.    <TextView
  23.       android:id="@+id/textView2"
  24.       android:layout_width="wrap_content"
  25.       android:layout_height="wrap_content"
  26.       android:text="Tutorials point"
  27.       android:textColor="#ff87ff09"
  28.       android:textSize="30dp"
  29.       android:layout_below="@+id/textView1"
  30.       android:layout_centerHorizontal="true" />
  31.  
  32.    <ImageButton
  33.       android:layout_width="wrap_content"
  34.       android:layout_height="wrap_content"
  35.       android:id="@+id/imageButton"
  36.       android:src="@drawable/abc"
  37.       android:layout_below="@+id/textView2"
  38.       android:layout_centerHorizontal="true" />
  39.  
  40.    <EditText
  41.       android:layout_width="wrap_content"
  42.       android:layout_height="wrap_content"
  43.       android:id="@+id/editText"
  44.       android:layout_below="@+id/imageButton"
  45.       android:layout_alignRight="@+id/imageButton"
  46.       android:layout_alignEnd="@+id/imageButton" />
  47.  
  48.    <Button
  49.       android:layout_width="wrap_content"
  50.       android:layout_height="wrap_content"
  51.       android:text="Start Browser"
  52.       android:id="@+id/button"
  53.       android:layout_alignTop="@+id/editText"
  54.       android:layout_alignLeft="@+id/imageButton"
  55.       android:layout_alignStart="@+id/imageButton"
  56.       android:layout_alignEnd="@+id/imageButton" />
  57.  
  58.    <Button
  59.       android:layout_width="wrap_content"
  60.       android:layout_height="wrap_content"
  61.       android:text="Start browsing with launch action"
  62.       android:id="@+id/button2"
  63.       android:layout_below="@+id/button"
  64.       android:layout_alignLeft="@+id/button"
  65.       android:layout_alignStart="@+id/button"
  66.       android:layout_alignEnd="@+id/button" />
  67.    <Button
  68.       android:layout_width="wrap_content"
  69.       android:layout_height="wrap_content"
  70.       android:text="Exceptional condition"
  71.       android:id="@+id/button3"
  72.       android:layout_below="@+id/button2"
  73.       android:layout_alignLeft="@+id/button2"
  74.       android:layout_alignStart="@+id/button2"
  75.       android:layout_toStartOf="@+id/editText"
  76.       android:layout_alignParentEnd="true" />
  77. </RelativeLayout>

下面是文件 res/layout/custom_view.xml 的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical" android:layout_width="match_parent"
  4.    android:layout_height="match_parent">
  5.    <TextView android:id="@+id/show_data"
  6.       android:layout_width="fill_parent"
  7.       android:layout_height="400dp"/>
  8. </LinearLayout>

下面是文件 res/values/strings.xml 的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.    <string name="app_name">My Application</string>
  4. </resources>

下面是文件 AndroidManifest.xml 的内容:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="com.example.it13527.myapplication">
  4.  
  5.    <application
  6.       android:allowBackup = "true"
  7.       android:icon = "@mipmap/ic_launcher"
  8.       android:label = "@string/app_name"
  9.       android:supportsRtl = "true"
  10.       android:theme = "@style/AppTheme">
  11.       <activity android:name = ".MainActivity">
  12.          <intent-filter>
  13.             <action android:name = "android.intent.action.MAIN" />
  14.             <category android:name = "android.intent.category.LAUNCHER" />
  15.          </intent-filter>
  16.       </activity>
  17.       
  18.       <activity android:name="com.example.it13527.myapplication.CustomActivity">
  19.  
  20.          <intent-filter>
  21.             <action android:name = "android.intent.action.VIEW" />
  22.             <action android:name = "com.example.it13527.myapplication.LAUNCH" />
  23.             <category android:name = "android.intent.category.DEFAULT" />
  24.             <data android:scheme = "http" />
  25.          </intent-filter>
  26.  
  27.       </activity>
  28.    </application>
  29.  
  30. </manifest>

让我们尝试运行我的应用程序。 我假设您在进行环境设置时创建了AVD。 要从Android Studio运行应用程序,请打开项目的某个活动文件,然后单击工具栏中的"运行Eclipse运行图标"图标。 Android Studio会在您的AVD上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口:

intent6.png

现在让我们从第一个按钮"使用VIEW Action启动浏览器"开始。 在这里,我们使用过滤器"android.intent.action.VIEW"定义了我们的自定义活动,并且已经有一个针对Android定义的VIEW动作的默认活动,即启动Web浏览器,因此android显示以下两个选项来选择您的活动 想要发布。

intent7.png

现在,如果您选择浏览器,那么Android将启动Web浏览器并打开example.com网站,但如果您选择IndentDemo选项,那么Android将启动CustomActivity,它只会捕获传递的数据,并在文本视图中显示如下:

intent8.png

现在返回使用后退按钮并单击"使用LAUNCH操作启动浏览器"按钮,此处Android应用过滤器以选择定义活动,它只需启动您的自定义活动

再次,返回使用后退按钮并单击"异常条件"按钮,这里Android尝试找出给定意图的有效过滤器,但它没有找到定义的有效活动,因为这次我们使用数据作为https而不是http 虽然我们正在给出正确的操作,但Android会引发异常并显示以下屏幕:

intent9.png


转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号