What is Uri in Android?

Source: Internet
Author: User

I studied the notepad example provided by the SDK a few days ago. From this example, I can see that intent redirection is used between activities, but I didn't understand what URI is. Today, when I was reading proandroid, I was inspired by the author and finally understood what URI is.

Take this notepad as an example,

 

There are two definitions in Notepad. Java:

Public static final stringAuthority= "Com. Google. provider. Notepad ";

Public static final URIContent_uri=
Uri. parse ("content: //" +Authority+ "/Notes ");

 

Original words of the author:

Actually, the URI, or the authority portion of the URI, is configured in the androidmanifest. xml file as a content provider:
<Provider Android: Name = "notepadprovider"
Android: Authorities = "com. Google. provider. Notepad"/>
When Android sees a URI that needs to be resolved, it pulls out the authority portion of it and looks up the contentprovider class configured for the Authority. in the notepad application, the androidmanifest. XML file contains a class called notepadprovider configured for the com. google. provider. notepad authority.

 

The author means that when Android wants to process operations related to a URI, the android framework will be in androidmanifest according to the authority content in the URI. in XML <provider> (one or more), find the provider with the same authority content and match the two. In the notepad exampleContent_uriCorresponds to the notepadprovider class.

I have set several breakpoints and debugged them to verify that the URI parameter of managedquery () in the following code is passed to the query () in the notepadprovider class during the transfer process () method Processing !! ^

 

// Noteslist. Java

Public class noteslist extends listactivity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setdefakeykeymode (default_keys_shortcut );
Intent intent = getintent ();
If (intent. getdata () = NULL ){
Intent. setdata (notes. content_uri); // set the URI to be processed by intent.
}
Getlistview (). setoncreatecontextmenulistener (this );

// The first parameter of managedquery () is the URI set above. I want to track it here
Cursor cursor = managedquery (getintent (). getdata (), projection, null, null, notes. default_sort_order );
Simplecursoradapter adapter = new simplecursoradapter (this,
R. layout. noteslist_item, cursor, new string [] {notes. Title },
New int [] {Android. R. Id. text1 });
Setlistadapter (adapter );
}
}

 

When the program runs, it traces to managedquery () and jumps

Contentresolver. Class

Public final cursor query (URI Uri, string [] projection,
String selection, string [] selectionargs, string sortorder ){
Icontentprovider provider = acquireprovider (URI); // This is the key. The contentresolver query obtains the provider according to the URI!

If (provider = NULL ){
Return NULL;
}
Try {
Cursor qcursor = provider. Query (Uri, projection, selection, selectionargs, sortorder); // reverse call in the legend!
If (qcursor = NULL ){
Releaseprovider (provider );
Return NULL;
}
// Wrap the cursor object into cursorwrapperinner object
Return new cursorwrapperinner (qcursor, provider );
}

..

}

The program continues to run. If you set a breakpoint in the query in notepadprovider. Java, you will find that the program stops at the breakpoint.

 

This proves that the android framework finds the corresponding provider based on the authority in the URI, and then processes the provider in detail based on the complete URI content, such as updating a record in the database.

 

 

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.