File Association for Android applications

Source: Internet
Author: User

For example, to implement File Association, you must first modify AndroidManifest. xml. The example is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
 
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
 
Package = "com. android. sample" android: versionCode = "1"
 
Android: versionName = "1.0" type = "codeph" text = "/codeph">
 
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name"
 
Android: debuggable = "true">
 
<Activity android: name = ". MainActivity"
 
Android: configChanges = "orientation | keyboardHidden | navigation"
 
Android: label = "@ string/app_name">
 
<Intent-filter>
 
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
 
</Intent-filter>
 
</Activity>
 
<Activity android: name = ". OtherActivity"
 
Android: configChanges = "orientation | keyboardHidden | navigation"/>
 
<Activity android: name = ". AssociatedActivity"
 
Android: configChanges = "orientation | keyboardHidden | navigation">
 
<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>
 
</Activity>
 
</Application>
 
<Uses-sdk android: minSdkVersion = "8"/>
 
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>
 
<Uses-permission android: name = "android. permission. INTERNET"/>
 
</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 {
 
 
 
/** Called when the activity is first created .*/
 
@ Override
 
Public void onCreate (Bundle savedInstanceState ){
 
Super. onCreate (savedInstanceState );
 
SetContentView (R. layout. main );
 
 
 
Intent intent = getIntent ();
 
String action = intent. getAction ();
 
If (intent. ACTION_VIEW.equals (action )){
 
Uri uri = (Uri) intent. getData ();
 
String filename = uri. getPath ();
 
Log. e ("info", filename );
 
}
 
}
 
}
 
Here, the action mainly determines whether the VIEW action corresponds to android. intent. action. VIEW in xml. You can obtain the passed parameters through intent. getData, that is, the clicked file.

From chenghai2011

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.