【Android】 Intent详解

简介:

一、 Intent 作用

   Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯。比如说调用startActivity()来启动一个activity,或者由broadcaseIntent()来传递给所有感兴趣的BroadcaseReceiver, 再或者由startService()/bindservice()来启动一个后台的service.所以可以看出来,intent主要是用来启动其他的activity 或者service,所以可以将intent理解成activity之间的粘合剂。

 

二、 、Intent的构成

 

      要在不同的activity之间传递数据,就要在intent中包含相应的东西,一般来说数据中最基本的应该包括:

         Action 用来指明要实施的动作是什么,比如说ACTION_VIEW, ACTION_EDIT等。具体的可以查阅android SDK-> reference中的Android.content.intent类,里面的constants中定义了所有的action。

          Data 要事实的具体的数据,一般由一个Uri变量来表示 ,下面是一些简单的例子:

          ACTION_VIEW content://contacts/1 //显示identifier为1的联系人的信息。

          ACTION_DIAL  content://contacts/1 //给这个联系人打电话 

除了Action和data这两个最基本的元素外,intent还包括一些其他的元素,

          Category(类别): 这个选项指定了将要执行的这个action的其他一些额外的信息,例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。具体同样可以参考android SDK-> reference中的Android.content.intent类。

          Type(数据类型):显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。

          component(组件):指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

          extras(附加信息),是其它所有附加信息的集合。使用extras可以为组件提供扩展信息,比如,如果要执行“发送电子邮件”这个动作,可以将电子邮件的标题、正文等保存在extras里,传给电子邮件发送组件。

下面是这些额外属性的几个例子:

          ACTION_MAIN with category CATEGORY_HOME //用来 Launch home screen.

          ACTION_GET_CONTENT with MIME type vnd.android.cursor.item/phone //用来列出列表中的所有人的电话号码

综上可以看出,action、 data/type、category和extras 一起形成了一种语言,这种语言可以是android可以表达出诸如“给张三打电话”之类的短语组合

 

三、 intent的解析

应用程序的组件为了告诉Android自己能响应、处理哪些隐式Intent请求,可以声明一个甚至多个Intent Filter。每个Intent Filter描述该组件所能响应Intent请求的能力——组件希望接收什么类型的请求行为,什么类型的请求数据。比如之前请求网页浏览器这个例子中,网页浏览器程序的Intent Filter就应该声明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及与之相关的请求数据是网页地址URI格式。如何为组件声明自己的Intent Filter? 常见的方法是在AndroidManifest.xml文件中用属性< Intent-Filter>描述组件的Intent Filter。

前面我们提到,隐式Intent(Explicit Intents)和Intent Filter(Implicit Intents)进行比较时的三要素是Intent的动作、数据以及类别。实际上,一个隐式Intent请求要能够传递给目标组件,必要通过这三个方面的检查。如果任何一方面不匹配,Android都不会将该隐式Intent传递给目标组件。接下来我们讲解这三方面检查的具体规则。

动作测试

  Java代码

 < intent-filter>元素中可以包括子元素< action>,比如:

      < intent-filter> < action android:name=”com.example.project.SHOW_CURRENT” />

 < action android:name=”com.example.project.SHOW_RECENT” />

< action android:name=”com.example.project.SHOW_PENDING” />

 < /intent-filter> 

 一条< intent-filter>元素至少应该包含一个< action>,否则任何Intent请求都不能和该< intent-filter>匹配。如果Intent请求的Action和< intent-filter>中个某一条< action>匹配,那么该Intent就通过了这条< intent-filter>的动作测试。如果Intent请求或< intent-filter>中没有说明具体的Action类型,那么会出现下面两种情况。 

(1) 如果< intent-filter>中没有包含任何Action类型,那么无论什么Intent请求都无法和这条< intent- filter>匹配;

(2) 反之,如果Intent请求中没有设定Action类型,那么只要< intent-filter>中包含有Action类型,这个 Intent请求就将顺利地通过< intent-filter>的行为测试。 

