Android Intent机制详解

什么是Intent


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


Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。比如,有一个Activity希望打开网页浏览器查看某一网页的内容,那么这个Activity只需要发出WEB_SEARCH_ACTION给Android,Android就会根据Intent的请求内容,查询各组件注册时声明的IntentFilter,找到网页浏览器的Activity来浏览网页。

Android的三个基本组件——Activity,Service和Broadcast Receiver——都是通过Intent机制激活的,不同类型的组件有不同的传递Intent方式:


1.1 要激活一个新的Activity,或者让一个现有的Activity做新的操作,可以通过调用Context.startActivity()或者Activity.startActivityForResult()方法。

1.2 要启动一个新的Service,或者向一个已有的Service传递新的指令,调用Context.startService()方法或者调用Context.bindService()方法将调用此方法的上下文对象与Service绑定。


1.3 Context.sendBroadcast()、Context.sendOrderBroadcast()、Context.sendStickBroadcast()这三个方法可以发送Broadcast Intent。发送之后,所有已注册的并且拥有与之相匹配IntentFilter的BroadcastReceiver就会被激活。

Intent一旦发出,Android都会准确找到相匹配的一个或多个Activity,Service或者BroadcastReceiver作响应。所以,不同类型的Intent消息不会出现重叠,即Broadcast的Intent消息只会发送给BroadcastReceiver,而决不会发送给Activity或者Service。由startActivity()传递的消息也只会发给Activity,由startService()传递的Intent只会发送给Service。


Intent的构成


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


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


当日常生活中,描述一个意愿或愿望的时候,总是有一个动词在其中。比如:我想 三个俯卧撑;我要 一部x片;我要 一部血泪史,之类云云。在Intent中,Action就是描述看、做、写等动作的,当你指明了一个Action,执行者就会依照这个动作的指示,接受相关输入,表现对应行为,产生符合的输出。在Intent类中,定义了一批量的动作,比如ACTION_VIEWACTION_PICK ,之类的,基本涵盖了常用动作,整一个降龙十八掌全集。


一些常用的Action:


ACTION_CALL activity 启动一个电话.
ACTION_EDIT activity 显示用户编辑的数据.
ACTION_MAIN activity 作为Task中第一个Activity启动
ACTION_SYNC activity 同步手机与数据服务器上的数据.
ACTION_BATTERY_LOW broadcast receiver 电池电量过低警告.
ACTION_HEADSET_PLUG broadcast receiver 插拔耳机警告
ACTION_SCREEN_ON broadcast receiver 屏幕变亮警告.
ACTION_TIMEZONE_CHANGED broadcast receiver 改变时区警告.


Data: 要事实的具体的数据,一般由一个Uri变量来表示

Category:一个字符串,包含了关于处理该intent的组件的种类的信息。一个intent对象可以有任意个category。intent类定义了许多category常数.

addCategory()方法为一个intent对象增加一个category,
removeCategory删除一个category,
getCategories()获取intent所有的category.


Type:显式指定Intent的数据类型(MIME)(多用途互联网邮件扩展,Multipurpose Internet Mail Extensions)。比如,一个组件是可以显示图片数据的而不能播放声音文件。很多情况下,data类型可在URI中找到,比如content:开头的URI,表明数据由设备上的content provider提供。但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。


MIME类型有2种形式


1.1  单个记录的格式: vnd.android.cursor.item/vnd.yourcompanyname.contenttype,如:content://com.example.transportationprovider/trains/122(一条列车信息的uri)的MIME类型是vnd.android.cursor.item/vnd.example.rail


1.2 多个记录的格式:vnd.android.cursor.dir/vnd.yourcompanyname.contenttype,如:content://com.example.transportationprovider/trains (所有列车信息)的MIME类型是vnd.android.cursor.dir/vnd.example.rail


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

Intent it = new Intent(Activity.Main.this, Activity2.class); startActivity(it);

startActivity(it);


