android 详细介绍Intent 显式,隐式,打开网页,打电话,传递数据

本文主要介绍了intent的显式用法,隐式用法,打开网页,打电话,传递数据这几个用法。在前面只放了关键的代码,文章最后会将整个demo的代码以及压缩包附上。

显式用法:

新建一个SecondeActivity,在SecondActivity中添加一个按钮,提示这个是另一个Activity。在MainActivity中添加代码

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

 Intent(Context packageContext,Class<?> cls),参数1Context表示要求提供一个启动活动的上下文,参数2Class指定想要启动的目标活动。

隐式用法:

在AndroidManifest.xml中标签activity name为SecondActivity的标签下添加intent-filter标签,然后在intent-filter标签下添加action和actegory。

<activity android:name=".SecondActivity" 
    <intent-filter>
        <!--指明当前活动可以响应com.zhy.intentdemo.ACTION_START这个action-->
        <action android:name="com.zhy.intentdemo.ACTION_START"/>
        <!--包含了一些附加信息,明确的指定了当前活动能够响应的Intent中还可能带有的category-->
        <!--只有<action>和<category>中的内容能够同时匹配上Intent中指定的action和category时,这个活动才能响应该Intent-->
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="com.zhy.intentdemo.MY_CATEGORY"/>
    </intent-filter>
</activity>

 在标签中指明了当前活动可以响应com.zhy.intentdemo.ACTION_START这个action,而<category>标签包含了一些附加信息,更精确的指明了当前活动能够响应的Intent中还可能带有的category。只有<action>和<category>中的内容同时匹配上Intent中指定的action和category,这个活动才能响应该Intent。

然后在MainActivity添加代码:

Intent intent1 = new Intent("com.zhy.intentdemo.ACTION_START");
intent1.addCategory("com.zhy.intentdemo.MY_CATEGORY");
startActivity(intent1);

 这里使用了另一个Intent的构造函数,直接将action的字符串传了进去,表明我们想要启动能够响应com.zhy.intentdemo.ACTION_START这个action的活动,没有指定android.intent.category.DEFAULT这个category是因为它是一种默认的category,在调用startActivity()方法时,会自动将这个category添加到intent中。

打开网页:

在MainActivity中添加代码:

Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent2);

 首先指定了Intent的action为Intent.ACTION_VIEW,这是android系统内置的一个动作,其常量值为android.intent.action.View。然后通过Uri.parse()方法,将一个网址字符串解析成一个Uri对象,再调用Intent的setData()方法将这个Uri对象传递进去。对于这种情况,我们还可以在<intent-filter>标签中再配置一个<data>标签,用于更精确的指定当前活动能够响应什么类型的数据。<data>标签中主要可以配置以下内容。

android:scheme 用于指定数据的协议部分,如上例中的http部分。

android:host 用于指定数据的主机名部分,如上例中的www.baidi.com部分。

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

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

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

打电话:

在MainActivity中添加代码

Intent intent3 = new Intent(Intent.ACTION_DIAL);
intent3.setData(Uri.parse("tel:10086"));
startActivity(intent3);

 首先指定了action为Intent.ACTION_DIAL,这是android系统内置的另一个动作。在data部分指定了协议是tel,号码是10086.

向下一个活动传递消息:

添加MainActivity中添加如下代码

Intent intent4 = new Intent(MainActivity.this, SecondActivity.class);
intent4.putExtra("data", "Hello SecondActivity");
startActivity(intent4);

 通过putExtra()方法传递了一个字符串,第一个参数是键,用于后面从Intent中取值,第二个参数才是真正要传递的值。接下来在SecondActivity中添加代码

Intent intent = getIntent();
String data = intent.getStringExtra("data");
tv.setText(data);

首先通过getIntent()方法获取到用于启动SecondActivity的Intent,然后调用getStringExtra()方法获取传递的数据,如果是整型的数据就是getIntExtra(),如果是布尔类型就使用getBooleanExtra()等等。

返回数据给上一个活动:

需要返回数据给上一个活动,要使用到另一个方法startActivityForResult(),此方法接收两个参数,第一个参数是Intent,第二个参数为请求码,用于在之后的回调中判断数据来源,在MainActivity中添加如下代码