类别测试

  Java代码

 < intent-filter>元素可以包含< category>子元素,比如:

      < intent-filter . . . >

      < category android:name=”android.Intent.Category.DEFAULT” />

      < category android:name=”android.Intent.Category.BROWSABLE” />

< /intent-filter> 

  只有当Intent请求中所有的Category与组件中某一个IntentFilter的< category>完全匹配时,才会让该 Intent请求通过测试,IntentFilter中多余的< category>声明并不会导致匹配失败。一个没有指定任何类别测试的 IntentFilter仅仅只会匹配没有设置类别的Intent请求。 

数据测试

  数据在< intent-filter>中的描述如下: Java代码 

< intent-filter . . . >

      < data android:type=”video/mpeg” android:scheme=”http” . . . />

      < data android:type=”audio/mpeg” android:scheme=”http” . . . />

< /intent-filter> 

  元素指定了希望接受的Intent请求的数据URI和数据类型,URI被分成三部分来进行匹配:scheme、 authority和path。其中,用setData()设定的Inteat请求的URI数据类型和scheme必须与IntentFilter中所指定的一致。若IntentFilter中还指定了authority或path,它们也需要相匹配才会通过测试。

简单例子说明

  讲解完Intent基本概念之后,接下来我们就使用Intent激活Android自带的电话拨号程序,通过这个实例你会发现,使用Intent并不像其概念描述得那样难。最终创建Intent的代码如下所示。

  Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));

  创建好Intent之后,你就可以通过它告诉Android希望启动新的Activity了。

  startActivity(i);

  Activity启动后显示界面如下:

  

 

四、Intent的构造函数

公共构造函数:

1、Intent() 空构造函数

2、Intent(Intent o) 拷贝构造函数

3、Intent(String action) 指定action类型的构造函数

4、Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

5、Intent(Context packageContext, Class<?> cls) 传入组件的构造函数,也就是上文提到的

6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

 

Intent有六种构造函数,3、4、5是最常用的,并不是其他没用!

Intent(String action, Uri uri)  的action就是对应在AndroidMainfest.xml中的action节点的name属性值。在Intent类中定义了很多的Action和Category常量。

 

示例代码二:

   1: Intent intent = new Intent(Intent.ACTION_EDIT, null);

   2: startActivity(intent);

示例代码二是用了第四种构造函数,只是uri参数为null。执行此代码的时候,系统就会在程序主配置文件AndroidMainfest.xml中寻找<action android:name="android.intent.action.EDIT" />对应的Activity,如果对应为多个activity具有<action android:name="android.intent.action.EDIT" />此时就会弹出一个dailog选择Activity,如下图:

 

 如果是用示例代码一那种方式进行发送则不会有这种情况。

 

五、利用Intent在Activity之间传递数据

在Main中执行如下代码:

   1: Bundle bundle = new Bundle();

   2: bundle.putStringArray("NAMEARR", nameArr);

   3: Intent intent = new Intent(Main.this, CountList.class);

   4: intent.putExtras(bundle);

   5: startActivity(intent);

 

在CountList中,代码如下:

   1: Bundle bundle = this.getIntent().getExtras();

   2: String[] arrName = bundle.getStringArray("NAMEARR");

以上代码就实现了Activity之间的数据传递!

Android intent intent-filter

六、总结说明

