Android开发 Intent

文章详细介绍了Android中Intent的使用,包括显式Intent的三种设置方式,用于精确匹配发送方和接收方;隐式Intent的使用,常用于系统应用间的模糊匹配;以及Intent如何在组件间传递数据,包括从发送方到接收方以及接收方回传数据给发送方的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Intent

在组件之间传递信息,一般需要设置发送方,接收方和数据。

下图是Intent 的常用属性:

 

2. Intent分类

1)显式Intent:精确匹配发送方和接收方

方法一:

startActivity(new Intent(this,MainActivity4.class));

方法二:

Intent intent = new Intent();
intent.setClass(this, MainActivity4.class);
startActivity(intent);

方法三:

Intent intent = new Intent();
ComponentName cp = new ComponentName(this, MainActivity4.class);
intent.setComponent(cp);
startActivity(intent);

方法三除了可以和前两个方法一样,直接使用类,也可以通过包名和类名来指定。

这样可以在无法得到第三方应用的类的时候,用包名和类名来跳转。

2)隐式Intent

没有给定的发送方和接收方,通过一个动作字符串让系统自动匹配,属于模糊匹配。

一般是跳转到其他APP。

 方式一:跳转到系统应用

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);

        Uri uri = Uri.parse("tel:" + "56324856");
        intent.setData(uri);
        startActivity(intent);

如果Action重名,会跳出弹窗,让用户自行选择。

方式二:跳转到程序员开发的应用

首先在接收方的应用中的AndroidManifest.xml设置好action 和category.

 接着在发送方的java文件中:

        Intent intent = new Intent();
        intent.setAction("android.intent.action.CAL");
        intent.addCategory("android.intent.category.DEFAULT");
        startActivity(intent);

3.发送方向接收方传递数据

这里的发送方和接收方指的是发送方跳转到接收方,接下来也会写到接收方如何向发送方发送数据。

发送方代码:

        Intent intent = new Intent(this, MainActivity6.class);
        intent.putExtra("price",50);
        intent.putExtra("product", "skirt");
        startActivity(intent);

也可以用Bundle这个类打包数据

        Intent intent = new Intent(this, MainActivity6.class);
        Bundle bundle = new Bundle();
        bundle.putInt("price", 50);
        bundle.putString("product", "skirt");
        intent.putExtras(bundle);
        startActivity(intent);

接收方代码:

        TextView tv = findViewById(R.id.tv);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        tv.setText(bundle.getString("product") + ": " + bundle.getInt("price"));

4.接收方向发送方传递数据

发送方使用registerForActivityResult方法来启动页面跳转,与startActivity方法的不同在于,这样启动要求接收方返回数据给发送方。同时重写onActivityResult方法来设置发送方如何响应接收方的回答。

package com.example.ch2;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity5 extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private ActivityResultLauncher<Intent> resultLauncher;

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

        findViewById(R.id.bt).setOnClickListener(this);
        tv = findViewById(R.id.tv);

        resultLauncher = registerForActivityResult(new         
        ActivityResultContracts.StartActivityForResult(), new 
           ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if(result!=null){
                    Intent intent = result.getData();
                    tv.setText(intent.getExtras().getString("response"));
                }
            }
        });

    }

    @Override
    public void onClick(View view) {

        Intent intent = new Intent(this, MainActivity6.class);
        intent.putExtra("price",50);
        intent.putExtra("product", "skirt");
        resultLauncher.launch(intent);
    }
}

接收方收到发送方的数据,且回答发送方。

package com.example.ch2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity6 extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        TextView tv = findViewById(R.id.tv);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        tv.setText(bundle.getString("product") + ": " + bundle.getInt("price"));
        findViewById(R.id.bt).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.putExtra("response", "I want three skirts.");
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值