AIDL instance for Android Service learning

Source: Internet
Author: User

Note: This is an article I published earlier on eoe. Since each application runs in its own process space and can run another service process from the application UI, objects are often transferred between different processes. On the Android platform, a process generally cannot access the memory space of another process. Therefore, to perform a conversation, you need to break down the object into basic units that the operating system can understand and orderly pass through the process boundary. The following describes how to implement AIDL calling. This article includes AIDL interface calling and callback, and passing a Demo of complex data types to run the Aidl file Person. aidl package com. archermind. aidl; parcelable Person; ITaskBinder. aidl package com. archermind. aidl; import com. archermind. aidl. ITaskCallBack; import com. archermind. aidl. person; interface ITaskBinder {void fuc01 (); void fuc02 (); String fuc03 (in Person person); void registerCallBack (ITaskCallBack cb); void unregisterCallBack ();} ITaskCallBack. aidl package com. archermind. aidl; interface ITaskCallBack {void onActionBack (String str);} first, the Code server of Myservice implements the relevant AIDL interfaces for the client to call @ Override public IBinder onBind (Intent arg0) {// TODO Auto-generated method stub return mBinder;} private final ITaskBinder. stub mBinder = new ITaskBinder. stub () {@ Override public void unregisterCallBack () throws RemoteException {// TODO Auto-generated method stub Log. v (TAG, "unregisterCallBack... "); mTaskCallBack = null;} @ Override public void registerCallBack (ITaskCallBack cb) throws RemoteException {// TODO Auto-generated method stub Log. v (TAG, "registerCallBack... "); mTaskCallBack = cb ;}@ Override public void fuc01 () throws RemoteException {// TODO Auto-generated method stub Log. v (TAG, "fuc01... ") ;}@ Override public void fuc02 () throws RemoteException {// TODO Auto-generated method stub Log. v (TAG, "fuc02... "); mTaskCallBack. onActionBack ("hello world") ;}@ Override public String fuc03 (Person person) throws RemoteException {// TODO Auto-generated method stub String name = Person. getName (); String descrip = person. getDescrip (); int sex = person. getSex (); String ret = ""; if (sex = 0) {ret = "Hello" + name + ", you are handsome "+" ("+ descrip +") ";} else {ret =" Hello "+ name + ", you are beautiful "+" ("+ descrip +") ";}return ret ;}; the client implements the callback interface private final ITaskCallBack. stub mCallBack = new ITaskCallBack. stub () {@ Override public void onActionBack (String str) throws RemoteException {// TODO Auto-generated method stub Log. v (TAG, "onActionBack str =" + str) ;}}; how does the client bind to the server and establish a connection Intent intent = new Intent ("com. archermind. aidl. myservice "); bindService (intent, mConnection, BIND_AUTO_CREATE); private ServiceConnection mConnection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName, IBinder service) {// TODO Auto-generated method stub mService = ITaskBinder. stub. asInterface (service); try {mService. registerCallBack (mCallBack);} catch (RemoteException e) {// TODO Auto-generated catch block e. printStackTrace ();} Log. v (TAG, "onServiceConnected ");}

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.