这篇文章是我刚开始学习Android时看到的,当时理解的不是很深入,现在再回头看这篇文章总结的很详细,在这里与大家分享。

  1,掉web浏览器

  Java代码   

      Uri myBlogUri = Uri.parse("http://kuikui.javaeye.com");

  returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);

  Java代码

  Uri mapUri = Uri.parse("geo:38.899533,-77.036476");

  returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

  3,调拨打电话界面

  Java代码

  Uri telUri = Uri.parse("tel:100861");

  returnIt = new Intent(Intent.ACTION_DIAL, telUri);

  4,直接拨打电话

  Java代码

  Uri callUri = Uri.parse("tel:100861");

  returnIt = new Intent(Intent.ACTION_CALL, callUri);

  5,卸载

  Java代码

  Uri uninstallUri = Uri.fromParts("package", "xxx", null);

  returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

  6,安装

  Java代码

  Uri installUri = Uri.fromParts("package", "xxx", null);

  returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

  7,播放

  Java代码

  Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");

  returnIt = new Intent(Intent.ACTION_VIEW, playUri);

  8,掉用发邮件

  Java代码

  Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");

  returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);

  9,发邮件

  Java代码

  returnIt = new Intent(Intent.ACTION_SEND);

  String[] tos = { "shenrenkui@gmail.com" };

  String[] ccs = { "shenrenkui@gmail.com" };

  returnIt.putExtra(Intent.EXTRA_EMAIL, tos);

  returnIt.putExtra(Intent.EXTRA_CC, ccs);

  returnIt.putExtra(Intent.EXTRA_TEXT, "body");

  returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");

  returnIt.setType("message/rfc882");

  Intent.createChooser(returnIt, "Choose Email Client");

  10,发短信 Java代码

  Uri smsUri = Uri.parse("tel:100861");

  returnIt = new Intent(Intent.ACTION_VIEW, smsUri);

  returnIt.putExtra("sms_body", "shenrenkui");

  returnIt.setType("vnd.android-dir/mms-sms");

  11,直接发邮件

  Java代码

  Uri smsToUri = Uri.parse("smsto://100861");

  returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);

  returnIt.putExtra("sms_body", "shenrenkui");

  12,发彩信

  Java代码

  Uri mmsUri = Uri.parse("content://media/external/images/media/23");

  returnIt = new Intent(Intent.ACTION_SEND);

  returnIt.putExtra("sms_body", "shenrenkui");

  returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);

  returnIt.setType("image/png");

  用获取到的Intent直接调用startActivity(returnIt)就ok了。

 

 

实例

下面,以Android SDK中的便笺例子来说明,Intent如何定义及如何被解析。这个应用可以让用户浏览便笺列表、查看每一个便笺的详细信息。

Manifest.xml

Xml代码  

 

1.  <manifest xmlns:android="http://schemas.android.com/apk/res/android"   

2.  package="com.google.android.notepad">   

3.      <application android:icon="@drawable/app_notes"   

4.  android:label="@string/app_name">   

5.      <provider class="NotePadProvider"   

6.  android:authorities="com.google.provider.NotePad" />   

7.      <activity class=".NotesList"="@string/title_notes_list">   

8.        <intent-filter>   

9.          <action android:value="android.intent.action.MAIN"/>   

10.        <category android:value="android.intent.category.LAUNCHER" />   

11.      </intent-filter>   

12.      <intent-filter>   

13.        <action android:value="android.intent.action.VIEW"/>   

14.        <action android:value="android.intent.action.EDIT"/>   

15.        <action android:value="android.intent.action.PICK"/>   

16.        <category android:value="android.intent.category.DEFAULT" />   

17.        <type android:value="vnd.android.cursor.dir/vnd.google.note" />   

18.      </intent-filter>   

19.      <intent-filter>   

20.        <action android:value="android.intent.action.GET_CONTENT" />   

21.        <category android:value="android.intent.category.DEFAULT" />   

22.        <type android:value="vnd.android.cursor.item/vnd.google.note" />   

23.      </intent-filter>   

24.    </activity>   

25.    <activity class=".NoteEditor"="@string/title_note">   

26.      <intent-filter android:label="@string/resolve_edit">   

27.        <action android:value="android.intent.action.VIEW"/>   

28.        <action android:value="android.intent.action.EDIT"/>   

29.        <category android:value="android.intent.category.DEFAULT" />   

30.        <type android:value="vnd.android.cursor.item/vnd.google.note" />   

31.      </intent-filter>   

32.      <intent-filter>   

33.        <action android:value="android.intent.action.INSERT"/>   

34.        <category android:value="android.intent.category.DEFAULT" />   

