隐式Intent

新建一个工程,工程名为ActivityTest,分别创建两个活动FirstActivity和SecondActivity,布局文件分别为first_layout和second_layout,并分别添加一个Button控件。

1.first_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_1"
        android:text="Button 1"/>
</LinearLayout>

2.second_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_2"
        android:text="Button 2"/>
</LinearLayout>

3.现在通过FirstActivity活动隐式地启动SecondActivity活动,需在AndroidManifest.xml文件中配置<intent-filter>内容,将action配置为

com.example.activitytest.ACTION_START, category配置为android.intent.category.DEFAULT

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

4.SecondActivity.java文件

package com.example.activitytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
    }
}

5.FirstActivity.java文件中添加按键监听,在onClick方法中隐式启动活动

package com.example.activitytest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        //通过id找到按键控件对象
        Button button1 = (Button) findViewById(R.id.button_1);
        //设置按键点击事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 隐式Intent
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                //启动SecondActivity活动
                startActivity(intent);

            }
        });
    }
}

6.action和category要同时匹配上才能响应,SecondActivity中指定了默认的category,在调用startActivity方法的时候会自动将这

个android.intent.category.DEFAULT添加到Intent中,运行一下程序,点击Button 1一下,会启动SecondActivity,说明<intent-filter>下的配置生效了。

7.每个Intent只能指定一个action,但却能指定多个category,现在在onClick方法中再增加一个category(com.example.activitytest.MY_CATEGORY)

package com.example.activitytest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);

        //通过id找到按键控件对象
        Button button1 = (Button) findViewById(R.id.button_1);
        //设置按键点击事件
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 隐式Intent
                Intent intent = new Intent("com.example.activitytest.ACTION_START");
                //新增一个category
                intent.addCategory("com.example.activitytest.MY_CATEGORY");
                //启动SecondActivity活动
                startActivity(intent);

            }
        });
    }
}

8.运行程序,点击一下Button 1,app出现崩溃,在logcat查看日志,错误信息提示没有一个活动可以响应Intent,抛出了ActivityNotFoundException

异常.

10-05 21:12:11.495 25997-25997/com.example.activitytest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.activitytest, PID: 25997
                                                                          android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.activitytest.ACTION_START cat=[com.example.activitytest.MY_CATEGORY] }

9.检查AndroidManifest.xml中的SecondActivity中的<intent-filter>没有配置category为com.example.activitytest.MY_CATEGORY,将其添加上,如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.example.activitytest.MY_CATEGORY"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

10.重新运行程序,点击一下Button 1,会开启SecondActivity活动,刚才的崩溃问题也得到解决。

更多隐式Intent的用法

1.将FirstActivity中按钮点击事件的代码修改成如下

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.baidu.com"));
        startActivity(intent);

    }
});

2.这里指定了系统内置的动作Intent.ACTION_VIEW,其常量值为android.intent.action.View,然后通过Uri.parse()方法,将一个网址字符串解析成一个Uri对象,再调用Intent的setData()方法将这个Uri对象传递进去,运行程序,点击Button 1会打开系统浏览器并跳转到百度主页。

3.与此对应,我们还可以在<intent-filter>标签中配置一个<data>标签,用于更精确地指定当前活动能够响应什么类型的数据,<data>标签主要可以配

置以下内容。

android:scheme  -用于指定数据的协议部分,如上面的http部分

android:host -用于指定数据的主机名部分,如上面的www.baidu.com部分

android:port -用于指定数据的端口部分,一般紧随在主机名之后

android:path -用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容

android:mimeType -用于指定可以处理的数据类型,允许使用通配符的方式进行指定。

4.右击com.example.activitytest包->New->Activity->Empty Activity,新建ThirdActivity,并勾选Generate Layout File,给布局起名为third_layout,点

击finish完成创建,然后编辑third_layout.xml文件成如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
 >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button_3"
        android:text="Button 3"/>
</LinearLayout>

5.在AndroidManifest.xml文件中将ThirdActivity的<intent-filter>中配置了当前活动能够响应的action是Intent.ACTION_VIEW的常量值,而category使用了默认的category值,另外在<data>标签中配置了andoird:scheme指定了数据的协议是http协议,这样ThirdActivity应该就和浏览器一样,能够响应一个打开网页的Intent了。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.activitytest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".FirstActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.activitytest.ACTION_START"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="com.example.activitytest.MY_CATEGORY"/>
            </intent-filter>
        </activity>
        <activity android:name=".ThirdActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:scheme="http"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

6.运行程序,点击Button 1会弹出一个列表,显示目前能够响应这个Intent的所有程序,如果选择ActivityTest,则会启动ThirdActivity。

隐式Intent启动一个系统的拔打电话界面

1.将FirstActivity中的onClick方法中的内容修改为如下

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:10086"));
        startActivity(intent);

    }
});

2.运行程序,点击Button 1会启动一个系统拔号界面。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨依依

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值