This article is to master the use of implicit intent in Android development by completing a real-combat task.
Task : Let's say we've implemented a video player (playeractivity), and we want to register it with the system and start the video player when the user clicks on a local video or online video.
( Assuming that the full path of the class is: com.jhuster.videoplayer.PlayerActivity)
[Note]: Complete sample code for this article please go to my github download, address: Videoplayer
1. What is an implicit Intent?
Intent is a relatively important component of Android, used to launch a new activity or service, broadcast an event, and pass data between Android components. There are two ways to start a new activity or service through intent, one is to show startup and the other is to start implicitly.
Display startup is the class or package name that explicitly indicates the activity or service to start. For example:
Intent Intent = Newintent (this, playeractivity.class); StartActivity (Intent); Intent Intent = new Intent (); Intent.setclass (This,playeractivity.class); StartActivity (Intent); Intent Intent = new Intent () intent.setclassname ("Com.jhuster.videoplayer", "com.jhuster.videoplayer.PlayerActivity ”); StartActivity (Intent);
Implicit start is not explicitly specifying which activity or service to start, but by setting the action, Data, Category, the system to filter out the appropriate target.
For example, call:
Intent Intent = new Intent (Intent.action_dial,uri.parse ("tel:021-80961111")); StartActivity (Intent);
After the system receives an implicit start request, it compares and determines whether the current intent request is matched according to the <intent-filter> of each activity in the system that is declared in the Androidmanifest.xml file.
Therefore, if we want playeractivity to be able to be implicitly initiated by the system, you first need to add <intent-filter> to the activity in the Androidmanifest.xml file.
2. Add <intent-filter> for Playeractivity
<intent-filter> has a lot of tags, just to introduce and add the most basic and most commonly used three tags, respectively <action>,<category> and <data>.
2.1 Add <action>
This tag must be added, you can define it yourself, or you can use a predefined variable, and the Android system defines many actions by default, either by looking at the SDK documentation, or by Google "android.intent.action."
Here, because our class is used to "play video", you can use the system pre-defined: Android.intent.action.VIEW, which indicates that you need to start an activity to display the specified data (including pictures, videos, documents, and so on).
Added <action> after <activity> is as follows:
<activity android:name= "com.jhuster.videoplayer.PlayerActivity" > <intent-filter> <action android: Name= "Android.intent.action.VIEW"/> </intent-filter> </activity>
2.2 Add <category>
Category represents categories that define the activity category, and activity can set one or more category labels. Usually there are 3: Default,home,launcher
Default action home set to local desktop app launcher launch activity for this app
In this application we use the default category, and default is the most common option for category.
After the category has been added <activity> is as follows:
<activityandroid:name= "Com.jhuster.videoplayer.PlayerActivity" > <intent-filter> <actionandroid:na Me= "Android.intent.action.VIEW"/> <categoryandroid:name= "Android.intent.category.DEFAULT"/> & Lt;/intent-filter> </activity>
2.3 Add <data>
Data is the most complex label in <intent-filter>, because different activity supports a variety of data sources and types, so it needs to be specified with detailed data label information.
The data tag has many properties, including:
Android:host: Specify the hostname, for example: Google.comandroid:port: Establish a host port, for example: 80android:path: Specify a valid path value for the URL, for example:/index/examplesandroid: MimeType: Specifies the type of data that the component can perform, for example: Image/jpeg,video/*android:scheme: Specifies a specific pattern, for example: Content,http
Here, suppose our video player supports a variety of data sources, including: Local video files, local media URLs, network video streaming (HTTP, RTMP, RTSP protocol), and suppose our video player only supports MP4 and 3GPP two file formats.
So, let's add two of the most commonly used <data> tags, scheme and mimetype, and explain what kind of data source or data format each tag corresponds to.
(1) <data android:scheme= "xxx"/>
Here xxx can be: file,content, network Protocol (HTTP,RTMP, RTSP, etc.)
In this application we add to the <Intent-filter> of playeractivity:
<data android:scheme= "file"/><data android:scheme= "content"/><data android:scheme= "http"/>< Data android:scheme= "RTSP"/>
After adding a few data label entries, if the source URL in the implicit intent is a URL resource that begins with "file://", "content://", "http.//", "rtsp://", Will implicitly start our playeractivity.
For example, other activity can use the following method to implicitly launch our playeractivity.
Intent Intent = new Intent (Intent.action_view); Intent.setdata (Uri.fromfile (New File ("/sdcard/test.3gp")); StartActivity (intent);
Uri.fromfile This statement converts the specified file location to a URI object that begins with "file://", as the above example finally gets the URL: "FILE:///SDCARD/TEST.3GP"
Similarly, we can convert our common network address strings to URI objects by Uri.parse, for example:
Intent Intent = new Intent (Intent.action_view); Intent.setdata (Uri.parse ("Http://ticktick.blog.51cto.com/test.mp4")); StartActivity (intent);
(2) <data android:mimetype= "xxx"/>
MimeType is used to set the data type, such as data (Image/png or image/*), video data (Video/mp4 or video/*), if using * means matching all sub-types.
MIME type is the standard of a tagged data type for the internet and now supports a very wide number of types, here I do not enumerate, you can search on Google.
In this application we assume that we need to support two types of MP4 and 3GPP, so we can add two mimeType:
<data android:mimetype= "VIDEO/3GPP"/><data android:mimetype= "Video/mp4"/>
Then, the other activity can use the following method to implicitly launch our playeractivity. Note that after <Intent-filter> has added mimetype, the implicit Intent must set the type parameter to match the activity, so the Setdataandtype method is recommended. Rather than a single SetData method.
Intent Intent = new Intent (Intent.action_view); Intent.setdataandtype (Uri.fromfile (New File ("/sdcard/test.3gp")), "VIDEO/3GPP"); StartActivity (intent);
Of course, the "VIDEO/3GPP" here can also be written as: "video/*", but this may be matched to some players that do not support 3GPP.
(3) Summary
After adding the <data> tag, <intent-filter> is as follows:
<activity android:name= "Com.jhuster.videoplayer.PlayerActivity" > < Intent-filter> <action android:name= " Android.intent.action.VIEW " /> <category Android:name= "Android.intent.category.DEFAULT" /> < Data android:scheme= "File"/> <data android: Scheme= "Content"/> <data android:scheme= "http"/> <data android:scheme= "RTSP"/> <data android:scheme= "rtmp"/> <data android:mimetype= "VIDEO/3GPP" /> <data android:mimetype= "Video/mp4" /> </intent-filter> </activity>
3. Getting parameters in Playeractivity
With the introduction above, we already know how to add <intent-filter> and how to invoke our playeractivity through implicit intent, so Below we also learn how to parse parameters from an implicit intent in playeractivity .
In fact, intent provides a number of ways to get related parameter information, such as:
Public String getaction ();p ublic Uri getData ();p ublic string Getscheme ();p ublic string GetType ();
The above methods can obtain intent Action,data uri,scheme and mimetype values respectively.
For the URI object beginning with "file://", we can get the specific file address of the "file://" prefix by Uri.getpath method. For example, "File:///sdcard/test.mp4" can be converted to the actual "/sdcard/test.mp4".
For network streams, such as the URI that begins with "http:/", "rtsp://", you can directly convert the string to the actual address through the ToString () method.
The URI object that starts with "content://" is usually retrieved from the system's media database, so a reverse lookup is needed to get the actual file address, which provides a function to convert.
Public static string getvideopath (Context context, uri uri) { Uri videopathURI = uri; if (Uri.getscheme (). toString (). CompareTo ("content") == 0 ) { Cursor cursor = Context.getcontentresolver (). Query (Uri, null, null, null, null); if (Cursor.movetofirst ()) { int column_index = cursor.getcolumnindexorthrow ( MediaStore.Video.Media.DATA); Videopathuri = uri.parse (cursor.getstring (column_index)); return videopathuri.gEtpath (); } } else if (Uri.getscheme (). CompareTo ("file") == 0 ) { return videopathuri.getpath (); } return videopathuri.tostring ();}
4. Summary
To here is the implicit intent important knowledge points are clear, the sample code of this article please to my GitHub download (address at the beginning of this article) have unclear or wrong place, welcome to message discussion or letter [email protected] exchange. In addition reprint please famous this article author and provenance (Ticktick), thank you.
This article is from the "Shadow Three People" blog, please be sure to keep this source http://ticktick.blog.51cto.com/823160/1621957
Android Development Practice: The use of implicit intent in real-combat drills