35.        <type android:value="vnd.android.cursor.dir/vnd.google.note" />   

36.      </intent-filter>   

37.    </activity>   

38.    <activity class=".TitleEditor"="@string/title_edit_title"   

39.android:theme="@android:style/Theme.Dialog">   

40.      <intent-filter android:label="@string/resolve_title">   

41.        <action android:value="com.google.android.notepad.action.EDIT_TITLE"/>   

42.        <category android:value="android.intent.category.DEFAULT" />   

43.        <category android:value="android.intent.category.ALTERNATIVE" />   

44.        <category android:value="android.intent.category.SELECTED_ALTERNATIVE"/>   

45.        <type android:value="vnd.android.cursor.item/vnd.google.note" />   

46.      </intent-filter>   

47.    </activity>   

48.</application>   

49.</manifest>  

  
例子中的第一个Activity 是com.google.android.notepad.NotesList,它是应用的主入口,提供了三个功能,分别由三个intent-filter进行描述:
        1、第一个是进入便笺应用的顶级入口(action为android.app.action.MAIN)。类型为android.app.category.LAUNCHER表明这个Activity将在Launcher中列出。
        2、第二个是,当type为vnd.android.cursor.dir/vnd.google.note(保存便笺记录的目录)时,可以查看可用的便笺(action为android.app.action.VIEW),或者让用户选择一个便笺并返回给调用者(action为android.app.action.PICK)。
        3、第三个是,当type为vnd.android.cursor.item/vnd.google.note时,返回给调用者一个用户选择的便笺(action为android.app.action.GET_CONTENT),而用户却不需要知道便笺从哪里读取的。有了这些功能,下面的Intent就会被解析到NotesList这个activity: 
    * { action=android.app.action.MAIN }:与此Intent匹配的Activity,将会被当作进入应用的顶级入口。 
    * { action=android.app.action.MAIN, category=android.app.category.LAUNCHER }:这是目前Launcher实际使用的 Intent,用于生成Launcher的顶级列表。  
    * { action=android.app.action.VIEW data=content://com.google.provider.NotePad/notes }:显示"content://com.google.provider.NotePad/notes"下的所有便笺的列表,使用者可以遍历列表,并且察看某便笺的详细信息。  
    * { action=android.app.action.PICK data=content://com.google.provider.NotePad/notes }:显示"content://com.google.provider.NotePad/notes"下的便笺列表,让用户可以在列表中选择一个,然后将选择的便笺的 URL返回给调用者。 
    * { action=android.app.action.GET_CONTENT type=vnd.android.cursor.item/vnd.google.note }:和上面的action为pick的Intent类似,不同的是这个Intent允许调用者(在这里指要调用NotesList的某个Activity)指定它们需要返回的数据类型,系统会根据这个数据类型查找合适的 Activity(在这里系统会找到NotesList这个Activity),供用户选择便笺。  
        第二个Activity是com.google.android.notepad.NoteEditor,它为用户显示一条便笺,并且允许用户修改这个便笺。它定义了两个intent-filter,所以具有两个功能。第一个功能是,当数据类型为 vnd.android.cursor.item/vnd.google.note时,允许用户查看和修改一个便签(action为 android.app.action.VIEW和android.app.action.EDIT)。第二个功能是,当数据类型为vnd.android.cursor.dir/vnd.google.note,为调用者显示一个新建便笺的界面,并将新建的便笺插入到便笺列表中(action为android.app.action.INSERT)。 
      有了这两个功能,下面的Intent就会被解析到NoteEditor这个activity: 
    * { action=android.app.action.VIEW data=content://com.google.provider.NotePad/notes/{ID} } :向用户显示标识为 ID的便笺。 
    * { action=android.app.action.EDIT data=content://com.google.provider.NotePad/notes/{ID} }:允许用户编辑标识为ID的便笺。  
    * { action=android.app.action.INSERT data=content://com.google.provider.NotePad/notes }:在“content://com.google.provider.NotePad/notes”这个便笺列表中创建一个新的空便笺,并允许用户编辑这个便签。当用户保存这个便笺后,这个新便笺的URI将会返回给调用者。  
        最后一个Activity是com.google.android.notepad.TitleEditor,它允许用户编辑便笺的标题。它可以被实现为一个应用可以直接调用(在Intent中明确设置component属性)的类,不过这里我们将为你提供一个在现有的数据上发布可选操作的方法。在这个 Activity的唯一的intent-filter中,拥有一个私有的action: com.google.android.notepad.action.EDIT_TITLE,表明允许用户编辑便笺的标题。和前面的view和edit 动作一样,调用这个Intent 的时候,也必须指定具体的便笺(type为vnd.android.cursor.item/vnd.google.note)。不同的是,这里显示和编辑的只是便笺数据中的标题。
      除了支持缺省类别(android.intent.category.DEFAULT),标题编辑器还支持另外两个标准类别:android.intent.category.ALTERNATIVE和 android.intent.category.SELECTED_ALTERNATIVE。实现了这两个类别之后,其它 Activity就可以调用queryIntentActivityOptions(ComponentName, Intent[], Intent, int)查询这个Activity提供的action,而不需要了解它的具体实现;或者调用addIntentOptions(int, int, ComponentName, Intent[], Intent, int, Menu.Item[])建立动态菜单。需要说明的是,在这个intent-filter中有一个明确的名称(通过android:label= "@string/resolve_title"指定),在用户浏览数据的时候,如果这个Activity是数据的一个可选操作,指定明确的名称可以为用户提供一个更好控制界面。
      有了这个功能,下面的Intent就会被解析到TitleEditor这个Activity: 
    * { action=com.google.android.notepad.action.EDIT_TITLE data=content://com.google.provider.NotePad/notes/{ID} }:显示并且允许用户编辑标识为ID的便笺的标题。


如果是从BroadcastReceiver 启动一个新的Activity , 不要忘记i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

[java]  view plain copy print ?
  1. public class MyReceiver extends BroadcastReceiver{  
  2. public static final String action=”acc”;  
  3. public void onReceive(Context context, Intent intent) {  
  4. Intent i=new Intent(context,Receivered.class);  
  5. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  6. context.startActivity(i);  
  7. }  
  8. }  


1. 指定act ion 和type
// SIM import

[java]  view plain copy print ?
  1. Intent importIntent = new Intent(Intent.ACTION_VIEW);  
  2. importIntent.setType(“vnd.android.cursor.item/sim-contact”);  
  3. importIntent.setClassName(“com.android.phone”, “com.android.phone.SimContacts”);  
  4. menu.add(000, R.string.importFromSim)  
  5. .setIcon(R.drawable.ic_menu_import_contact)  
  6. .setIntent(importIntent);  


2. 指定act ion, da ta和type
(1)隐式查找type
示例代码:

[java]  view plain copy print ?
  1. uri: content://simcontacts/simPeople/(id)  
  2. intent = new Intent(“android.intent.action.SIMEDIT”,uri);  
  3. startActivity(intent);  

程序会根据data中的uri去查找匹配的type(必须的)
provider中的getType()
case SIM_PEOPLE_ID:
return “vnd.android.cursor.item/sim-contact”;

配置文件中的filter设定
AndroidManifest.xml
也可以自己设定type,但只能使用 setDataAndType()

3. 其他设定intent的属性方式

[java]  view plain copy print ?
  1. Intent setComponent(ComponentName component)  
  2. Intent setClassName(Context packageContext, String className)  
  3. Intent setClassName(String packageName, String className)  
  4. Intent setClass(Context packageContext, Class cls)  


打开其他APK文件的ACTIVITY

[java]  view plain copy print ?
  1. Intent mIntent = new Intent();  
  2. ComponentName comp = new ComponentName(“com.android.a”,”com.android.a.AbcActivity” );  
  3. mIntent.setComponent(comp);  
  4. mIntent.setAction(“android.intent.action.MAIN”);  
  5. startActivity(mIntent);  


显示网页:

[java]  view plain copy print ?
  1.  Uri uri = Uri.parse(“http://www.google.com”);  
  2. Intent it = new Intent(Intent.ACTION_VIEW,uri);  
  3. startActivity(it);  


显示地图:

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“geo:38.899533,-77.036476″);  
  2. Intent it = new Intent(Intent.Action_VIEW,uri);  
  3. startActivity(it);  


路径规划:

1. Uri uri = Uri.parse(“http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en”);

2. Intent it = new Intent(Intent.ACTION_VIEW,URI);

3. startActivity(it);

拨打电话:
调用拨号程序

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“tel:xxxxxx”);  
  2.  Intent it = new Intent(Intent.ACTION_DIAL, uri);  
  3.  startActivity(it);  

直接拨号

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“tel.xxxxxx”);  
  2. ntent it =new Intent(Intent.ACTION_CALL,uri);  


要使用这个必须在配置文件中加入权限

发送SMS/MMS
调用发送短信的程序

[java]  view plain copy print ?
  1. Intent it = new Intent(Intent.ACTION_VIEW);  
  2. it.putExtra(“sms_body”, “The SMS text”);  
  3.  it.setType(“vnd.android-dir/mms-sms”);  
  4. startActivity(it);  


发送短信

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“smsto:0800000123″);  
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
  3. it.putExtra(“sms_body”, “The SMS text”);  
  4. startActivity(it);  


发送彩信

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“content://media/external/images/media/23″);  
  2. Intent it = new Intent(Intent.ACTION_SEND);  
  3. it.putExtra(“sms_body”, “some text”);  
  4. it.putExtra(Intent.EXTRA_STREAM, uri);  
  5. it.setType(“image/png”);  
  6. startActivity(it);  


发送Email

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“mailto:xxx@abc.com”);  
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);  
  3. startActivity(it);  


[java]  view plain copy print ?
  1. Intent it = new Intent(Intent.ACTION_SEND);  
  2.  it.putExtra(Intent.EXTRA_EMAIL, “me@abc.com”);  
  3. it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  
  4. it.setType(“text/plain”);  
  5. startActivity(Intent.createChooser(it, “Choose Email Client”));  



[java]  view plain copy print ?
  1. Intent it=new Intent(Intent.ACTION_SEND);  
  2.  String[] tos={“me@abc.com”};  
  3.  String[] ccs={“you@abc.com”};  
  4. it.putExtra(Intent.EXTRA_EMAIL, tos);  
  5. it.putExtra(Intent.EXTRA_CC, ccs);  
  6. it.putExtra(Intent.EXTRA_TEXT, “The email body text”);  
  7. it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);  
  8. it.setType(“message/rfc822″);  
  9. startActivity(Intent.createChooser(it, “Choose Email Client”));  


