在 Android 中 Intent 的概念及应用

2024-06-22 13:48
文章标签 android 应用 概念 intent

本文主要是介绍在 Android 中 Intent 的概念及应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自:http://blog.csdn.net/zhandoushi1982/article/details/5455245

 在一个Android应用中,主要是由四种组件组成,包括:activityintentservicecontent provider。这四种组件是独立的,它们之间可以互相调用,协调工作,最终组成一个真正的Android应用。在这些组件之间的通讯中,主要是由Intent协助完成的。

       Intent负责对应用中一次操作的动作、动作涉及数据进行描述Android则根据此Intent的描述,负责找到对应的组件,将Intent传递给调用的组件,并完成组件的调用。      
1)抽象描述要描述什么
      
Android参考文档中,对Intent的定义是执行某操作的一个抽象描述。我们先来看看这里的抽象描述到底描述了什么。首先,是要执行的动作(action)的一个简要描述,如VIEW_ACTION(查看)、EDIT_ACTION(修改)等,Android定义了一套标准动作(部分):
android.intent.action.MAIN_ACTION
android.intent.action.VIEW_ACTION
android.intent.action.EDIT_ACTION
android.intent.action.PICK_ACTION
android.intent.action.GET_CONTENT_ACTION
android.intent.action.DIAL_ACTION
android.intent.action.CALL_ACTION
android.intent.action.SENDTO_ACTION
此外,我们还可以根据应用的需要,定义我们自己的动作,并可定义相应的Activity来处理我们的自定义动作。
       
其次,是执行动作要操作的数据(dataAndroid中采用指向数据的一个URI来表示,例如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。这种URI表示,是通过ContentURI这个类来描述,具体可以参考android.net.ContentURI类的文档。以联系人应用为例,以下是一些action / data对,及其它们要表达的意图:
VIEW_ACTION content://contacts/1 --
显示标识符为"1"的联系人的详细信息
EDIT_ACTION content://contacts/1 --
编辑标识符为"1"的联系人的详细信息
VIEW_ACTION content://contacts/ --
显示所有联系人的列表
        
另外,除了actiondata这两个重要属性外,还有一些附加属性
A
category(类别),被执行动作的附加信息。例如LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现。
B
type(数据类型),显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
C
component(组件),指定Intent的的目标组件的类名称。通常Android会根据Intent 包含的其它属性的信息,比如actiondata/typecategory进行查找,最终找到一个与之匹配的目标组件。但是,如果component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。
D
extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行发送电子邮件这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。
      
总之,actiondata/typecategoryextras 一起形成了一种语言。这种语言使系统能够理解诸如查看某联系人的详细信息之类的短语。
2Android如何解析Intent
       
在应用中,我们可以以两种形式来使用Intent,显式和隐式

       A,直接Intent用类名跳转,使用setClass(Context,Class)Intent传递过程中,要找到目标消费者(另一个Activity,IntentReceiverService),也就是Intent的响应者。

           Activity跳转的段:

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


          Service跳转的段:

  Intent i = new Intent();  i.setClass(this, SmartStayService.class);  startService(i);


        B,间接Intent:没有指定component属性的Intent。这些Intent需要包含足够的信息,这样系统才能根据这些信息,在所有的可用组件中,寻找确定满足此Intent的组件。对于直接IntentAndroid不需要去做解析,因为目标组件已经很明确,Android需要解析的是那些间接Intent,通过解析将Intent映射给可以处理此IntentActivityIntentReceiverService        

        Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent,最终找到匹配的Intent。在这个解析过程中,Android是通过Intentactiontypecategory这三个属性来进行判断的,比如如下一段,前提就是要在XML中申明对应的service class文件           

[java] view plaincopy

1.  startService(new Intent("org.allin.android.facedetcting"));  

3)对于有的应用,可以让它安装完后在APP列表中显示,也可以让它隐藏不显示。方法是:

AndroidManifest.xml<categoryandroid:name="android.intent.category.LAUNCHER" /> 去掉



