Android instance profiling notes notepad

Source: Internet
Author: User
Tags creative commons attribution

Kaiqiyu

As the saying goes, "regular reading of three hundred Tang poems will not make poems, but will also sing ". Recently, I have collected a lot of sample code for Android. I learned a lot from the reading and Experiment of the Code to develop my plan for writing this series. The goal is to implement it step by step with the instance, learn about Android development from "doing.
This is the first article in this series. The goal is to build an example program for Android: notepad, which will be divided into four articles for detailed introduction.

Prerequisites

Set up a development environment and try to write "Hello World" to understand the basic concepts of Android and be familiar with Android APIs (available in official documents. Do not repeat them here ).

Program

First, let's take a look at the program running effect.

Program entry point

Similar to the winmain function in Win32 programs, Android naturally has its program entry point. It is specified through configuration in the androidmanifest. xml file. You can see that there is such an intent-filter under the activity node named noteslist, and its action is Android. Intent. Action. Main,

Category is specified as Android. intent. category. launcher, which indicates that this activity is used as the entry activity. After the system finds it, it will create this activity instance for running, if not found, it will not be started (you can change the main name to try ).

<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>

NoteslistDetails

From the activity (see Figure 1) where the entry point is located, you can see that the most important function of this activity is to display the log list. Logs of this program are stored in the SQLite database. Therefore, you need to read and display all log records.

First, let's look at two important private data: the firstProjectionThe field specifies the fields in the database that the "log list" is concerned with (that is, only the ID and title are required ).

Private Static final string [] projection = new string [] {
Notes. _ id, // 0
Notes. Title, // 1
};

Second FieldColumn_index_titleSpecifies the index of the title field in the data table.

Private Static final int column_index_title = 1;

Then go to the oncreate function called first.

Intent intent = getintent ();
If (intent. getdata () = NULL)
{
Intent. setdata (notes. content_uri );
}

Because the noteslist activity is called by the system, the intent does not contain data and operation types. The system only specifies that the target component is notelist, so here we set "content: // COM. google. provider. notepad/Notes is saved to intent. This URI specifies the name of the data table in the database (see the notepadprovider class in the future), that is, the notes of the data table that saves logs.

Cursor cursor = managedquery (getintent (). getdata (), projection, null, null, notes. default_sort_order );

Then call the managedquery function to query all the log information. The first parameter here is the content: // COM. google. provider. notepad/Notes "URI, that is, the notes data table. The projection field specifies the fields required in the results, and notes. default_sort_order specifies the sorting rules for the results. In fact, managedquery does not directly query the database, but uses the content provider to perform actual database operations. In this way, the logical layer and the database layer are separated.

Simplecursoradapter adapter = new simplecursoradapter (this, R. layout. noteslist_item, cursor,
New String [] {notes. Title}, new int [] {Android. R. Id. text1 });
Setlistadapter (adapter );

After querying the log list, construct a cursoradapter and use it as the data source of the List View to display the log list on the interface. The second parameter is R. layout. noteslist_item. Open the corresponding noteslist_item.xml file,

<Textview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Id = "@ Android: ID/text1"
Android: layout_width = "fill_parent"
Android: layout_height = "? Android: ATTR/listpreferreditemheight"
Android: textappearance = "? Android: ATTR/textappearancelarge"
Android: gravity = "center_vertical"
Android: paddingleft = "5dip"
Android: singleline = "true"
/>

It is used to display the textview of a log record. The last two fields indicate the actual field ing relationship. This textview is used to display the title field of a log record.

Process "Select log" Event

With the "log list", we naturally need to consider how to handle the Click Event of a log. This is done by reloading the onlistitemclick method,

@ Override
Protected void onlistitemclick (listview L, view V, int position, long ID ){
Uri uri = contenturis. withappendedid (getintent (). getdata (), ID );

String action = getintent (). getaction ();
If (intent. action_pick.equals (Action) | intent. action_get_content.equals (Action )){
// The caller is waiting for us to return a note selected
// The user. The have clicked on one, so return it now.
Setresult (result_ OK, new intent (). setdata (URI ));
} Else {
// Launch activity to View/Edit the currently selected item
Startactivity (new intent (intent. action_edit, Uri ));
}
}

First, use "content: // COM. google. provider. notepad/notes and the log ID are spliced to obtain the real URI of the selected log, and then a new intent is created. The operation type is intent. action_edit: indicates the log URI to be edited in the data field (only else blocks are analyzed here ).

Intent deep profiling

