Use of the Android URI (Common resource Identifier)

Source: Internet
Author: User

URI one, the usage location generic resource identifier (Universal Resource Identifier, referred to as "URI"). URIs represent the data to be manipulated, and each resource available on Android-images, video clips, and so on-can be represented by URIs. URIs are generally made up of three parts: the naming mechanism for accessing resources. Host name of the storage resource. The name of the resource itself, represented by the path. The URI of Android consists of the following three parts: "content://", the path of the data, the ID (optional), for example, for all contacts uri:content://contacts/people a contact uri:content:// CONTACTS/PEOPLE/5 All pictures uri:content://media/external A picture uri:content://media/external/images/media/4 we often need to parse the URI, And gets the data from the URI. The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis, respectively. While these two classes are not very important, mastering their use will facilitate our development efforts. Let's take a look at the effects of these two classes. Second, in the Intent as a parameter passed 1, open a Web page, the category is Intent.action_view uri uri = uri.parse ("http://www.android-study.com/"); Intent Intent = New Intent (Intent.action_view, URI); 22. Open the map and navigate to a point URI uri = Uri.parse ("geo:52.76,-79.0342"); Intent Intent = new Intent ( Intent.action_view, Uri); 3, open the dial-up interface, type is intent.action_dial uri uri = uri.parse ("tel:10086"); Intent Intent = new Intent ( Intent.action_dial, Uri); 4, direct call, with three different is that this direct call, instead of opening the dial-up interface uri URI = Uri.parse ("tel:10086"); Intent Intent = new Intent (Intent.action_call, URI); 5, uninstall an application, IntentThe category is intent.action_delete uri uri = uri.fromparts ("package", "XXX", null); Intent Intent = new Intent (intent.action_delete , Uri); 6, install the application, Intent category is intent.action_package_added uri uri = uri.fromparts ("package", "XXX", null); Intent Intent = New Intent (intent.action_package_added, URI); 7, play audio file uri uri = uri.parse ("File:///sdcard/download/everything.mp3") ; Intent Intent = new Intent (Intent.action_view, URI); Intent.settype ("Audio/mp3"); 8, open the outgoing mail interface uri uri= uri.parse ("mailto: [email protected] "); Intent Intent = new Intent (intent.action_sendto, URI); 9, e-mail, and eight different here is to send the message out Intent Intent = new Intent (Intent.action_send); String[] tos = {"[email protected]"}; String[] CCS = {"[email protected]"};intent.putextra (Intent.extra_email, TOS); Intent.putextra (Intent.EXTRA_CC , CCS); Intent.putextra (Intent.extra_text, "I come from http://www.android-study.com"); Intent.putextra (Intent.extra_ SUBJECT, "http://www.android-study.com"), Intent.settype ("message/rfc882"); Intent.createchooser (Intent, "ChoOSE email Client ");//Send an email with an attachment? Intent Intent = new Intent (intent.action_send); Intent.putextra (Intent.extra_subject, "The email SUBJECT text"); Intent.putextra (Intent.extra_stream, "File:///sdcard/mysong.mp3"); Intent.settype ("Audio/mp3"); StartActivity ( Intent.createchooser (Intent, "Choose Email Client"), 10. Send SMS Uri uri= uri.parse ("tel:10086"); Intent Intent = new Intent ( Intent.action_view, Uri), Intent.putextra ("Sms_body", "I come from http://www.android-study.com"); Intent.settype (" Vnd. Android-dir/mms-sms "); 11, direct text message Uri uri= uri.parse (" smsto://100861 "); Intent Intent = new Intent (Intent.action_sendto, URI); Intent.putextra ("Sms_body", "3g Android Http://www.android-study.com"), 12, send MMS Uri uri= uri.parse ("content:// Media/external/images/media/23 "); Intent Intent = new Intent (intent.action_send); Intent.putextra (" Sms_body "," 3g Android http://www.android-study.com "); Intent.putextra (Intent.extra_stream, URI); Intent.settype (" image/png "); 13. # Market Related 1//Look for an app uri uri = Uri.parse ("Market://search?q=pname:pkg_name "); Intent it = new Intent (Intent.action_view, URI); startactivity (it);//where Pkg_name is the full package Path for an application 2//displays information about an app uri uri = uri.parse ("market://details?id=app_id"); Intent it = new Intent (intent.act Ion_view, URI); startactivity (it);//where app_id is the application ID, find the id//by clicking on your application on Mar Ket Home//page, and notice the ID from the address Bar14, path plan uri uri = uri.parse ("Http://maps.google.com/maps?f=d&sad Dr=startlat%20startlng&daddr=endlat%20endlng&hl=en "); Intent it = new Intent (Intent.action_view, URI); StartActivity (it);//where Startlat, STARTLNG, Endlat, ENDLNG is a long with 6 decimals like:50.12345615, installation specified apk public v    OID setupapk (String apkname) {string fileName = Environment.getexternalstoragedirectory () + "/" + apkname;    Intent Intent = new Intent (Intent.action_view);    Intent.setdataandtype (Uri.fromfile (New File), "application/vnd.android.package-archive"); Mservice.startacTivity (intent);} 16. Enter the contact page Intent Intent = new Intent (); intent.setaction (Intent.action_view); Intent.setdata (People.content_uri); StartActivity (intent); 17. View the specified contact Uri Personuri = Contenturis.withappendedid (People.content_uri, info.id);// Info.id Contact person Idintent Intent = new Intent (); intent.setaction (Intent.action_view); Intent.setdata (Personuri); StartActivity (intent); 18, call album public static final String mime_type_image_jpeg = "image/*";p ublic static final int Activit Y_get_image = 0;intent GetImage = new Intent (intent.action_get_content); Getimage.addcategory (Intent.category_ openable); Getimage.settype (mime_type_image_jpeg); Startactivityforresult (GetImage, activity_get_image); 19, Call the system camera application and save the captured photo Intent Intent = new Intent (mediastore.action_image_capture); time = Calendar.getinstance (). Gettimeinmillis (); Intent.putextra (Mediastore.extra_output, Uri.fromfile (new File) ( Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/tucue", Time + ". jpg")); Startactivityforresult ( Intent, Activity_get_caMera_image) Urimatcher and Contenturis in Android are the tool classes used to manipulate URIs, respectively, Urimatcher and Contenturis. The urimatcher is used to match URIs. Use the following: First of all you need to match the URI path to the registration: After registering the URI to match, you can use the Smatcher.match (URI) method to match the input URI, if the match will return the match code, the match code is called Adduri () The third parameter passed in by the method, such as a matching Content://com.test.provider.personprovider/person path, returns a match code of 1. The constant Urimatcher.no_match represents a return code that does not match any path urimatcher surimatcher = new Urimatcher (urimatcher.no_match);//Add a URI that needs to be matched, If the match will return a match code//if the match () method matches the Content://com.test.provider.personprovider/person path, the return match code is 1 Surimatcher.adduri (" Com.test.provider.personprovider "," person ", 1);//if the match () method matches Content://com.test.provider.personprovider/person /530 path, return match code for 2//#号为通配符 Surimatcher.adduri ("Com.test.provider.personprovider", "person/#", 2); switch ( Surimatcher.match (Uri.parse ("CONTENT://COM.TEST.PROVIDER.PERSONPROVIDER/PERSON/10"))) {Case 1, case 2 break; default://does not match break; The}contenturis class is used to get the ID part after the URI path: 1, Contenturis.withappendedid (URI Contenturi, long ID) is used to add the ID portion of the path: URI uri = Uri.parse (" Content://com.test.provider.personprovIder/person ") Uri Resulturi = Contenturis.withappendedid (URI, 5);//The resulting URI is: content:// COM.TEST.PROVIDER.PERSONPROVIDER/PERSON/5 The result is equivalent to Uri.withappendedpath (Uri BaseUri, String pathsegment) Uri Resulturi = Uri.withappendedpath (URI, "5"); 2, Contenturis.parseid (URI) method is used to get the ID part from the path: URI uri = Uri.parse ("content:// COM.TEST.PROVIDER.PERSONPROVIDER/PERSON/5 ") Long PersonID = Contenturis.parseid (URI);//Gets the result of : 5ContentResolver uses URIs to add, delete, modify, and query the data provided in ContentProvider. In addition to the URI, you must know the name of the data segment you want to get, and the data type of the data segment. If you need to get a specific record, you have to know the ID of the current record, which is part D of the URI. You can use the Getcontentresolver () method provided by the activity. Contentresolver uses the INSERT, delete, update, and query methods to manipulate the data. 1. Final Cursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) query the GI Ven URI, returning a Cursor over the result set. 2. Final URI insert (URI URL, contentvalues values) inserts a row into a table at the given URL. 3. Final int update (URI uri, contentvalues values, String where, string[] selectionargs) update row (s) in a content URI.4, Final int delete (Uri URL, String where, string[] selectionargs) deletes row (s) specified by a content Uri. 

Use of the Android URI (Common resource Identifier)

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.