Enable exposure and reception of data across apps in Android

Source: Internet
Author: User

For example, a small project: the ability to add words to a word book

Features: Different ways to achieve data exposure and reception across apps
Exposed app: To add words (Word, Translate), adding and deleting;
Receiver app: Fuzzy query to get the data from the exposed end.

Exposed home page and layout:
1. Layout:
Home layout: ListView, TextView (empty)
Item layout: TextView (Word), TextView (translation)
Add or modify a Word's dialog layout: EditText (hint= "word"), EditText (hint= "translate")

2. Menu:
Options menu: Add (Word)
Context Menu: Delete, modify (on a ListView)

3. Homepage Program:
Initialize Mysqliteopenhelper (tool Class), initialize control
Database operations, custom dialog boxes, setting adapters, creating options menus, context menus

Receiving end home page and layout:
1, Layout: Home layout: EditText (keywords), Button (click Query), ListView, TextView (empty)
Item layout: TextView (Word), TextView (translation)
2. Home: Initialize the control, parse the resulting string back into the collection, set the adapter

First, Content-provider


Provider:
1. Custom provider inheritance ContentProvider, overriding 6 abstract methods
2. Define static blocks of code: The purpose is to define multiple URI addresses.
3, declaration Sqlitedatabase, Mysqliteopenhelper, life cycle OnCreate (), instantiation
4. Rewrite database additions and deletions: crud based on the return value of the match (URI) method of the Urimatcher object
Note: When querying, the URI object is returned:
Long id = dbconn.insert ("Tb_words", null,values);
Uri uri_id = Contenturis.withappendedid (Uri,id);
5. Register in List: node: <provider>:authorities, Name, exported

Resolver:
1, definition Contentresolver, and uristring: provider-defined authorities
Resolver=getcontentresolver ();

2. Query: Resolver.query (Uri.parse (URI)-->> return cursor

3, if there is a need, you can use the Loader method: When the provider data changes in time to update,
①, realize 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)}

Second, aidl:android Interface Define Language


Service side:
1. Aidl folder--------.aidl--> abstract method, parameter is the data to be passed, return string
2. Custom service classes, inheriting services
①, OnCreate (), Initialize helper objects
②, custom inner classes inherit the stub from the custom Aidl, overriding a custom abstract method that performs a database check operation, gets the collection,

The appropriate handling of the collection gets a string so that the recipient can parse
③, Onbind (), returns the new Mybinder () object
3. manifest registration, including <Intent-filter> node <action> properties: Recommended package name. Class Name

Client:

1, with the first server, you can copy. aidl files, do not recommend copying folders
2. Home page: Declare a custom. Aidl interface, bind the service:
Bindservice (Intent, Conn, bind_auto_create)--return Boolean, Bind succeeded No
Set up three parameters Intent.setpackage, Setaction (package name, class name), create conn, override two methods,

When establishing a connection: instantiating MyInterface equals Stub.asinterface (IBinder), NULL for disconnection, third parameter recommended for Bind_auto_create
3, since MyInterface has been instantiated, you can call the inside of the method, processing the returned data, loaded onto the listview
4. Unbind


Third, Messenger
Service side:
1. Custom Service classes Inherit services
2. Initialize the declaration of the Messenger, database Operation Tool Class object
3, Cycle Method OnCreate (): Initialize helper object, Messenger object:
Handler Handler = new Handler () {//5th step, 6th step}
Messenger = new Messenger (handler);
4. Rewrite Onbind () method, return Messenger.getbinder ()
5, in handler{}, the information sent to the client after the post-processing, the processing of the message back to the client:
Perform a check operation (the query condition is, Msg.getdata (). getString () + "%") returns a collection that defines a message,what value of 1,

Carry SetData (bundle), bundle carrying string
6. Define Messenger object equals Msg.replyto, Messenger object sends message
7. List registration: Include <Intent-filter> node <action> properties: Recommended package name. Class Name

Client:
1. Declaration of two Messenger objects Messenger, messenger_reply;
2, bindmyservice binding services;
Bindservice (Intent, Conn,bind_auto_create): Collect three parameters:
Intent.setpackage, Setaction (package name, class name), when creating conn, override two methods when establishing a connection:
Instantiate messenger = new Messenger (service);
The disconnection assignment is null, the third parameter is suggested as Bind_auto_create
3. Initmesengerreply Initialization return Messenger:
Handler Handler = new Handler () {//5th step}, Messenger_reply = new Messenger (Handler);
4. The client sends the request information to the server:
Define MESSAGE,MSG.SETDATA (bundle), bundle carry string (query keyword), focus:
Msg.replyto = messenger_reply, Messenger.send (msg)
5, return Messenger to the return information after the post-processing: Msg.what = 1,
String result = Msg.getdata (). getString ("result");
6. The resulting result is loaded onto the ListView control

Enable exposure and reception of data across apps in Android

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.