extras:附加信息,例如ACTION_TIMEZONE_CHANGED的intent有一个"time-zone"附加信息来指明新的时区,而ACTION_HEADSET_PLUG有一个“state”附加信息来指示耳机是被插入还是被拔出。intent对象有一系列put...()和set...()方法来设定和获取附加信息。 这些方法和Bundle对象很像。事实上附加信息可以使用putExtras()和getExtras()作为Bundle来读和写。例如:


//用Bundle传递数据 Intent it = new Intent(Activity.Main.this, Activity2.class); Bundle bundle=new Bundle(); bundle.putString("name", "This is from MainActivity!"); it.putExtras(bundle); startActivity(it); //获得数据 Bundle bundle=getIntent().getExtras(); String name=bundle.getString("name");


intent的解析:

在应用中,我们可以以两种形式来使用Intent:


1.1 显式Intent:指定了component属性的Intent(调用setComponent(ComponentName)或者setClass(Context, Class)来指定)。通过指定具体的组件类,通知应用启动对应的组件。

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



下面,以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的便笺的标题。



Intent解析机制

Intent解析机制主要是通过查找已注册在AndroidManifest.xml中的所有<intent-filter>及其中定义的Intent,通过PackageManager(注:PackageManager能够得到当前设备上所安装的
application package的信息)来查找能处理这个Intent的component。在这个解析过程中,Android是通过Intent的action、type、category这三个属性来进行判断的,判断方法如下:
1.1  如果Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;
1.2  如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配。
1.3  如果Intent中的数据不是content:类型的URI,而且Intent也没有明确指定type,将根据Intent中数据的scheme(比如 http:或者mailto:)进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中。
1.4 如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比如Intent中包含了两个类别:LAUNCHER_CATEGORY和ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。



Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件。Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙述其所期望的服务或动作、与动作有关的数据等。Android则根据此Intent对象之叙述,负责配对,找出相配的组件,然后将 Intent对象传递给所找到的组件,Android的媒婆任务就完成了。

 

在Google Doc中是这样描述Intent的(摘自Android中文翻译组)
当接收到ContentResolver发出的请求后,内容提供者被激活。而其它三种组件──activity、服务和广播接收器被一种叫做intent的异步消息所激活。intent是一个保存着消息内容的Intent对 象。对于activity和服务来说,它指明了请求的操作名称以及作为操作对象的数据的URI和其它一些信息。比如说,它可以承载对一个activity 的请求,让它为用户显示一张图片,或者让用户编辑一些文本。而对于广播接收器而言,Intent对象指明了声明的行为。比如,它可以对所有感兴趣的对象声 明照相按钮被按下。

对于每种组件来说,激活的方法是不同的:
1.通过传递一个Intent对象至 Context.startActivity()或Activity.startActivityForResult()以载入(或指定新工作给)一个activity。相应的activity可以通过调用 getIntent() 方法来查看激活它的intent。Android通过调用activity的onNewIntent()方法来传递给它继发的intent。

一个activity经常启动了下一个。如果它期望它所启动的那个activity返回一个结果,它会以调用startActivityForResult()来取代startActivity()。比如说,如果它启动了另外一个activity以使用户挑选一张照片,它也许想知道哪张照片被选中了。结果将会被封装在一个Intent对象中,并传递给发出调用的activity的onActivityResult() 方法。

2.通过传递一个Intent对象至Context.startService()将启动一个服务(或给予正在运行的服务以一个新的指令)。Android调用服务的onStart()方法并将Intent对象传递给它。

与此类似,一个Intent可以被调用组件传递给 Context.bindService()以获取一个正在运行的目标服务的连接。这个服务会经由onBind() 方法的调用获取这个Intent对象(如果服务尚未启动,bindService()会先启动它)。比如说,一个activity可以连接至前述的音乐回放服务,并提供给用户一个可操作的(用户界面)以对回放进行控制。这个activity可以调用 bindService() 来建立连接,然后调用服务中定义的对象来影响回放。

3.应用程序可以凭借将Intent对象传递给 Context.sendBroadcast() ,Context.sendOrderedBroadcast(), 以及Context.sendStickyBroadcast()和其它类似方法来产生一个广播。Android会调用所有对此广播有兴趣的广播接收器的 onReceive()方法将intent传递给它们。

 

