5.2、Intent对象

Intent是一种轻量级消息传递机制,旨在解决各项组件之间的通信问题。它描述了应用中一次操作的动作、动作涉及数据、附加数据,Android则根据此描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成该组件的调用。Intent启动Activity分为显示启动和隐式启动两种:

1.显示启动

1)创建Intent对象,并初始化指明要启动的Activity

方法一: Intent  intent=new  Intent(A.this,B.class);

方法二: ComponentName component=new ComponentName(A.this,B.class);

Intent  intent=new  Intent();

Intent.setComponent(component);

2)调用startActivity(intent)完成启动Activity的启动。

2.隐式启动

所谓隐式启动,表示不需指明要启动哪一个Activity,由系统来决定。隐式启动Activity时,系统会自动匹配。

1)创建Intent对象

Intent  intent=new  Intent();

2)设置用于匹配的Intent属性

Intent.setAction();  //设置动作属性

Intent.addCategory();  //设置类别属性

Intent.setData();  //设置Date属性

Intent.setType();  / /设置对应的mimiType属性

3)调用startActivity(intent)完成启动Activity的启动。

3.部署文件AndroidManifest.xml文件中的Intent Filter

Android系统的这种匹配机制是依靠过滤器Filter来实现。与Intent属性保持一致,Intent Filter包含动作、类别、数据等过滤内容。

对应Intent的动作、类别、数据三个属性,一个过滤器也有<action>,<catrgory>,<data>三个节点。

 (1)Action属性:通过Intent自定义动作字符串来隐式启动某个Activity,该字符串必须唯一。一个Intent对象最多只能设置一个Action属性,Action要完成的动作可以自定义,也可以指定系统提供的Action。例如:

String COM_SUDA=”com.mialab.demo.SUDA_ACTION”;
Intent intent=new Intent();
Intent.setAction(COM_SUDA);

Action常量

Action常量

对应字符串

描述

ACTION_MAIN

Android.intent.action.MAIN

应用程序的入口

ACTION_VIEW

Android.intent.action.VIEW

显示指定数据

ACTION_EDIT

Android.intent.action. EDIT

编辑指定数据

ACTION_GET_CONTENT

Android.intent.action. GET_CONTENT

用户选择数据,并返回所选数据

ACTION_DIAT

Android.intent.action.DIAT

显示拨号面板

ACTION_CALL

Android.intent.action. CALL

直接向指定用户拨打电话

ACTION_SEND

Android.intent.action.SEND

向其他人发送数据

ACTION_SEND_TO

Android.intent.action.SEND_TO

向其他人发送信息

ACTION_SEND_ANSWER

Android.intent.action. ANSWER

应答电话

(2)Category属性:被执行动作的附加属性,一个Intent对象可以设置多个Category属性,通过调用addCategory()方法来添加Category属性。

(3)Data属性:用来向Action提供操作的数据,例如:

 Intent intent=new Intent();intent.setData(Uri.parse(“content://com.mialav.demo/data”));intent.setType(“abc/xyz”);

后面的属性setType属性会覆盖前面的setData属性。如果希望拥有两个属性,可以调用 setDataAndType()方法,如:

Intent intent=new Intent();
intent. setDataAndType(Uri.parse(“content://com.mialav.demo/data”, “abc/xyz”); 

举例:发送并返回短信内容

本示例主要实现输入电话号码,点击“发送消息”,将启动手机中的短信应用程序。当发送完短信退出后,界面将显示发送的内容和时间。

MainActivity.java中的代码如下:

package com.example.intentdemo3;import java.text.SimpleDateFormat;
import java.util.Date;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {final int PICK_CODE=0; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn=(Button)findViewById(R.id.btnsend);final EditText txttel=(EditText)findViewById(R.id.txttel);      btn.setOnClickListener(new OnClickListener() {          @Overridepublic void onClick(View arg0) {Intent intent=new Intent();intent.setAction(Intent.ACTION_SENDTO);String phone=txttel.getText().toString();intent.setData(Uri.parse("smsto:"+phone));startActivityForResult(intent, PICK_CODE);              }});}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data){super.onActivityResult(requestCode, resultCode, data);final EditText txttel=(EditText)findViewById(R.id.txttel);final EditText txtmsg=(EditText)findViewById(R.id.txtmsg);  final EditText txtdate=(EditText)findViewById(R.id.txtdate);   if(requestCode==PICK_CODE&&resultCode==Activity.RESULT_CANCELED){Uri uri = Uri.parse("content://sms/sent");  String[] projection = new String[] { "_id", "address", "person", "body","date", "type" };Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc");StringBuilder smsmsg = new StringBuilder();  if(cur.moveToFirst()) {  int idx_addr = cur.getColumnIndex("address");  int idx_body = cur.getColumnIndex("body");  int idx_date = cur.getColumnIndex("date");    do {String strAddress = cur.getString(idx_addr);  if(strAddress.equals(txttel.getText().toString())){ String strbody = cur.getString(idx_body); long date = cur.getLong(idx_date);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");              Date d = new Date(date);  txtmsg.setText(strbody);txtdate.setText(format.format(d));break;}    } while (cur.moveToNext());     if (!cur.isClosed()) {  cur.close();  cur = null;  }  } }}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}
}

