In Android, data is exposed and received across apps.

Source: Internet
Author: User

In Android, data is exposed and received across apps.

For example, a small project is used to add words in a single word base.

Function: data is exposed and received across apps in different ways.
Exposed app: adds, deletes, modifies, and queries words (Word, and Translate;
Acceptor app: fuzzy query to obtain the data of the exposed end.

Exposed homepage and layout:
1. Layout:
Homepage layout: ListView and TextView (empty)
Item layout: TextView (Word) and TextView (Translation)
Add or modify the Dialog layout of words: EditText (hint = "word"), EditText (hint = "translate ")

2. Menu:
Option menu: add (word)
Context Menu: delete, modify (on ListView)

3. Home page program:
Initialize MySQLiteOpenHelper (Tool class) and control
Database Operations, custom dialog box, set adapter, create option menu, context menu

Acceptor homepage and layout:
1. Layout: homepage layout: EditText (keyword), Button (click to query), ListView, TextView (empty)
Item layout: TextView (Word) and TextView (Translation)
2. Home Page: Initialize the control, parse the String returned collection, and set the adapter

1. Content-Provider


Provider:
1. The custom Provider inherits ContentProvider and overrides 6 abstract methods.
2. Define static code blocks: To define multiple Uri addresses.
3. Declare SQLiteDatabase, MySQLiteOpenHelper, and instantiate in onCreate ()
4. add, delete, modify, and query the rewrite Database: Perform CRUD Based on the return value of the match (uri) method of the UriMatcher object
Note: When querying, the returned Uri object is:
Long id = dbConn. insert ("tb_words", null, values );
Uri uri_id = ContentUris. withAppendedId (uri, id );
5. Register in the list: node: <provider>: authorities, name, exported

Resolver:
1. Define ContentResolver and uriString: authorities defined by the provider
Resolver = getContentResolver ();

2. query: resolver. query (Uri. parse (uri) --> return Cursor

3. If necessary, use the Loader method: to update the provider's data in a timely manner,
① Implement LoaderManager. LoaderCallbacks <Cursor>:
② Initialize LoaderManager:
LoaderManager = getLoaderManager ();
LoaderManager. initLoader (LOADER_ID, null, this );
③ Implement three methods:
OnCreateLoader () {return new CursorLoader (6 parameters )},
OnLoadFinished () {adapter. changeCursor (data )},
OnLoaderReset () {adapter. changeCursor (null )}

Ii. AIDL: Android Interface Define Language


Server:
1. aidl folder --> package --> suffix. aidl --> abstract method. The parameter is the data to be passed, and the String is returned.
2. Custom Service class, inheriting Service
①. In onCreate (), initialize the helper Object
②. The custom internal class inherits the Stub in the Custom aidl and overwrites a custom abstract method, where the database query operation is executed to obtain the set,

The String can be obtained by processing the set appropriately so that the receiver can parse it.
③ In onBind (), the new MyBinder () object is returned.
3. List registration, including <Intent-filter> node <action> attribute: Suggested package name. Class Name

Client:

1. Copy the. aidl file as the first entry on the server. It is not recommended to copy folders.
2. Home Page: declare the custom. aidl interface and bind the service:
BindService (intent, conn, BIND_AUTO_CREATE) --> return boolean. The binding is successful. No
Set up three parameters intent. setPackage, setAction (package name. Class Name), and override the two methods when creating conn,

When establishing a connection: instantiate myInterface to be the same as Stub. asInterface (iBinder). The value of the disconnected connection is null. The third parameter is BIND_AUTO_CREATE.
3. Since myInterface has been instantiated, you can call the method in it to process the returned data and load it to the ListView.
4. Unbind


3. Messenger
Server:
1. Custom Service class inherits Service
2. initialize the declaration of messenger and database operation tool objects
3. In the onCreate () method, initialize the helper Object and messenger object:
Handler handler = new Handler () {// Step 1 and Step 2}
Messenger = new Messenger (handler );
4. Override the onBind () method and return messenger. getBinder ()
5. In Handler {}, perform post-processing on the information transmitted from the client, and return the processed message to the client:
Run the query operation (the query condition is msg. getData (). getString () + "%") to return the set and define the Message. The value of what is 1,

Carrying setData (bundle) and Bundle carrying String
6. define that the messenger object is equal to msg. replyTo, And the messenger object sends the message
7. List registration: contains the <Intent-filter> node <action> attribute: Suggested package name. Class Name

Client:
1. Declare two messenger objects: messenger and messenger_reply;
2. bindMyService is bound to a service;
BindService (intent, conn, BIND_AUTO_CREATE): set three parameters:
Intent. setPackage, setAction (package name. Class Name), and conn creation, rewrite the two methods. When establishing a connection:
Instantiate messenger = new Messenger (service );,
The disconnection value is null. The third parameter is BIND_AUTO_CREATE.
3. initMesengerReply:
Handler handler = new Handler () {// step 3}, messenger_reply = new Messenger (handler );
4. The client sends the request information to the server:
Define Message, msg. setData (bundle), Bundle carries String (query keyword), focus:
Msg. replyTo = messenger_reply, messenger. send (msg)
5. The return messenger processes the returned information: msg. what = 1,
String result = msg. getData (). getString ("result ");
6. The processed result is loaded to the listView control.

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.