To implement File Association, first modify androidmanifest. xml. The androidmanifest example is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. android. sample "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" <br/> Android: debuggable = "true"> <br/> <activity Android: name = ". mainactivity "Android: configchanges =" orientation | keyboardhidden | navigation "Android: label = "@ string/app_name"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: Name = ". otheractivity "Android: configchanges =" orientation | keyboardhidden | navigation "/> <br/> <activity Android: Name = ". associatedactivity "Android: configchanges =" orientation | keyboardhidden | navigation "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. view "/> <br/> <category Android: Name =" android. intent. category. default "/> <br/> <data Android: mimetype =" text/plain "> </data> <br/> <data Android: mimetype = "application/Epub + Zip"> </data> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "8"/> <br/> <uses-Permission Android: Name = "android. permission. write_external_storage "/> <br/> <uses-Permission Android: Name =" android. permission. internet "/> <br/> </manifest>
In this example, there are two "intent-filter", the first is the first activity started by the application, and the second "intent-filter" is used to implement File Association.
<Intent-filter>
<Action Android: Name = "android. Intent. Action. View"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "text/plain"> </data>
<Data Android: mimetype = "application/Epub + Zip"> </data>
</Intent-filter>
In this XML section, the program is registered to be associated with the TXT and Epub file formats. If JPEG is to be associated, mimetype is changed to: "image/JPEG", and the association with all files is changed: "*/*". in this way, when you click related files in the File Manager, the system will attempt to execute your program.
Public class associatedactivity extends activity {</P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> intent = getintent (); <br/> string action = intent. getaction (); <br/> If (intent. action_view.equals (Action) {<br/> URI uri = (URI) intent. getdata (); <br/> string filename = Uri. getpath (); <br/> log. E ("info", filename); <br/>}< br/>}
Here, the action mainly determines whether it comes from the view action, which corresponds to Android. Intent. Action. View in XML. You can use intent. getdata to obtain the passed parameters, that is, the clicked file.
For more information about MIME type, see http://en.wikipedia.org/wiki/MIME_type.