Blog from: http://blog.csdn.net/liuxian13183, reprint annotated source! All Rights Reserved!
Early translation of an Android API article Android Advanced development of the--API service, today mainly about the actual combat things
For example, the service is started in two ways, first configure the service in Androidmanifest.xml, and then start with Bindservice and StartService,
The difference is:
Execution mode: The former executes the Oncreate-->onbind method at startup and executes only once, while the latter executes the Oncreate-->onstartcommand method, Onstartcommand method can execute multiple times
Communication mode: The former can only pass a value by Onbind, and then get a reference to the Bind class to Quan Zhuan the value
Foreservice
public int last (int index) {if (index = = 0) {index = 4;} else {index-= 1;} Simageid = index% 5;return Simageid;} public class Mybinder extends Binder {public Foreservice getService () {return foreservice.this;}}
Call class
Foreservice = new Intent (this, foreservice.class);//Create connection Foreconn = new Serviceconnection () {@Overridepublic void Onservi cedisconnected (componentname name) {//TODO auto-generated method stub} @Overridepublic void Onserviceconnected ( ComponentName name, IBinder service) {//TODO auto-generated method Stubmybinder = (Mybinder) service;transportfore ();}};
the latter can pass the value of the intent object in the Onstartcommand method each time, or it can pass the value through the above method.
The above is the starting mode. Of course it is impossible to tell such a little knowledge, the next thing is more important.
Since Android allocates memory based on the process, and our app runs on a virtual machine, which is a master process, then if the execution logic is complicated, even if there are more threads (and there are limits), the method comes to the main process to open a sub-process, with what to open it? Yes, that's service!. Because today only speaks of it.
First in Androidmanifest.xml (if you read my blog all the time, remember that later it is called the Master profile), look at it first, register the service
<service android:name= "Com.liuxian13183.service.BkService" android:process= ". Core"/><service Android: Name= "Com.liuxian13183.service.ForeService"/>
we can see the class named Bkservice, more than the following foreservice a parameter android:process, right is this parameter, if you do not set the default with the main process of the same process, if set regardless of the point. or a colon:, represents a new START process , and the child process communicates with the main process only through Aidl.
Second, define a aidl file, the return value can use the underlying data type +list and map, mainly used for value communication
Cases:
Package Com.liuxian13183.service;interface inext{int next (int index);}
Finally define the service, which is called on the front
Service Definition:
public class Bkservice extends Service {private int simageid = 0; @Overridepublic ibinder onbind (Intent Intent) {//TODO Au To-generated Method Stubsimageid = Intent.getintextra ("key", 0); return new Next (); public class Next extends Inext.stub {@Overridepublic int next (int index) throws RemoteException {//TODO auto-generated m Ethod Stubindex + = 1;simageid = index% 5;return simageid;}}
Front-end invocation
Backservice = new Intent (this, bkservice.class); backconn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated method stublog.i ("Com.liuxian13183.service", "");} @Overridepublic void onserviceconnected (componentname name, IBinder service) {//TODO auto-generated method Stubinext = In Ext. Stub.asinterface (service); Transportback ();};
if (iNext! = null) {Transportback ();} else {//Getconnectionbackservice.putextra ("key", Simageid); StartService ( Backservice); Bindservice (Backservice, Backconn, bind_auto_create);//callback}
private void Transportback () {try {Simageid = Inext.next (Simageid);} catch (RemoteException e) {//TODO auto-generated Cat CH Blocke.printstacktrace ();} Showalbum ();} private void Showalbum () {InputStream rawimagestream = getresources (). Openrawresource (Simagearray[simageid]); byte[] data = Webputils.streamtobytes (Rawimagestream); final Bitmap Webpbitmap = webputils.webptobitmap (data); Imageview.setimagebitmap (WEBPBITMAP);}
The effect is as follows:
This is the source you want most! Android Service and WEBP tutorial source
The difference between the Android background process and the foreground thread