Intent对象包含的内容

在Intent类的Java源代码中定义了Intent相关内容的变量,如下:

[java]  view plain copy
  1. // Action  
  2. private String mAction;  
  3. // Data  
  4. private Uri mData;  
  5. private String mType;  
  6. private String mPackage;  
  7. // ComponentName  
  8. private ComponentName mComponent;  
  9. // Flag  
  10. private int mFlags;  
  11. // category  
  12. private HashSet<String> mCategories;  
  13. // extras  
  14. private Bundle mExtras;  

1.componentName(组件名称),指定Intent的目标组件的类名称。组件名称是可选的,如果填写,Intent对象会发送给指定组件名称的组件,否则也可以通过其他Intent信息定位到适合的组件。组件名称是个ComponentName类型的对象。
用法:

 

[java]  view plain copy
  1. Intent intent = new Intent();  
  2. // 构造的参数为当前Context和目标组件的类路径名  
  3. ComponentName cn = new ComponentName(HelloActivity.this"com.byread.activity.OtherActivity");  
  4. intent.setComponent(cn);  
  5. startActivity(intent);  
相当于以下常用方法:
[java]  view plain copy
  1. Intent intent = new Intent();  
  2. intent.setClass(HelloActivity.this, OtherActivity.class);  
  3. startActivity(intent);  

Intent类中也包含一个初始化ComponentName的构造函数: 

[java]  view plain copy
  1. public Intent(Context packageContext, Class<?> cls) {  
  2.     mComponent = new ComponentName(packageContext, cls);  
  3. }  

 

2.action(动作),指定Intent的执行动作,比如调用拨打电话组件。 

[java]  view plain copy
  1. public Intent(String action) {  
  2.     mAction = action;  
  3. }  

 

3.data(数据),起到表示数据和数据MIME类型的作用。不同的action是和不同的data类型配套的,通过设置data的Uri来获得。 

[java]  view plain copy
  1. public Intent(String action, Uri uri) {  
  2.     mAction = action;  
  3.     mData = uri;  
  4. }  

比如调用拨打电话组件:

[java]  view plain copy
  1. Uri uri = Uri.parse("tel:10086");  
  2. // 参数分别为调用拨打电话组件的Action和获取Data数据的Uri  
  3. Intent intent = new Intent(Intent.ACTION_DIAL, uri);  
  4. startActivity(intent);  

 

4.category(类别),被执行动作的附加信息。例如应用的启动Activity在intent-filter中设置category。

[xhtml]  view plain copy
  1. <intent-filter>  
  2.     <action android:name="android.intent.action.MAIN" />  
  3.     <category android:name="android.intent.category.LAUNCHER" />  
  4. </intent-filter>  

 

5.extras(附加信息),为处理Intent组件提供附加的信息。可通过putXX()和getXX()方法存取信息;也可以通过创建Bundle对象,再通过putExtras()和getExtras()方法来存取。 

 

6.flags(标记),指示Android如何启动目标Activity,设置方法为调用Intent的setFlags方法。常用的Flags参数有:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_NO_HISTORY
FLAG_ACTIVITY_SINGLE_TOP  

 

Intent的投递

1.显式方式。直接设置目标组件的ComponentName,用于一个应用内部的消息传递,比如启动另一个Activity或者一个services。
通过Intent的setComponent和setClass来制定目标组件的ComponentName。

 

2.隐式方式。ComponentName为空,用于调用其他应用中的组件。需要包含足够的信息,这样系统才能根据这些信息使用intent filter在所有的组件中过滤action、data或者category来匹配目标组件。可参考Android中Activity组件详解(5.Activity的Intent Filter)
如果Intent指明定了action,则目标组件的IntentFilter的action列表中就必须包含有这个action,否则不能匹配;
如果Intent没有提供type,系统将从data中得到数据类型。和action一样,目标组件的数据类型列表中必须包含Intent的数据类型,否则不能匹配;
如果Intent中的数据不是content: 类型的URI,而且Intent也没有明确指定它的type,将根据Intent中数据的scheme (比如 http: 或者mailto: ) 进行匹配。同上,Intent 的scheme必须出现在目标组件的scheme列表中;
如果Intent指定了一个或多个category,这些类别必须全部出现在组建的类别列表中。比如 Intent中包含了两个类别:LAUNCHER_CATEGORY 和 ALTERNATIVE_CATEGORY,解析得到的目标组件必须至少包含这两个类别。

 

