Some of the aidl of Android

Source: Internet
Author: User

On the service side, just define one of the services:

<service  android:name= ". Basetypeservice ">            <intent-filter >                <action android:name=" Cn.bgxt.Service.BASE_TYPE_SERVICE "/ >            </intent-filter>                    </service>

For the basic type passed: Create the data type to be passed:

Interface idog{string getName (); int getage ();}

Then create the stub,stub on the server's service is the stub meaning, in the client also has a same idog class, communication is the time to pass the Idog type:

Service-Side services:

Import com.example.aidlservicedemo.domain.IDog.Stub;

public class Basetypeservice extends Service {private final String tag= "main";p rivate dogbinder binder=null;private Strin G[] names=new string[]{"small white", "Wang Choi", "Small Black"};p rivate int[] ages=new int[]{1,2,3};/** * Stub implementation class, Stub internal implementation of Binder * Internally implemented Aidl defined methods */public class Dogbinder extends stub{@Overridepublic String getName () throws RemoteException {<strong& Gt;return names[0];</strong>} @Overridepublic int getage () throws RemoteException {<strong>return ages[1]; </STRONG>}} @Overridepublic void OnCreate () {super.oncreate ();//Instantiate Binder Object Binder=new dogbinder (); LOG.I (TAG, "Create service Success");} @Overridepublic ibinder onbind (Intent Intent) {log.i (TAG, "bind service succeeded");//Return binder object return binder;}}

This is called by the client:

public class Basetypeactivity extends Activity {private Button btn_startservice, Btn_endservice,btn_getservicedata; Private Idog dogservice; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_service); btn_startservice = (Button) Findviewbyid (R.id.btn_ StartService) Btn_endservice = (Button) Findviewbyid (r.id.btn_endservice); btn_getservicedata = (Button) findViewById (R.id.btn_getservicedata); Btn_startservice.setonclicklistener (click); Btn_endservice.setonclicklistener (click); Btn_getservicedata.setonclicklistener (click);}  Private View.onclicklistener click = New Onclicklistener () {@Overridepublic void OnClick (View v) {switch (V.getid ()) {case R.id.btn_startservice:startservice (); break;case r.id.btn_endservice:endservice (); Break;case R.id.btn_ Getservicedata:getservicedate (); break;}}};/ * * Get data */private void Getservicedate () {try {if (dogservice!=null) {<strong>stringbuilder sbuilder=new StringBuilder (); Sbuilder.Append ("Name:" +dogservice.getname ()); Sbuilder.append ("\nage:" +dogservice.getage ());//Get data, this result should be small white, 2</ Strong>toast.maketext (Basetypeactivity.this, sbuilder.tostring (), Toast.length_short). Show (); Else{toast.maketext (Basetypeactivity.this, "Please bind service first", Toast.length_short). Show ();}} catch (Exception e) {e.printstacktrace ();}} Private serviceconnection connbase=new serviceconnection () {@Overridepublic void onservicedisconnected (componentname Name) {Dogservice=null;} @Overridepublic void onserviceconnected (componentname name, IBinder service) {//IDog.Stub.asInterface, get interface <strong >dogservice=idog.stub.asinterface (service);//indicates that the service object received </strong>}};/** * Start Services */private void StartService () {<strong>intent intent=new Intent (); Intent.setaction ("Cn.bgxt.Service.BASE_TYPE_SERVICE");// This must be consistent with the action on the server Menufest Bindservice (Intent, connbase, bind_auto_create); Toast.maketext (Basetypeactivity.this, "Start binding Service", Toast.length_short). Show (); </strong>}/** * Stop service */private void EnDservice () {if (connbase!=null) {unbindservice (connbase); Toast.maketext (basetypeactivity.this, "Service Unbound", Toast.length_short). Show ();}}


If Aidl communication is a custom type:

A common example of returning object message information based on user, user, message is a custom type:

public class Message implements parcelable {private int id;private string Msgtext;private string Fromname;private string D ate;} public class User implements parcelable {private int id;private string username;private string password;}

Definition of two aidl files

Message.aidl:

Parcelable Message;

User.aidl

Parcelable User;

Server code:

public class Customtypeservice extends Service {private static final String TAG = "main";p rivate msgbinder msgbinder = nul L;private static Map<user, list<message>> Map = new Hashmap<user, list<message>> (), Static {for ( int i = 0; I < 3; i++) {User user = new User (I, "Jack" + I, "+" + i); list<message> messages = new arraylist<message> (); Message msg = NULL;IF (i = = 0) {msg = new Message (i, "How are these two days?"). "," Jerry ", New Date (). togmtstring ()); Messages.add (msg); Map.put (user, messages);}} public class Msgbinder extends stubs {@Overridepublic list<message> getmes (User us) throws RemoteException {for (Map. Entry<user, list<message>> Msgs:map.entrySet ()) {if (Msgs.getkey (). GetUserName (). Equals (Us.getusername ()) && Msgs.getkey (). GetPassword (). Equals (Us.getpassword ())) {log.i (TAG, "found information"); return Msgs.getvalue ();}} LOG.I (TAG, "No information Found"); return Map.get (US);}} @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn Msgbinder;} @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); msgbinder = new Msgbinder ();} @Overridepublic void OnDestroy () {msgbinder = Null;super.ondestroy ();}}

The client is basically consistent:

private static user[] users = new user[] {new User (0, "jack0", "990")};/** * Get additional thread service data */private void Getservicedate () {TR Y {list<message> msgs=getmsg.getmes (users[0]); StringBuilder sbuilder=new StringBuilder (); for (Message msg:msgs) {sbuilder.append (msg.tostring () + "\ n");} Toast.maketext (Customtypeactivity.this, sbuilder.tostring (), Toast.length_short). Show (); catch (Exception e) {toast.maketext (customtypeactivity.this, "Get data Error", Toast.length_short). Show (); e.printstacktrace ();}}


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.