★intent action大全: 
android.intent.action.ALL_APPS
android.intent.action.ANSWER
android.intent.action.ATTACH_DATA
android.intent.action.BUG_REPORT
android.intent.action.CALL
android.intent.action.CALL_BUTTON
android.intent.action.CHOOSER
android.intent.action.CREATE_LIVE_FOLDER
android.intent.action.CREATE_SHORTCUT
android.intent.action.DELETE
android.intent.action.DIAL
android.intent.action.EDIT
android.intent.action.GET_CONTENT
android.intent.action.INSERT
android.intent.action.INSERT_OR_EDIT
android.intent.action.MAIN
android.intent.action.MEDIA_SEARCH
android.intent.action.PICK
android.intent.action.PICK_ACTIVITY
android.intent.action.RINGTONE_PICKER
android.intent.action.RUN
android.intent.action.SEARCH
android.intent.action.SEARCH_LONG_PRESS
android.intent.action.SEND
android.intent.action.SENDTO
android.intent.action.SET_WALLPAPER
android.intent.action.SYNC
android.intent.action.SYSTEM_TUTORIAL
android.intent.action.VIEW
android.intent.action.VOICE_COMMAND
android.intent.action.WEB_SEARCH
android.net.wifi.PICK_WIFI_NETWORK
android.settings.AIRPLANE_MODE_SETTINGS
android.settings.APN_SETTINGS
android.settings.APPLICATION_DEVELOPMENT_SETTINGS
android.settings.APPLICATION_SETTINGS
android.settings.BLUETOOTH_SETTINGS
android.settings.DATA_ROAMING_SETTINGS
android.settings.DATE_SETTINGS
android.settings.DISPLAY_SETTINGS
android.settings.INPUT_METHOD_SETTINGS
android.settings.INTERNAL_STORAGE_SETTINGS
android.settings.LOCALE_SETTINGS
android.settings.LOCATION_SOURCE_SETTINGS
android.settings.MANAGE_APPLICATIONS_SETTINGS
android.settings.MEMORY_CARD_SETTINGS
android.settings.NETWORK_OPERATOR_SETTINGS
android.settings.QUICK_LAUNCH_SETTINGS
android.settings.SECURITY_SETTINGS
android.settings.SETTINGS
android.settings.SOUND_SETTINGS
android.settings.SYNC_SETTINGS
android.settings.USER_DICTIONARY_SETTINGS
android.settings.WIFI_IP_SETTINGS
android.settings.WIFI_SETTINGS
android.settings.WIRELESS_SETTINGS




