Android实验五-组件通信2

news/2025/2/12 7:57:14/

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以及安装成功了。赶快看看模拟器里是不是已经安装上了我们需要的软件了呢。

 

 

 

 

 

 


http://www.ppmy.cn/news/128976.html

相关文章

金刚石切割丝的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告

本文研究全球与中国市场金刚石切割丝的发展现状及未来发展趋势&#xff0c;分别从生产和消费的角度分析金刚石切割丝的主要生产地区、主要消费地区以及主要的生产商。重点分析全球与中国市场的主要厂商产品特点、产品规格、不同规格产品的价格、产量、产值及全球和中国市场主要…

动词的时态(Les temps du verbe )

在开始讲解直陈式现在时的主要用法之前,我们有必要先搞清楚两个基本概念:▶语式(mode):语式是动词表达动作的方式。一个动作,可以作为实在的事表达出来,也可以作为希望或单纯设想的事表达出来,法语动词共有六种语式:直陈式,命令式,条件式,虚拟式,分词式和不定式。…

提高情商 倾听能力

父文章 表达 ,情感描写 沟通 工具书_个人渣记录仅为自己搜索用的博客-CSDN博客 让 chatgpt推荐的 1. 心理学倾听教程 2. 表达 - 比喻 具象 ,有画面感 https://blog.csdn.net/fei33423/article/details/129131137 小说&#xff1a; 1.《呼啸山庄》&#xff08;Wuthering He…

表达 ,情感描写 沟通 工具书

父文章 人人都是xx_个人渣记录仅为自己搜索用的博客-CSDN博客 相关 如何表达,写好文章,技术文章,论文_如何写好技术文章_个人渣记录仅为自己搜索用的博客-CSDN博客 人人都是总结大师_fei33423 人人都是_个人渣记录仅为自己搜索用的博客-CSDN博客 提高情商 倾听能力_个人渣记录…

结合结构特征基于测试集重排序的故障诊断方法

摘要 故障诊断是集成电路领域中的重要研究方向,基于测试激励集方法求解候选故障诊断是目前较为高效的诊断方法,而GTreord是目前具有较高诊断准确性的方法.在对GTreord方法深入研究的基础上,本文依据测试激励与候选故障诊断解之间的结构特征,通过分析电路故障输出响应,提出…

Star-GAN阅读笔记

Star-GAN阅读笔记 SummaryContributionRelated WorkApproach and Model ArchitectureImplementExperimentsCode Summary 之前传统的GAN只能在两个域之间做图像转换(例如Cycle-GAN)&#xff0c;如果要在多个域之间做图像转换&#xff0c;则需要N(N-1)对生成器/鉴别器。然而Star…

【paper and code】StarGAN

文章下载链接&#xff1a;https://arxiv.org/abs/1711.09020 代码链接&#xff1a;https://github.com/yunjey/StarGAN 一、这篇文章讲了什么&#xff1f; Pix2Pix模型解决了有Pair对数据的图像翻译问题&#xff1b;CycleGAN解决了Unpaired数据下的图像翻译问题。 但无论是Pi…

StarGAN论文

目录一览 摘要1.介绍术语解释数据集一对一图像转换的不足针对以上不足的StarGAN优势论文的贡献 2.相关基础知识生成式对抗网络GAN条件生成式对抗网络CGAN图像翻译 3.StarGAN3.1多域单数据集的图像翻译对抗损失(adv)域分类损失(cls)重建损失(rec)完整的损失 3.2多域多数据集的图…