Intent调用常见系统组件 

[java]  view plain copy
  1. // 调用浏览器  
  2. Uri webViewUri = Uri.parse("http://blog.csdn.net/zuolongsnail");  
  3. Intent intent = new Intent(Intent.ACTION_VIEW, webViewUri);  
  4.   
  5. // 调用地图  
  6. Uri mapUri = Uri.parse("geo:100,100");  
  7. Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);  
  8.   
  9. // 播放mp3  
  10. Uri playUri = Uri.parse("file:///sdcard/test.mp3");  
  11. Intent intent = new Intent(Intent.ACTION_VIEW, playUri);  
  12. intent.setDataAndType(playUri, "audio/mp3");  
  13.   
  14. // 调用拨打电话  
  15. Uri dialUri = Uri.parse("tel:10086");  
  16. Intent intent = new Intent(Intent.ACTION_DIAL, dialUri);  
  17. // 直接拨打电话,需要加上权限<uses-permission id="android.permission.CALL_PHONE" />  
  18. Uri callUri = Uri.parse("tel:10086");  
  19. Intent intent = new Intent(Intent.ACTION_CALL, callUri);  
  20.   
  21. // 调用发邮件(这里要事先配置好的系统Email,否则是调不出发邮件界面的)  
  22. Uri emailUri = Uri.parse("mailto:zuolongsnail@163.com");  
  23. Intent intent = new Intent(Intent.ACTION_SENDTO, emailUri);  
  24. // 直接发邮件  
  25. Intent intent = new Intent(Intent.ACTION_SEND);  
  26. String[] tos = { "zuolongsnail@gmail.com" };  
  27. String[] ccs = { "zuolongsnail@163.com" };  
  28. intent.putExtra(Intent.EXTRA_EMAIL, tos);  
  29. intent.putExtra(Intent.EXTRA_CC, ccs);  
  30. intent.putExtra(Intent.EXTRA_TEXT, "the email text");  
  31. intent.putExtra(Intent.EXTRA_SUBJECT, "subject");  
  32. intent.setType("text/plain");  
  33. Intent.createChooser(intent, "Choose Email Client");  
  34.   
  35. // 发短信  
  36. Intent intent = new Intent(Intent.ACTION_VIEW);  
  37. intent.putExtra("sms_body""the sms text");  
  38. intent.setType("vnd.android-dir/mms-sms");  
  39. // 直接发短信  
  40. Uri smsToUri = Uri.parse("smsto:10086");  
  41. Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);  
  42. intent.putExtra("sms_body""the sms text");  
  43. // 发彩信  
  44. Uri mmsUri = Uri.parse("content://media/external/images/media/23");  
  45. Intent intent = new Intent(Intent.ACTION_SEND);  
  46. intent.putExtra("sms_body""the sms text");  
  47. intent.putExtra(Intent.EXTRA_STREAM, mmsUri);  
  48. intent.setType("image/png");  
  49.   
  50. // 卸载应用  
  51. Uri uninstallUri = Uri.fromParts("package""com.app.test"null);  
  52. Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);  
  53. // 安装应用  
  54. Intent intent = new Intent(Intent.ACTION_VIEW);  
  55. intent.setDataAndType(Uri.fromFile(new File("/sdcard/test.apk"), "application/vnd.android.package-archive");  
  56.   
  57. // 在Android Market中查找应用  
  58. Uri uri = Uri.parse("market://search?q=愤怒的小鸟");           
  59. Intent intent = new Intent(Intent.ACTION_VIEW, uri);  



转自:http://blog.csdn.net/t12x3456/article/details/7688154  

            http://blog.csdn.net/zuolongsnail/article/details/6574211

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值