几种Intent的用法
android中intent是经常要用到的。不管是页面牵转,还是传递数据,或是调用外部程序,系统功能都要用到intent。在做了一些intent的例子之后,整理了一下intent,希望对大家有用。由于intent内容太多,不可能真的写全,难免会有遗落,以后我会随时更新。如果你们有疑问或新的intent内容,希望交流。

 
★intent大全: 
1.从google搜索内容 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_WEB_SEARCH); 
intent.putExtra(SearchManager.QUERY,"searchString") 
startActivity(intent); 2.浏览网页 
Uri uri = Uri.parse("http://www.google.com"); 
Intent it  = new Intent(Intent.ACTION_VIEW,uri); 
startActivity(it); 3.显示地图 
Uri uri = Uri.parse("geo:38.899533,-77.036476"); 
Intent it = new Intent(Intent.Action_VIEW,uri); 
startActivity(it); 4.路径规划 
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); 
Intent it = new Intent(Intent.ACTION_VIEW,URI); 
startActivity(it); 5.拨打电话 
Uri uri = Uri.parse("tel:xxxxxx"); 
Intent it = new Intent(Intent.ACTION_DIAL, uri);   
startActivity(it); 6.调用发短信的程序 
Intent it = new Intent(Intent.ACTION_VIEW);    
it.putExtra("sms_body", "The SMS text");    
it.setType("vnd.android-dir/mms-sms");    
startActivity(it); 7.发送短信 
Uri uri = Uri.parse("smsto:0800000123");    
Intent it = new Intent(Intent.ACTION_SENDTO, uri);    
it.putExtra("sms_body", "The SMS text");    
startActivity(it); 
String body="this is sms demo"; 
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null)); 
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); 
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true); 
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true); 
startActivity(mmsintent); 8.发送彩信 
Uri uri = Uri.parse("content://media/external/images/media/23");    
Intent it = new Intent(Intent.ACTION_SEND);    
it.putExtra("sms_body", "some text");    
it.putExtra(Intent.EXTRA_STREAM, uri);    
it.setType("image/png");    
startActivity(it); 
StringBuilder sb = new StringBuilder(); 
sb.append("file://"); 
sb.append(fd.getAbsoluteFile()); 
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null)); 
// Below extra datas are all optional. 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject); 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body); 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString()); 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode); 
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent); 
startActivity(intent); 9.发送Email 
Uri uri = Uri.parse("mailto:xxx@abc.com"); 
Intent it = new Intent(Intent.ACTION_SENDTO, uri); 
startActivity(it); 
Intent it = new Intent(Intent.ACTION_SEND);    
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");    
it.putExtra(Intent.EXTRA_TEXT, "The email body text");    
it.setType("text/plain");    
startActivity(Intent.createChooser(it, "Choose Email Client")); 
Intent it=new Intent(Intent.ACTION_SEND);      
String[] tos={"me@abc.com"};      
String[] ccs={"you@abc.com"};      
it.putExtra(Intent.EXTRA_EMAIL, tos);      
it.putExtra(Intent.EXTRA_CC, ccs);      
it.putExtra(Intent.EXTRA_TEXT, "The email body text");      
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");      
it.setType("message/rfc822");      
startActivity(Intent.createChooser(it, "Choose Email Client"));    Intent it = new Intent(Intent.ACTION_SEND);    
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");    
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3");    
sendIntent.setType("audio/mp3");    
startActivity(Intent.createChooser(it, "Choose Email Client")); 10.播放多媒体   
Intent it = new Intent(Intent.ACTION_VIEW); 
Uri uri = Uri.parse("file:///sdcard/song.mp3"); 
it.setDataAndType(uri, "audio/mp3"); 
startActivity(it); 
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");    
Intent it = new Intent(Intent.ACTION_VIEW, uri);    
startActivity(it); 11.uninstall apk 
Uri uri = Uri.fromParts("package", strPackageName, null);    
Intent it = new Intent(Intent.ACTION_DELETE, uri);    
startActivity(it); 12.install apk 
Uri installUri = Uri.fromParts("package", "xxx", null); 
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri); 13. 打开照相机 <1>Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null); this.sendBroadcast(i); <2>long dateTaken = System.currentTimeMillis(); String name = createName(dateTaken) + ".jpg"; fileName = folder + name; ContentValues values = new ContentValues(); values.put(Images.Media.TITLE, fileName); values.put("_data", fileName); values.put(Images.Media.PICASA_ID, fileName); values.put(Images.Media.DISPLAY_NAME, fileName); values.put(Images.Media.DESCRIPTION, fileName); values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName); Uri photoUri = getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(inttPhoto, 10); 14.从gallery选取图片 Intent i = new Intent(); i.setType("image/*"); i.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(i, 11); 15. 打开录音机 Intent mi = new Intent(Media.RECORD_SOUND_ACTION); startActivity(mi); 16.显示应用详细列表       
Uri uri = Uri.parse("market://details?id=app_id");         
Intent it = new Intent(Intent.ACTION_VIEW, uri);         
startActivity(it);         
//where app_id is the application ID, find the ID          
//by clicking on your application on Market home          
//page, and notice the ID from the address bar      刚才找app id未果,结果发现用package name也可以 
Uri uri = Uri.parse("market://details?id=<packagename>"); 
这个简单多了 17寻找应用       
Uri uri = Uri.parse("market://search?q=pname:pkg_name");         
Intent it = new Intent(Intent.ACTION_VIEW, uri);         
startActivity(it); 
//where pkg_name is the full package path for an application       18打开联系人列表 <1>            Intent i = new Intent(); i.setAction(Intent.ACTION_GET_CONTENT); i.setType("vnd.android.cursor.item/phone"); startActivityForResult(i, REQUEST_TEXT); <2> Uri uri = Uri.parse("content://contacts/people"); Intent it = new Intent(Intent.ACTION_PICK, uri); startActivityForResult(it, REQUEST_TEXT); 19 打开另一程序 
Intent i = new Intent(); ComponentName cn = new ComponentName("com.yellowbook.android2", "com.yellowbook.android2.AndroidSearch"); i.setComponent(cn); i.setAction("android.intent.action.MAIN"); startActivityForResult(i, RESULT_OK); 20.调用系统编辑添加联系人(高版本SDK有效):
Intent it = newIntent(Intent.ACTION_INSERT_OR_EDIT);it.setType("vnd.android.cursor.item/contact");//it.setType(Contacts.CONTENT_ITEM_TYPE);it.putExtra("name","myName");it.putExtra(android.provider.Contacts.Intents.Insert.COMPANY,  "organization");it.putExtra(android.provider.Contacts.Intents.Insert.EMAIL,"email");it.putExtra(android.provider.Contacts.Intents.Insert.PHONE,"homePhone");it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE,"mobilePhone");it.putExtra(  android.provider.Contacts.Intents.Insert.TERTIARY_PHONE,"workPhone");it.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE,"title");startActivity(it);21.调用系统编辑添加联系人(全有效):
Intent intent = newIntent(Intent.ACTION_INSERT_OR_EDIT);intent.setType(People.CONTENT_ITEM_TYPE);intent.putExtra(Contacts.Intents.Insert.NAME, "My Name");intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,Contacts.PhonesColumns.TYPE_MOBILE);intent.putExtra(Contacts.Intents.Insert.EMAIL, "com@com.com");intent.putExtra(Contacts.Intents.Insert.EMAIL_TYPE,                    Contacts.ContactMethodsColumns.TYPE_WORK);startActivity(intent);


 


这篇关于在 Android 中 Intent 的概念及应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1084470

相关文章

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

redis-sentinel基础概念及部署流程

《redis-sentinel基础概念及部署流程》RedisSentinel是Redis的高可用解决方案,通过监控主从节点、自动故障转移、通知机制及配置提供,实现集群故障恢复与服务持续可用,核心组件包... 目录一. 引言二. 核心功能三. 核心组件四. 故障转移流程五. 服务部署六. sentinel部署

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

一文带你迅速搞懂路由器/交换机/光猫三者概念区别

《一文带你迅速搞懂路由器/交换机/光猫三者概念区别》讨论网络设备时,常提及路由器、交换机及光猫等词汇,日常生活、工作中,这些设备至关重要,居家上网、企业内部沟通乃至互联网冲浪皆无法脱离其影响力,本文将... 当谈论网络设备时,我们常常会听到路由器、交换机和光猫这几个名词。它们是构建现代网络基础设施的关键组成

从基础到高阶详解Python多态实战应用指南

《从基础到高阶详解Python多态实战应用指南》这篇文章主要从基础到高阶为大家详细介绍Python中多态的相关应用与技巧,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、多态的本质:python的“鸭子类型”哲学二、多态的三大实战场景场景1:数据处理管道——统一处理不同数据格式