Use of Android URI (Universal Resource Identifier)

Source: Internet
Author: User
Tags response code

Use of Android URI (Universal Resource Identifier)

Uri 1. Universal Resource Identifier ("URI "). Uri indicates the data to be operated. Each type of resource available on Android-images and video clips can be represented by Uri. A URI is generally composed of three parts: the naming mechanism for accessing resources. Host Name for storing resources. The name of the resource, represented by the path. The Uri of Android consists of three parts: "content: //", data path, and ID (Optional). For example, the Uri of all contacts: content: // contacts/people the Uri of a contact: content: // contacts/people/5 all images Uri: content: // media/external the Uri of an image: content: // media/external/images/media/4 we often need to parse the Uri and obtain data from the Uri. The Android system provides two tool classes for Uri operations: UriMatcher and ContentUris. Although these two types are not very important, understanding their usage will facilitate our development work. Let's take a look at the functions of these two classes. 2. pass as a parameter in INTENT. 1. Open a webpage. the category is Intent. ACTION_VIEW Uri uri = Uri. parse ("http://www.android-study.com/"); Intent intent = new Intent (Intent. ACTION_VIEW, uri); 2. Open the map and locate a vertex Uri uri = Uri. parse ("geo: 52.76,-79.0342"); Intent intent = new Intent (Intent. ACTION_VIEW, uri); 3. Open the dialing interface. The type is Intent. ACTION_DIAL Uri uri = Uri. parse ("tel: 10086"); Intent intent = new Intent (Intent. ACTION_DIAL, uri); 4. Call the phone number directly. Instead of opening the dialing interface Uri uri = Uri. parse ("tel: 10086"); Intent intent = new Intent (Intent. ACTION_CALL, uri); 5. Uninstall an application. The Intent category is Intent. ACTION_DELETE Uri uri = Uri. fromParts ("package", "xxx", null); Intent intent = new Intent (Intent. ACTION_DELETE, uri); 6. Install the application. The 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/everythinghistory"); Intent intent = new Intent (Intent. ACTION_VIEW, uri); intent. setType ("audio/mp3"); 8. open the mail interface Uri uri = Uri. parse ("mailto: admin@android-study.com"); Intent intent = new Intent (Intent. ACTION_SENDTO, uri); 9. send an email. Different from sending an email, Intent intent = new Intent (Intent. ACTION_SEND); String [] tos = {"admin@android-study.com"}; String [] ccs = {"webmast Er@android-study.com "}; 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 a text message Uri uri = Uri. parse ("tel: 10086"); Intent intent = new Intent (Intent. ACTION_VIEW, uri); intent. putExtra ("sms_body", "I come f Rom http://www.android-study.com "); intent. setType ("vnd. android-dir/mms-sms "); 11. send a 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 and Roid http://www.android-study.com "); intent. putExtra (Intent. EXTRA_STREAM, uri); intent. setType ("image/png"); 13, # Market related 1 // search for an application 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 application Uri = uri. parse ("market: // details? Id = app_id "); Intent it = new Intent (Intent. ACTION_VIEW, uri); startActivity (it); // where app_id is the application ID, find the ID // by clicking on your application on Market home // page, and notice the ID from the address bar14, Path Planning Uri uri = Uri. parse ("http://maps.google.com/maps? F = d & saddr = startLat % 20 startLng & daddr = endLat % 20 endLng & hl = en "); Intent it = new Intent (Intent. ACTION_VIEW, uri); startActivity (it); // where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.12345615, install specified apk public void setupAPK (String apkname) {String fileName = Environment. getExternalStorageDirectory () + "/" + apkname; Intent intent = new Intent (Intent. ACTION_VIEW); intent. setDataAn DType (Uri. fromFile (new File (fileName), "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 IDIntent intent = new Intent (); intent. setAction (Intent. ACTION_VIEW); intent. setData (personUri); startActivity (intent); 18. Call the album public static final String MIME_TYPE_IMAGE_JPEG = "image/*"; public static final int ACTIVITY_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 store the Inten pictures taken. T 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 tool classes used to operate Uris, respectively UriMatcher and ContentUris. UriMatcher is used to match a Uri. Usage: First, you need to match all the Uri paths to the Registration: After registering the Uri to be matched, you can use sMatcher. the match (uri) method matches the input Uri. If it matches, the matching code is returned. The matching code is the third parameter passed in by calling the addURI () method, for example, matching content: // com. test. provider. personprovider/person path. The returned matching code is 1. // Constant UriMatcher. NO_MATCH indicates that the response code UriMatcher sUriMatcher = new UriMatcher (UriMatcher. NO_MATCH); // Add the uri to be matched. If matched, the matching code will be returned. // If the match () method matches content: // com. test. provider. personprovider/person path, returns the matching code 1 sUriMatcher. addURI ("com. test. provider. personprovider "," person ", 1); // If the match () method matches content: // com. test. provider. personprovider/person/530 path, returns the matched code 2 // # As the wildcard sUriMatcher. addURI ("com. test. provider. personprov Ider "," person/# ", 2); switch (sUriMatcher. match (Uri. parse ("content: // com. test. provider. personprovider/person/10 ") {case 1 break; case 2 break; default: // does not match break;} The ContentUris class is used to obtain the ID part after the Uri path: 1. ContentUris. withAppendedId (Uri contentUri, long id) is used to add the ID part to the path: Uri uri Uri = Uri. parse ("content: // com. test. provider. personprovider/person ") Uri resultUri = ContentUris. withAppendedId (uri, 5); // The generated Uri is: conte Nt: // com. test. provider. the result of personprovider/person/5 is equivalent to Uri. withAppendedPath (Uri baseUri, String pathSegment) Uri resultUri = Uri. withAppendedPath (uri, "5"); 2. ContentUris. the parseId (uri) method is used to obtain the ID part from the path: Uri uri = Uri. parse ("content: // com. test. provider. personprovider/person/5 ") long personid = ContentUris. parseId (uri); // The result is: 5ContentResolver uses URI to add, delete, modify, and query the data provided in ContentProvider. In addition to URI, you must also know the name of the data segment to be obtained and the Data Type of the Data Segment. If you need to obtain a specific record, you must know the ID of the current record, that is, the D part of the URI. You can use the getContentResolver () method provided by Activity. ContentResolver uses the insert, delete, update, and query methods to operate data. 1. final Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) Query the given 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.

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.