1、Intent组件在Android中是一个十分重要的组件,它是连接不同应用程序的桥梁和纽带,也是让组件级复用(Activity和Service)成为可能的一个重要因素。Intent组件的主要作用是运行在相同或不同应用程序的Activity、Service、Broadcasereciver间进行切换和数据的传送。Intent的功能有很多,例如:打开网页、打电话、发短信等。
2、Intent的构成
Intent数据结构两个最重要的部分是动作和动作对应的数据,典型的动作类型有:MAIN、VIEW、DAIL、EDIT等,二动作对应的数据一般以URI的形式进行表示。
1、Intent的动作(Action)
在Intent中,Action就是希望触发的动作。当你指明了一个Action,执行者就会依照这个动作的指示接受响应的输入,表现对应行为,产生响应的输出。可以通过setAction()方法进行设置Action,通过getAction()方法读取。
2、Intent动作对应的数据
一共分为以下六种数据:数据(Data)、数据类型(Type)、操作类别(Category)、附加信息(Extras)、组件(Component)、标志(Flags)。
数据:指作用与动作的数据的URI和数据的MIME类型。不同的动作有不同的数据规格。例如如果动作是ACTION_VIEW,数据字段是一个http:URI,接受活动将被调用去下载和显示URI指向的数据,不同的动作对应不同的Data。
数据类型:指定要传送的数据的MIME类型,可以直接通过setType()方法进行设置,getType()方法读取类型。一般Intent的数据类型能够根据数据本身进行判定,但是通过setType()方法设置属性,可以强制采用显示指定的类型而不必进行推导。
操作类别:有时通过Action,配合Data或Type,很多时候可以准确地表达出一个完整的意图,但也会知道一些约束在里面才能够更精准。一个实例是:所有的应用主Activity都需要一个Category为CATEGORY_LAUNCHER,Action为ACTION_MAIN的Intent。
附加信息:传递的是一组键值对,可以使用putExtra()方法进行设置,主要的功能是传递数据(Uri)所需要的一些额外的操作信息。使用extras可以为组件提供扩展信息。
组件:指明了将要处理的Activity程序,通常Android会根据Intent中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是如果component这个属性有指定的话,将直接使用它指定的组件。所有的组件信息都被封装在一个ComponentName对象中,这些组件必须在AndroidManifest.xml文件中“<application>”中注册。
标志:Flags是一个整形数,由一系列的标志位构成,这些标志用来指明运行模式的,共有20种。这些标志都定义在Intent类中,可以通过addFlags()方法进行增加。
3、使用实例
从google搜索内容:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "searchString");
startActivity(intent);
浏览网页
Uri uri = Uri.parse("http://www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
显示地图
Uri uri = Uri.parse("geo:33.333333, -44.444444");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
路径规划
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);
拨打电话
Uri uri = Uri.parse("tel:03165659631");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
调用发短信的程序
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("sms_body", "text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
发送短信
Uri uri = Uri.parse("smsto:12345678999");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "text");
startActivity(it);
String body = "This is body";
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(intent);
发送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "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("smsto", number, null));
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);
播放多媒体
方式一:Intent it = new Intent(Intent.ACTION)VIEW);
Uri uri = Uri.parse("file:///sdcard/demo.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);
卸载apk
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
安装apk
Uri uri = Uri.fromParts("package", "xxx", null);
return new Intent(Intent.ACTION_PACKAGE_ADDED, uri);
打开照相机
方式一:Intent it = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
sendBrodcast(it);
方式二:long dataTaken = System.currentTimeMillis();
String name = createName(dataTaken) + ".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 uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
it.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(it, 10);
从gallery选取图片
Intent it = new Intent();
it.setType("image/*");
it.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(it, 11);
打开录音机
Intent it = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(it);
显示应用详细列表
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
发送email
方式一:Uri uri = Uri.parse("mailto:[email protected]");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
方式二:Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
it.putExtra(Intent.EXTRA_TEXT, "The body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose email client"));
方式三:Intent it = new Intent(Intent.ACTION_SEND);
String[] tos = {"[email protected]"};
String[] ccs = {"[email protected]"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "body text");
it.putExtra(Intent.EXTRA_SUBJECT, "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, "subject text');
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/demo.mp3");
it.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose enail client"));
寻找应用
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
打开联系人列表
方式一:Intent it = new Intent();
it.setAction(Intent.ACTION_GET_CONTENT);
it.setType("vnd.android.cursor.item/phone");
startActivityForResult(it, REQUEST_TEXT);
方式二:Uri uri = Uri.parse("content://contacts/people");
Intent it = new Intent(Intent.ACTION_PICK, uri);
startActivity(it, REQUEST_TEXT);
打开另一程序
Intent it = new Intent();
ComponentName cn = new ComponentBane("com.yellowbook.android2", "com.yellowbock.android2.AndroidSearch");
it.setComponent(cn);
it.setAction("android.intent.action.MAIN");
startActivityForResult(it, RESULT_OK);
调用系统编辑器添加联系人
Intent it = new Intent(Intent.ACTION_INSERT_OR_EDIT);
it.setType("vnd.android.cursor.item/contact:);
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);
深入了解Intent
猜你喜欢
转载自xiaofuyan.iteye.com/blog/2351489
今日推荐
周排行