The above startactivity (NewIntent (intent.Action_edit(URI) what will happen after execution? At this time, the android system will jump out and take over. It will find the corresponding activity based on the intent information. Here we find the activity noteeditor, and then create and run the activity instance.

Then, how does Android find the corresponding activity of noteeditor? This is the time when intent plays a role.

New intent (intent. action_edit, Uri)

Intent.Action_edit= "Android. Intent. Action. Edit". In addition, by setting a breakpoint, we can see the URI value here:

The URI of the selected log entry is: Content: // com. Google. provider. notepad/Notes/1.

Next let's take a look at androidmanfest. XML, which contains the provider

<Provider Android: Name = "notepadprovider"
Android: Authorities = "com. Google. provider. Notepad"
/>

Found? It also has com. Google. provider. notepad, which is part of content: // com. Google. provider. notepad/Notes/1.

<Activity Android: Name = "noteeditor"
Android: theme = "@ Android: style/theme. Light"
Android: Label = "@ string/title_note"
Android: screenorientation = "sensor"
Android: configchanges = "keyboardhidden | orientation"
>
<! -- This filter says that we can view or edit the data
A single note -->
<Intent-filter Android: Label = "@ string/resolve_edit">
<Action Android: Name = "android. Intent. Action. View"/>
<Action Android: Name = "android. Intent. Action. Edit"/>
<Action Android: Name = "com. Android. notepad. Action. edit_note"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "Vnd. Android. cursor. Item/vnd. Google. Note"/>
</Intent-filter>
<! -- This filter says that we can create a new note inside
Of a directory of notes. -->
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Insert"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "Vnd. Android. cursor. DIR/vnd. Google. Note"/>
</Intent-filter>
</Activity>

In the first intent-filter above, there is an action named Android. Intent. Action. edit, and the intent we created earlier is exactly

Intent. action_edit = "android. Intent. Action. Edit", you must have understood what is going on.

The following describes the activity selection mechanism:

The system obtains the content: // COM. google. provider. notepad/Notes/1, remove the starting content: identifier, and get COM. google. provider. notepad/Notes/1, and then get the com. google. provider. notepad, and then go to androidmanfest. in XML, the authorities is com. google. provider. notepad provider. This is the contentprovider to be discussed later, and then the content provider is loaded.

<Provider Android: Name = "notepadprovider"
Android: Authorities = "com. Google. provider. Notepad"
/>

Here is notepadprovider, then call the GetType function of notepadprovider, and pass the URI above to this function. The function returns the type of the URI (here the return notes. content_item_type indicates a log record, while content_item_type = "Vnd. android. cursor. item/vnd. google. note ").

@ Override
Public String GetType (URI ){
Switch (surimatcher. Match (URI )){
Case Notes:
Return notes. content_type;
Case note_id:
Return notes. content_item_type;
Default:
Throw new illegalargumentexception ("unknown Uri" + URI );
}
}

The surimatcher. Match above is used to check whether the URI can be processed, andSurimatcherThe returned value of. Match (URI) is actually composed

Surimatcher = new urimatcher (urimatcher. no_match );
Surimatcher. adduri (Notepad. Authority, "notes", notes );
Surimatcher. adduri (Notepad. Authority, "notes/#", note_id );

.

Then the system uses the obtained "Vnd. android. cursor. item/vnd. google. note "and" android. intent. action. edit "to androidmanfest. find the matching activity in XML.

<Intent-filter Android: Label = "@ string/resolve_edit">
<Action Android: Name = "android. Intent. Action. View"/>
<Action Android: Name = "android. Intent. Action. Edit"/>
<Action Android: Name = "com. Android. notepad. Action. edit_note"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Data Android: mimetype = "Vnd. Android. cursor. Item/vnd. Google. Note"/>
</Intent-filter>

The intent-filter of the noteeditor activity meets the preceding conditions, so that the noteeditor is found. Then the system loads this class, instantiates it, runs it, and then goes to the oncreate function of noteeditor (see subsequent articles ).

 

Tips

1. Run the "ADB shell" command in the command line to enter the system, and then "CD app" to enter the directory where the application is located. "rm xxx" will delete the specified APK, remove the icon occupied by the system top-level interface. If "CD Data" is used twice, you can enter the data directory used by the application. Your data can be saved here, for example, notepad stores its database in its databases directory named note_pad.db.

2. It will be slow to start the simulator for the first time, but do not shut down the simulator in the future. You do not need to modify the code or start the simulator again for debugging. After you directly modify the code, run or debug the simulator.

Author: phinecos)
Source: http://phinecos.cnblogs.com/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it, but please keep this statement and provide the original article connection clearly on the article page.

Author: Dongting sangren

Source: http://phinecos.cnblogs.com/

This blog complies with the Creative Commons Attribution 3.0 License. If it is used for non-commercial purposes, you can reprint it freely, but please keep the original author information and article URL.
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.