Android Intent基本知识

**Intent 作用:**信息传递
Bundle比喻为包裹,Intent就是快递员

intent如何传递数据?

构造Intent对象
调用startActivity()
将信息传入Intent

系统根据Intent描述,找到满足要求的Activity

目标
根据Intent中的描述,执行相应的动作

Intent的基本应用
Activity 的开启
Service 的开启(如下载)
用来传递广播

Intent对象的属性
1、Component name
用来设置Intent对象的组件名称,通过名称可以启动Activity或其它应用中的Activity
可以通过指定包名或类名来指定唯一的Activity,程序就可以根据指定的内容来启动特定的组件
通过setComponent(ComponentName)方法来设置,ComponentName 的参数1为应用的包名,参数2 类名

通过Intent内容打开Activity

准备内容
1、创建新activity的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="详细内容"/>

</RelativeLayout>

2、创建新布局文件的类

package com.example.intent1;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class DetailActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
    }
}

3、在manifest中添加类
在这里插入图片描述

4、在mainActivity中使用Intent,打开新的Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bnt = (Button) findViewById(R.id.bnt1);
        bnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName("com.example.intent1","com.example.intent1.DetailActivity");//参数1 应用的包名
                intent.setComponent(componentName);
                startActivity(intent);//启动activity并为其传递intent
            }
        });
    }
}

2、Component Action与Data
Action与Data需要联合使用

Action可以通过Intent 提供的Action常量来确定
Data 与Action是内容有关

实现打电话及发短信的action
准备工作:

<?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">
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/img2"/>

    <ImageButton
        android:id="@+id/bnt1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/img1"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/img"
        android:background="#0000"
        android:scaleType="fitXY" />
    <ImageButton
        android:id="@+id/bnt2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/img1"
        android:layout_marginTop="30dp"
        android:layout_toRightOf="@+id/bnt1"
        android:layout_below="@+id/img"
        android:background="#0000"
        android:scaleType="fitXY" />
</RelativeLayout>

在MainActivity中使用Action,执行打电话及发短信的action

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton imgbnt1 = (ImageButton) findViewById(R.id.bnt1);
        ImageButton imgbnt2 = (ImageButton) findViewById(R.id.bnt2);

        imgbnt1.setOnClickListener(l);
        imgbnt2.setOnClickListener(l);

    }
    //创建单击事件监听器对象
    View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            ImageButton imageButton = (ImageButton) v;

           switch (imageButton.getId()){
               case R.id.bnt1:
                   intent.setAction(intent.ACTION_DIAL);//设置action,打电话
                   intent.setData(Uri.parse("tel:111"));//action的data,拨打号码111
                   startActivity(intent);//启动Activity、
                   break;
               case R.id.bnt2:
                   intent.setAction(intent.ACTION_SENDTO);//设置action,发短信
                   intent.setData(Uri.parse("smsto:111"));//action的data,给111发送短信
                   intent.putExtra("sms_body","Hello World!");//设置发送短信的默认内容
                   startActivity(intent);//启动Activity
                   break;
                   default:break;

           }
        }
    };

最后开启打电话及发短信的权限

在这里插入图片描述

3、Action与Category
category用于对执行动作的类别进行描述,通过Intent提供的常量来设置

准备工作

<?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">
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/img2"/>

    <ImageButton
        android:id="@+id/bnt1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/img1"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/img"
        android:background="#0000"
        android:scaleType="fitXY" />
</RelativeLayout>

在MainActivity文件中使用Action Category,实现单击按钮,回到主页面

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton imgbnt1 = (ImageButton) findViewById(R.id.bnt1);

        imgbnt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                //startActivity(intent);
                Intent intent = new Intent();
                intent.setAction(intent.ACTION_MAIN);//设置Action属性
                intent.addCategory(intent.CATEGORY_HOME);//设置category属性,返回桌面
                startActivity(intent);
            }
        });
    }
}

4、Extras

主要用于向Intent中添加附加信息,通常这些信息以键值对来保存
应用在多个Activity进行数据交换时
存储信息 putExtras()
获取信息 getExtras()

5、Flags
程序栈的操作
指示如何去启动另一个activity
指示程序启动后,如何处理
可以用Intent提供的常量来设置

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button) findViewById(R.id.bnt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY);//让程序不在历史栈中保留,用户一旦离开,activity自动关闭
                startActivity(intent);
            }
        });
    }
}

6 Intent种类
1、显式
应用程序内部传递信息
创建Intent对象时,直接指定目标组件的名称,然后启动组件,明确知道activity名称时使用

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

2、隐式
不同应用程序间传递信息
不指定要启动的activity名称,而是指定action/category/data,让Android根据设定好的规则,自动匹配目标组件

准备工作:
在MainActivity中添加按钮

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button) findViewById(R.id.bnt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(intent.ACTION_VIEW);//启动网页,通过指定action和category来让android自动匹配要启动的activity
                intent.setData(Uri.parse("http://www.baidu.com"));
                intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY);//让程序不在历史栈中保留,用户一旦离开,activity自动关闭
                startActivity(intent);
            }
        });
    }
}

7 Intent过滤器

根据某个intent中的action/data/category等,对适合接收的组件进行筛选。

过滤器的设置
通过标记在AndroidManifest.xml文件中配置

<action …>指定action所能响应的动作
<category …>指定以哪种方式来执行intent请求
<data …>

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button) findViewById(R.id.bnt1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(intent.ACTION_VIEW);//将数据显示给用户,在AndroidManifest.xml中设置过滤器
                startActivity(intent);
            }
        });
    }
}

AndroidManifest.xml

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

    <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=".DetailActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值