ActivityMain.xml中的代码如下:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="5dip" ><TableRowandroid:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="SentTo:" /><EditTextandroid:id="@+id/txttel"android:layout_width="match_parent"android:layout_height="wrap_content"android:ems="10"android:inputType="phone" /></TableRow><Buttonandroid:id="@+id/btnsend"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送消息" /><TableRowandroid:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Message:" /><EditTextandroid:id="@+id/txtmsg"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="" /></TableRow><TableRowandroid:layout_width="wrap_content"android:layout_height="wrap_content" ><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Date:" /><EditTextandroid:id="@+id/txtdate"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="" /></TableRow></TableLayout>

3、部署文件AndroidManifest.xml文件中的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.mialab.sendmsg"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="14"android:targetSdkVersion="19" /><uses-permission android:name="android.permission.READ_SMS"></uses-permission>  <applicationandroid:allowBackup="true"android:icon="@drawable/icon"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.mialab.sendmsg.MainActivity"android:label="@string/app_name" >            <intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

使用命令在android模拟器上面安装apk文件

  1. 启动模拟器
  2. 将你要安装的apk文件复制到E:\AndroidSDK\adt-bundle-windows-x86-20131030\sdk\platform-tools文件夹下,我的是在E盘,你的可以根据platform-tools文件的路径来看。
  3. 点击开始-》运行-》输入cmd
  4. 然后在控台中找到sdk 文件中platform-tools的路径,我的是在E:盘,所有直接输入E:就进入E:盘目录了,如果你的实在D:盘,你就直接输入D:回撤就行了
  5. 之后才cd E:\Android SDK\adt-bundle-windows-x86-20131030\sdk\platform-tools进入platform-tools文件的目录下
  6. 最后在控台中输入adb install AIDLService.apk,我这里安装的是AIDLService.apk所以我输入的是adb install AIDLService.apk,然后回车,你就看到下面信息就表示apk以及安装成功了。赶快看看模拟器里是不是已经安装上了我们需要的软件了呢。

 

