Android Intent通信详细讲解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Android Intent通信详细讲解

上后左爱   2022-12-17 我要评论

Intent

将Activity 、Serivce 、 BroadReceive 之间通信 使用Intent

Intent 对象属性:

  • 1.Action
  • 2. Data
  • 3.Category
  • 4. Extras
  • 5.Flags
  • 6.Component name

Component name:

设置Intnet 组件对象 名称 通过 包名 + 类名 确定唯一的一个activity

类似Spring 中Component 根据component name 设置或者启动特定的组件

setComponent(componentName)

Intnet intnet = new Intnet();
ComponentName componentName = new ComponentName("com.example","com.example.DetailActivity");
intnet.setComponent(componentName);
startActivity(intnet)

Action & Data

接下来动作 和 对应数据

Action 属性和Data 属性

第一步在 manifest 设置开启电话和邮件的权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <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=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--  自定义Activity 需要在mainifests 配置声明       -->
    </application>
</manifest>
package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
/**
 *  功能: Action Data 共用 点击打电话 和 发送邮件按钮 进行 跳转系统 打电话 和邮件界面
 *
 *
 * */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageButton phoneButton =  findViewById(R.id.imageButton_phone);
        ImageButton smsButton = findViewById(R.id.imageButton_sms);
        phoneButton.setOnClickListener(l);
        smsButton.setOnClickListener(l);
    }
    // 定义监听器对象 方法共用
    View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            ImageButton imageButton = (ImageButton) view;
            switch (imageButton.getId()){
                case R.id.imageButton_phone:
                 // 调用拨号面板
                    intent.setAction(intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel: 043184978981"));
                    startActivity(intent);
                    break;
                case R.id.imageButton_sms:
                    intent.setAction(intent.ACTION_SENDTO);
                    intent.setData(Uri.parse("smsto: 5554"));
                    // 设置短信内容
                    intent.putExtra("sms_body", "welcome to Android");
                    startActivity(intent);
                    break;
            }
        }
    };
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="技术支持:吉林省明日科技有限公司"
        android:layout_marginTop="20dp"/>
<TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="网址:http://www.mingrisoft.com"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/text1"/>
    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="企业邮箱:mingrisoft@mingrisoft.com"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/text2"/>
    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="技术服务热线:0431-84978981"
        android:layout_marginTop="10dp"
        android:layout_below="@+id/text3"/>
    <ImageButton
        android:id="@+id/imageButton_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/phone"
        android:layout_below="@+id/text4"
        android:layout_marginTop="30dp"
        android:background="#0000"
        android:scaleType="fitXY"
        />
    <ImageButton
        android:id="@+id/imageButton_sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageButton_phone"
        android:layout_below="@+id/text4"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:background="#0000"
        android:scaleType="fitXY"
        android:src="@drawable/sms"/>
</RelativeLayout>

Action & Category

Category属性 将Activity 进行分类, 将对应Activity 进行分类

package com.example.myapplication;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import androidx.appcompat.app.AppCompatActivity;
/**
 *  功能: Action Data 共用 点击打电话 和 发送邮件按钮 进行 跳转系统 打电话 和邮件界面
 *
 *
 * */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ImageButton imageButton= (ImageButton) findViewById(R.id.imageButton1); //获取ImageView组件
        //为ImageView组件设置单击事件监听器
        imageButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(intent.ACTION_MAIN);
                intent.addCategory(intent.CATEGORY_HOME);
            }
        });
    }
}

Flags 属性

作用: 让当前的activity 不在历史栈当中保留,当用户离开app 再一次进入时候 不在是当前Activity 界面 转到系统的主界面

Intnet 种类

显示Intnet:

创建Intnet对象 -> 目标组件 ->启动 目标组件:

Intnet intnet = new Intnet(MainActivity.this, 调用Activity对象)

隐式Intnet创建对象不指定 目标组件,通过action , category , data 让Android 系统自动匹配目标组件

区别: 显示 直接指定目标组件名称,多常用在应用程序内部传递信息

隐式Intnet:不会用组件名称定义激活的目标组件,多常用在不同应用程序之间传递信息

Intent Filter

Activity A 通过category + action + data 作为Filter的条件刷选出来的ActivityB 是目标Activity

在AndroidManifest.xml 文件中配置

 <intent-filter>
 	<category></category>
 	<action></action>
 </intnet-filter>
 <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

打开图片时候 采用不同app画图 或者 点击

通过隐式Intnet 完成

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们