添加附件

[java]  view plain copy print ?
  1. Intent it = new Intent(Intent.ACTION_SEND);  
  2. it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);  
  3. it.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3″);  
  4. sendIntent.setType(“audio/mp3″);  
  5. startActivity(Intent.createChooser(it, “Choose Email Client”));  


播放多媒体

//方法1.

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“file:///sdcard/song.mp3″);  
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  3. // it.addFlags(it.FLAG_ACTIVITY_NEW_TASK);//非必须选项  
  4. it.setDataAndType(uri, “audio/mp3″);  
  5. context.startActivity(it);  


//方法2.


[java]  view plain copy print ?
  1. Intent it = new Intent(Intent.ACTION_VIEW);  
  2. Uri uri = Uri.parse(“file:///sdcard/song.mp3″);  
  3. it.setDataAndType(uri, “audio/mp3″);  
  4. startActivity(it);  


//方法3. 启动一个播放器并播放一个系统声音

[java]  view plain copy print ?
  1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, “1″);  
  2.  Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  3.  startActivity(it);  
  4. Uninstall 程序  
  5.  Uri uri = Uri.fromParts(“package”, strPackageName, null);  
  6.  Intent it = new Intent(Intent.ACTION_DELETE, uri);  
  7.  startActivity(it);  



uninstall apk

