Android.Intent.Action 迷南。 2022-06-04 06:59 284阅读 0赞 #### **Intent.Action的作用** #### 在讲action之前,我跟大家回忆一个场景。用过Android手机的人应该都知道,比如说我在某个App例如新浪微博中点击拍照,会跳转到照相机的界面。但是当我新装了camer360或之类的第三方照相app的时候在微博中点击照相就会先弹出一个Dialog来让我选择是使用默认camer还是camer360. ok,在讲完刚刚那个场景之后我给大家介绍一下显示intent 与隐式intent。 对于明确指出了目标组件名称的Intent,我们称之为“**显式Intent**”。对于没有明确指出目标组件名称的Intent,则称之为“**隐式 Intent**”。**Android系统使用IntentFilter 来寻找与隐式Intent相关的对象。** **显式Intent**直接用组件的名称定义目标组件,这种方式很直接。但是由于开发人员往往并不清楚别的应用程序的组件名称,因此,显式Intent更 多用于在应用程序内部传递消息。比如在某应用程序内,一个Activity启动一个Service。 **隐式Intent**恰恰相反,它不会用组件名称定义需要 激活的目标组件,它更广泛地用于在不同应用程序之间传递消息。 在通常的HelloWorld中使用的都是显示Intent,即需要指明跳转的activity对象,如:Intent intent = new Intent(MainActivity.this,BActivity.class); 如果要跳转到一个未知路径,但是已知功能的Activity就要用到隐式intent,如;Intent intent = new Intent(Intent.ACTION\_SEND); #### **实例** #### **实例一:** 有三个Activity:MainActivity.java/BAcitvity.java/CActivity.java 假设这是一个照相应用MainActivity是程序入口,BActivity和CActivity都可以处理照相的内容。 在我们不会使用action的时候我们要使这个应用既可以使用BActivity照相,又能使用CActivity照相如何实现? 当然是在MainActivity中写两个Button, new Intent(MainActivity.this,BActivity.class); new Intent(MainActivity.this,CActivity.class); 通过两种不同的Intent来发起请求。 **清单配置文件:** <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BActivity" android:label="@string/title_activity_main" android:launchMode="singleTask" > <intent-filter> <action android:name="com.h3c.intent.ACTION_VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> <activity android:name=".CActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="com.h3c.intent.ACTION_VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> </intent-filter> </activity> **我们在MainActivity中实现一个Button,点击发送:** new Intent(“com.h3c.intent.ACTION\_VIEW”); 此时就会弹出一个Dialog让我们自动选择是使用BActivity还是使用CActiviy了,就如前文我提到camer360的那个例子 这也就是隐式intent,即发送一种类型的action,可以让所有支持该类型的activity都收到消息,然后由用户选择使用哪个activity处理。 **实例二:(Android分享与接收分享)** 自己之前其实也接触过action,但是没有自己的去想清楚。比如在做DLNA这个项目的时候需要支持第三方媒体app分享给DLNA,如何实现呢? 首先看看如何分享给别人: public static void shareText(Context context, String title, String text) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_SUBJECT, title); intent.putExtra(Intent.EXTRA_TEXT, text); context.startActivity(Intent.createChooser(intent, title)); } 就是用上文提到的隐式intent,因为我并不知道有那些应用可以支持分享,所以我用隐式intent发起分享这一类的请求。 我们再看如何支持分享: <activity android:name=".SharePage" android:label="分享到微博"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> </activity> 是的,使用action告诉别人我支持SEND这类隐式intent,注意必须也有这样一句话,否则运行会报错。 <category android:name="android.intent.category.DEFAULT" /> 最后通过data约束一下格式。 **注:phonewindow.java文件中Intent Action在Intent.java中有定义,而接受方在packages/apps/XXXX对应的AndroidManifest.xml文件中有定义。** #### **ACTION值自定义** #### 在启动一个ACTIVITY时,作为信使的Intent主要由三部分构成:Intent<Action,Data,Category>;对于Action,有时候我们不一定非得用系统自定义的值,而可以自定义,同样可以达到启动其它Activity的效果。 这里我们需要明确如下定义: **1.ACTION的值是一个类型为String的字符串** 2.Intent中ACTION的值是双向的,发送方发给接受方中的ACTION值能够被双方了解;这样在发送方中,自定义ACTION的值;在接受方中,Intent-filter中的ACTION值要与自定义的值相同,这样才能够正确接受Intent 现在我们以一个简单的例子来说明自定义ACTION值的使用。 **这里我们有两个ACTIVITY A和B,其中A作为Intent发送方,B为接受方;** **在A的java中定义ACTION的值,并将其发送出去,如下** public static final String myACTION=”B.ACTION”; Intent intent=new Intent(myACTION); startActivity(intent); **在B中的Androidmanifest.xml文件中,添加如下activity的intent-filter** <intent-filter> <action android:name=”B.ACTION” /> <category android:name=”android.intent.category.DEFAULT” /> </intent-filter> **这样,A就能够成功的启动B了** 最后,自定义的ACTION值最好格式为:这个字符串最好是package name + activity class name+your action #### **Android中Intent的各种常见作用** #### #### **1 Intent.ACTION\_MAIN** #### **String: android.intent.action.MAIN** **标识Activity为一个程序的开始。比较常用。** Input:nothing Output:nothing <activity android:name=”.Main” android:label=”@string/app\_name”> <intent-filter> **<action android:name=”android.intent.action.MAIN” />** <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter> </activity> #### **2 Intent.Action\_CALL** #### **Stirng: android.intent.action.CALL** **呼叫指定的电话号码。** Input:电话号码。数据格式为:tel:+phone number Output:Nothing Intent intent=new Intent(); intent.setAction(Intent.ACTION\_CALL); intent.setData(Uri.parse(“tel:1320010001”); startActivity(intent); #### **3 Intent.Action.DIAL** #### **String: action.intent.action.DIAL** **调用拨号面板** Intent intent=new Intent(); intent.setAction(Intent.ACTION\_DIAL); //android.intent.action.DIAL intent.setData(Uri.parse(“tel:1320010001”); startActivity(intent); Input:电话号码。数据格式为:tel:+phone number Output:Nothing 说明:打开Android的拨号UI。如果没有设置数据,则打开一个空的UI,如果设置数据,action.DIAL则通过调用**getData()**获取电话号码。 但设置电话号码的数据格式为 tel:+phone number. #### **4 Intent.Action.ALL\_APPS** #### **String: andriod.intent.action.ALL\_APPS** **列出所有的应用。** Input:Nothing. Output:Nothing. #### **5 Intent.ACTION\_ANSWER** #### **Stirng:android.intent.action.ANSWER** **处理呼入的电话。** Input:Nothing. Output:Nothing. #### **6 Intent.ACTION\_ATTACH\_DATA** #### **String: android.action.ATTCH\_DATA** **别用于指定一些数据应该附属于一些其他的地方,例如,图片数据应该附属于联系人** Input: Data Output:nothing #### 7 Intent.ACTION\_BUG\_REPORT #### **String: android.intent.action.BUG\_REPORT** **显示Dug报告。** Input:nothing output:nothing #### **8 Intent.Action\_CALL\_BUTTON** #### **String: android.action.intent.CALL\_BUTTON.** **相当于用户按下“拨号”键。经测试显示的是“通话记录”** Input:nothing Output:nothing Intent intent = new Intent(Intent.ACTION\_CALL\_BUTTON); startActivity(intent); #### 9 Intent.ACTION\_CHOOSER #### **String: android.intent.action.CHOOSER** \*\*显示一个activity选择器,允许用户在进程之前选择他们想要的,与之对应的是\*\*Intent.ACTION\_GET\_CONTENT. #### 10. Intent.ACTION\_GET\_CONTENT #### **String: android.intent.action.GET\_CONTENT** **允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)** Input: Type Output:URI int requestCode = 1001; Intent intent = new Intent(Intent.ACTION\_GET\_CONTENT); // “android.intent.action.GET\_CONTENT” intent.setType(“image/*“); // 查看类型,如果是其他类型,比如视频则替换成 video/*,或 */* Intent wrapperIntent = Intent.createChooser(intent, null); startActivityForResult(wrapperIntent, requestCode); #### **11 Intent.ACTION\_VIEW** #### **String android.intent.action.VIEW** **用于显示用户的数据。** **比较通用,会根据用户的数据类型打开相应的Activity。** 比如 tel:13400010001打开拨号程序,[http://www.g.cn][http_www.g.cn]则会打开浏览器等。 Uri uri = Uri.parse(“[http://www.google.com][http_www.google.com]“); //**浏览器** Uri uri =Uri.parse(“tel:1232333”); **//拨号程序** Uri uri=Uri.parse(“geo:39.899533,116.036476”); **//打开地图定位** Intent it = new Intent(Intent.ACTION\_VIEW,uri); startActivity(it); **//播放视频** Intent intent = new Intent(Intent.ACTION\_VIEW); Uri uri = Uri.parse(“file:///sdcard/media.mp4”); intent.setDataAndType(uri, “video/\*”); startActivity(intent); **//调用发送短信的程序** Intent it = new Intent(Intent.ACTION\_VIEW); it.putExtra(“sms\_body”, “信息内容…”); it.setType(“vnd.android-dir/mms-sms”); startActivity(it); #### 12 Intent.ACTION\_SENDTO #### String: android.intent.action.SENDTO 说明:发送短信息 **//发送短信息** Uri uri = Uri.parse(“smsto:13200100001”); Intent it = new Intent(Intent.ACTION\_SENDTO, uri); it.putExtra(“sms\_body”, “信息内容…”); startActivity(it); **//发送彩信,设备会提示选择合适的程序发送** Uri uri = Uri.parse(“content://media/external/images/media/23”); **//设备中的资源(图像或其他资源)** Intent intent = new Intent(Intent.ACTION\_SEND); intent.putExtra(“sms\_body”, “内容”); intent.putExtra(Intent.EXTRA\_STREAM, uri); intent.setType(“image/png”); startActivity(it); **//Email** Intent intent=new Intent(Intent.ACTION\_SEND); String\[\] tos=\{“android1@163.com”\}; String\[\] ccs=\{“you@yahoo.com”\}; intent.putExtra(Intent.EXTRA\_EMAIL, tos); intent.putExtra(Intent.EXTRA\_CC, ccs); intent.putExtra(Intent.EXTRA\_TEXT, “The email body text”); intent.putExtra(Intent.EXTRA\_SUBJECT, “The email subject text”); intent.setType(“message/rfc822”); startActivity(Intent.createChooser(intent, “Choose Email Client”)); #### 13 Intent.ACTION\_GET\_CONTENT #### **//选择图片 requestCode 返回的标识** Intent intent = new Intent(Intent.ACTION\_GET\_CONTENT); //”android.intent.action.GET\_CONTENT” intent.setType(contentType); //查看类型 String IMAGE\_UNSPECIFIED = “image/\*”; Intent wrapperIntent = Intent.createChooser(intent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); **//添加音频** Intent intent = new Intent(Intent.ACTION\_GET\_CONTENT); intent.setType(contentType); //String VIDEO\_UNSPECIFIED = “video/\*”; Intent wrapperIntent = Intent.createChooser(intent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); **//拍摄视频** int durationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt(“ro.media.enc.lprof.duration”, 60); Intent intent = new Intent(MediaStore.ACTION\_VIDEO\_CAPTURE); intent.putExtra(MediaStore.EXTRA\_VIDEO\_QUALITY, 0); intent.putExtra(MediaStore.EXTRA\_SIZE\_LIMIT, sizeLimit); intent.putExtra(MediaStore.EXTRA\_DURATION\_LIMIT, durationLimit); startActivityForResult(intent, REQUEST\_CODE\_TAKE\_VIDEO); **//视频** Intent intent = new Intent(Intent.ACTION\_GET\_CONTENT); intent.setType(contentType); //String VIDEO\_UNSPECIFIED = “video/\*”; Intent wrapperIntent = Intent.createChooser(intent, null); ((Activity) context).startActivityForResult(wrapperIntent, requestCode); **//录音** Intent intent = new Intent(Intent.ACTION\_GET\_CONTENT); intent.setType(ContentType.AUDIO\_AMR); //String AUDIO\_AMR = “audio/amr”; intent.setClassName(“com.android.soundrecorder”, “com.android.soundrecorder.SoundRecorder”); ((Activity) context).startActivityForResult(intent, requestCode); **//拍照 REQUEST\_CODE\_TAKE\_PICTURE 为返回的标识** Intent intent = new Intent(MediaStore.ACTION\_IMAGE\_CAPTURE); //”android.media.action.IMAGE\_CAPTURE”; intent.putExtra(MediaStore.EXTRA\_OUTPUT, Mms.ScrapSpace.CONTENT\_URI); // output,Uri.parse(“content://mms/scrapSpace”); startActivityForResult(intent, REQUEST\_CODE\_TAKE\_PICTURE); 推荐阅读: [Android Intent action有什么用???][Android Intent action] [Intent ACTION值自定义][Intent ACTION] [http_www.g.cn]: http://www.g.cn [http_www.google.com]: http://www.google.com [Android Intent action]: http://blog.csdn.net/h3c4lenovo/article/details/7722259 [Intent ACTION]: http://blog.csdn.net/tangy110/article/details/6415579
还没有评论,来说两句吧...