Android实验五-组件通信2相关推荐

  1. 实现Android跨进程组件通信能有多简单?

    实现Android跨进程组件通信能有多简单? 作为一个Android开发,都要会点组件化知识.组件化的主要的特点,是剥离依赖,但组件间不直接依赖后,通信问题要怎么解决呢. 通常我们用的一下这种类似Bi ...

  2. Android深入四大组件(五)Android8.0 根Activity启动过程(后篇)

    前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程(前篇)和Android深入四大组件(一)应用程序启动过程(后篇)这两篇文章,它们都是基于Android 7.0,当我开始阅读An ...

  3. Android 四大组件通信核心

    前言 系列文章: Android Activity创建到View的显示过程 Android 四大组件通信核心 Android 系统启动到App 界面完全展示终于明白(图文版) 我们知道Android ...

  4. Vue组件通信的五种方式

    Vue组件通信的五种方式 文章目录 Vue组件通信的五种方式 一. props/$emit(父子通信) 二. vuex(组件之间通信) 三. 事件总线EventBus(组件之间通信) 四. provi ...

  5. Android组件化之组件通信

    Demo地址:https://github.com/751496032/ComponentDemo 本文是续上一篇Android组件化方案实践与思考文章一些思考,主要是针对组件间通信,比如: 每个组件 ...

  6. android的应用组件,跟我学android-Android应用基本组件介绍(五)

    Activity activity 是最基本的模块,我们成为活动,一个activity通常就是一个单独的屏幕,每一个活动都被实现为一个独立的类,且都继承活动的基类.在activity的实现类里显示用户 ...

  7. 【安卓实验】实验五、广播实验

    [实验名称]实验五.广播实验 [实验目的] 1.了解使用Intent进行组件通信的原理: 2.了解Intent过滤器的原理和匹配机制: 3.掌握发送和接收广播的方法 [实验内容] 任务1.普通广播: ...

  8. 深入Android 【五】 —— 任务和进程

    任务.进程和线程 关于Android中的组件和应用,之前涉及,大都是静态的概念.而当一个应用运行起来,就难免会需要关心进程.线程这样的概念.在Android中,组件的动态运行,有一个最与众不同的概念, ...

  9. android实现箭头流程列表_反思|Android 列表分页组件Paging的设计与实现:系统概述...

    作者:却把清梅嗅 链接:https://github.com/qingmei2/blogs/issues/30 前言 本文将对Paging分页组件的设计和实现进行一个系统整体的概述,强烈建议 读者将本 ...

  10. Android开发之 Android 的基本组件的概述

    Android是一个为组件化而搭建的平台,它的应用是由一些零散的有联系的组件组成,并通过AndroidManifest.xml文件 把它们绑定起来. Android常用的组件有: Activity(活 ...

最新文章

  1. 2021-10-27 我与地坛
  2. 【Android】开发优化之——调优工具:TrackView,Method Profiling
  3. linux weblogic启动目录,Linux下WebLogic开机启动设置
  4. HDU 5730 Shell Necklace(生成函数 多项式求逆)
  5. 儿童python编程能给孩子带来哪些好处_python编程入门学习对孩子成长有哪些优势?...
  6. caffe安装_目标检测之caffe-ssd模型训练与测试
  7. Wireshark中遇到的epoch time
  8. 基于RV1126平台imx291分析 --- media部件连接 四
  9. UI控件Telerik UI for Silverlight发布R2 2019|附下载
  10. 字母数字下划线常用正则表达式
  11. 你还在为找素材发愁吗?自媒体高手都知道的免费自媒体素材网
  12. 2021中国科学院文献情报中心期刊分区表 计算机
  13. 微型计算机ccc认证的流程,计算机的3C认证办理以及流程
  14. 轻松学会分布式事务算法
  15. xy苹果助手未受信任_经过苹果企业签名的应用该如何安装
  16. Latex 表格的模版笔记
  17. Flyme 6将于30日公测 魅蓝Note5有望率先尝鲜
  18. easyexcel复杂表格---包含单元格合并,表格标题,以及自定义字段写入
  19. Eureka常见问题解答
  20. 2022年中国数字藏品行业研究报告 附下载

热门文章

  1. 支付宝崩了登上微博热搜
  2. express基本使用步骤
  3. CF 285D 285E
  4. 痛与教训,我所亲历的3个失败游戏创业公司
  5. git 如何下载单个文件夹或者单个文件
  6. 笔记本电脑突然搜索不到无线网信号怎么办?
  7. 强烈推荐收藏!3W 字Python 操作 Excel 报表自动化指南
  8. 永中office linux卸载,永中office2012forLinux的安装卸载
  9. 用友NC V6.5 6.33 6.31 6.3 6.1 2019新个人所得税增强包税改升级补丁包
  10. 求推荐一款移动硬盘,日立和西数哪个好?