Intent intent5 = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent5, 1);

 在这里请求码为1,接下来在SecondActivity中添加返回数据的逻辑

Intent intent = new Intent();
intent.putExtra("data_return","Hello MainActivity");
setResult(RESULT_OK,intent);
finish();

 最关键的setResult()方法接收两个参数,参数1用于向上一个活动返回处理结果,一般只用RESULT_OK或RESULT_CANCELED这两个值,参数2则是把带有数据的intent传递回去,然后调用finish()方法结束当前活动。

因为使用的是startActivityForResult()方法来启动的SecondActivity,在SecondActivity被销毁之后会回调上一个活动的OnActivityResult()方法,因此需要在MainActivity中重写OnActivityResult()方法。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1:
            if (resultCode == RESULT_OK) {
                String str = data.getStringExtra("data_return");
                tv.setText(str);
            }
            break;
        default:
            break      
    }
}

 onActivityResult()方法带有三个参数,第一个参数requestCode,即在启动活动的时候传入的请求码,第二个参数resultCode,即在返回数据时传入的处理结果,第三个参数data,即带有返回数据的Intent。由于在一个活动中可能调用startActivityForResult()方法去启动多种不同的活动,每一个活动返回的数据都会回调到onActivityResult()这个方法中,因此我们首先要做的是通过检查requestCode的值来判断数据来源,确定数据是从SecondActivity返回的之后,再通过resultCode的值来判断处理结果是否成功。最后从data中将值取出来。

以下是整个demo完整的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转到Second Activity(显式Intent)"/>

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转到Second Activity(隐式Intent)"
        android:layout_below="@id/button_1"/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开网页(隐式Intent)"
        android:layout_below="@id/button_2"/>

    <Button
        android:id="@+id/button_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拨号(隐式Intent)"
        android:layout_below="@id/button_3"/>

    <Button
        android:id="@+id/button_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="向下一个活动传递数据"
        android:layout_below="@id/button_4"/>

    <Button
        android:id="@+id/button_6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回数据给上一个活动"
        android:layout_below="@id/button_5"/>

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button_6"/>

</RelativeLayout>

 activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".SecondActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Second Activity"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button"/>

</RelativeLayout>

 MainActivity.java

package com.zhy.intentdemo;

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

/**
 * Intent
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private TextView tv;

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

    private void initView() {
        button1 = findViewById(R.id.button_1);
        button2 = findViewById(R.id.button_2);
        button3 = findViewById(R.id.button_3);
        button4 = findViewById(R.id.button_4);
        button5 = findViewById(R.id.button_5);
        button6 = findViewById(R.id.button_6);
        tv = findViewById(R.id.tv);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
        button6.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_1:
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
                break;
            case R.id.button_2:
                Intent intent1 = new Intent("com.zhy.intentdemo.ACTION_START");
                intent1.addCategory("com.zhy.intentdemo.MY_CATEGORY");
                startActivity(intent1);
                break;
            case R.id.button_3:
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent2);
                break;
            case R.id.button_4:
                Intent intent3 = new Intent(Intent.ACTION_DIAL);
                intent3.setData(Uri.parse("tel:10086"));
                startActivity(intent3);
                break;
            case R.id.button_5:
                Intent intent4 = new Intent(MainActivity.this, SecondActivity.class);
                intent4.putExtra("data", "Hello SecondActivity");
                startActivity(intent4);
                break;
            case R.id.button_6:
                Intent intent5 = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(intent5, 1);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String str = data.getStringExtra("data_return");
                    tv.setText(str);
                }
                break;
            default:
                break;
        }
    }

}

 SecondActivity.java

package com.zhy.intentdemo;

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

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private Button button;

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

    private void initData() {
        Intent intent = getIntent();
        String data = intent.getStringExtra("data");
        tv.setText(data);
    }

    private void initView() {
        tv = findViewById(R.id.tv);
        button = findViewById(R.id.button);

        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Intent intent = new Intent();
                intent.putExtra("data_return","Hello MainActivity");
                setResult(RESULT_OK,intent);
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 按返回键和点击按钮为相同效果
     */
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data_return","Hello MainActivity");
        setResult(RESULT_OK,intent);
        finish();
    }
}

demo传送门

猜你喜欢

转载自blog.csdn.net/zhaohuiyang_949/article/details/81749712