[java]  view plain copy print ?
  1. Uri uninstallUri = Uri.fromParts(“package”, “xxx”, null);  
  2. returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);  



install apk

[java]  view plain copy print ?
  1. Uri installUri = Uri.fromParts(“package”, “xxx”, null);  
  2. eturnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);  



play audio

[java]  view plain copy print ?
  1. Uri playUri = Uri.parse(“file:///sdcard/download/everything.mp3″);  
  2. returnIt = new Intent(Intent.ACTION_VIEW, playUri);  

哈,原来你还没贴完,我再加个:

//发送附件

[java]  view plain copy print ?
  1.  Intent it = new Intent(Intent.ACTION_SEND);  
  2.  it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”);  
  3. it.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/eoe.mp3″);  
  4. sendIntent.setType(“audio/mp3″);  
  5. startActivity(Intent.createChooser(it, “Choose Email Client”));  



market相关

//搜索应用

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“market://search?q=pname:pkg_name”);  
  2.  Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  3. startActivity(it);  
  4. //where pkg_name is the full package path for an application  


//显示指定应用的详细页面(这个好像不支持了,找不到app_id)

[java]  view plain copy print ?
  1. Uri uri = Uri.parse(“market://details?id=app_id”);  
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);  
  3. startActivity(it);  

相关文章
|
3月前
|
Android开发 开发者
Android基础知识:什么是Intent?有哪些类型的Intent?
Android基础知识:什么是Intent?有哪些类型的Intent?
48 0
|
6月前
|
存储 SQL 人工智能
Android Activity启动流程一:从Intent到Activity创建
Android Activity启动流程一:从Intent到Activity创建
|
4月前
|
Android开发
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
43 0
|
9月前
|
Android开发
关于Android中intent传值问题
关于Android中intent传值问题
57 0
|
8月前
|
Java API Android开发
Android 中Activity和Intent的详解
Android 中Activity和Intent的详解
75 0
|
8月前
|
Java Android开发
Android 中通过Intent传递类对象,通过实现Serializable和Parcelable接口两种方式传递对象
Android 中通过Intent传递类对象,通过实现Serializable和Parcelable接口两种方式传递对象
75 1
|
10月前
|
Java API 文件存储
Android:常用的隐式 Intent
本篇文章的内容其实是属于上一篇文章(Android 基础知识5:Intent 和 Intent 过滤器)的延伸,考虑到篇幅长度的原因,所以没有把本篇文章的内容写到上一篇文章中,另外单独写成一篇文章还有个好处就是方便读者查阅。这篇文章主要为大家列举了常用的隐式 Intent,大家如果在平时工作中有相关的需求可以直接复制代码使用。
67 0
Android:常用的隐式 Intent
|
10月前
|
XML 存储 前端开发
Android:Intent 和 Intent 过滤器
在前 4 篇文章中,我们介绍了 Android 四大组件的基础知识,四大组件是构成我们 App 的基础,也是 Android 系统设计的最佳体现。各个组件之间完全是解耦的,如果想访问其他组件或者启动其他组件可以使用 Intent 来操作。在四种组件类型中,有三种(Activity、Service 和 Broadcast)均可以通过异步消息 Intent 进行启动。Intent 会在运行时对各个组件进行互相绑定。所以我们可以把 Intent 当作是各个组件之间的信使(无论该组件是自己 App 的还是其他 App)。
51 0
Android:Intent 和 Intent 过滤器
|
API 开发工具 数据库
❤️【Android精进之路-06】真的有必要说说Intent,Intent就仙女的魔法棒❤️
上一篇文章我们详细介绍了Activity组件的创建以及传参。但是其中的关键人物Intent 却没有做过多的介绍。
92 0
|
安全 API Android开发
谈一谈 Android 嵌套 Intent 以及解决方案
谈一谈 Android 嵌套 Intent 以及解决方案
谈一谈 Android 嵌套 Intent 以及解决方案