Aidl implementation demo for Android

Source: Internet
Author: User

Forwarding, but there was a problem in the past. This is basically the case. I have changed all the problematic ones. Now this is the task that can be run.


Creating an aidl service is more complex than creating a common service. The specific steps are as follows:

(1) Create an aidl file in the Java package directory of the eclipse Android project. The syntax of this file is similar to Java code, but it will be slightly different. For details, see instance 52.
(2) If the content of the aidl file is correct, ADT will automatically generate a Java interface file (*. Java ).
(3) create a service class ).
(4) Implement the Java interface generated by the aidl file.
(5) In androidmanifest. configure the aidl service in the XML file. Note that the attribute value of Android: name in the <action> tag is the ID of the service to be referenced by the client, that is, the parameter value of the intent class, you need to add an Android: Progress = ": Remote ".

Create an aidl Service

In this example, a simple aidl service is created. This aidl service only has one getvalue method. This method returns a value of the string type. After the service is installed, the getvalue method is called on the client and the return value is output in the textview component. Follow these steps to create the aidl service:
(1) Create an aidl file. Create an imyservice. aidl file in the Java package directory. Imyservice. aidl File Location

 

The content of the imyservice. aidl file is as follows:

Java code:
Package EOE. Demo;

Interface imyservice {
String getvalue ();
}

Imyservice. the content of the aidl file is very similar to that of the Java code. However, you must note that the data types not supported by modifiers (such as public and private) and aidl (such as inputstream and outputstream) cannot be added).

(2) If the content in the imyservice. aidl file is entered correctly, ADT will automatically generate an imyservice. Java file. Readers do not need to care about the specific content of this file or maintain it. For details about the file, you can view the source code provided in this section.
(3) Compile a myservice class. Myservice is a subclass of service. In the myservice class, myserviceimpl is defined. This class is a subclass of imyservice. stub. The myservice class code is as follows:

Java code:
Package EOE. Demo;

Import Android. App. Service;
Import Android. content. intent;
Import Android. OS. ibinder;
Import Android. OS. RemoteException;

Public class myservice extends Service {
Public class myserviceimpl extends imyservice. Stub {
@ Override
Public String getvalue () throws RemoteException {
Return "android/ophone development handouts ";
}
}
@ Override
Public ibinder onbind (intent ){
Return new myserviceimpl ();
}

}

Note the following when writing the above Code:

Imyservice. stub is automatically generated according to the imyservice. aidl file. Generally, you do not need to care about the content of this class. You only need to compile a subclass (myserviceimpl class) that inherits from imyservice. Stub class.

The onbind method must return the object instance of the myserviceimpl class. Otherwise, the client cannot obtain the service object.

(4) configure the myservice class in the androidmanifest. xml file. The Code is as follows:

Java code:
<Service android: Name = ". myservice" Android: Process = ": Remote">
<Intent-filter>
<Action Android: Name = "net. blogjava. Mobile. aidl. imyservice"/>
</Intent-filter>
</Service>

Compile the call code of the client. Create an eclipse Android Project (ch08_aidlclient) and send the aidl file and package to the src directory of the ch08_aidlclient project ,.This figure is incorrect.

 

To call the aidl service, you must bind the service before obtaining the service object. The Code is as follows:

 

Java code:
Package net. blogjava. Mobile;

Import net. blogjava. Mobile. aidl. imyservice;
Import Android. App. activity;
Import Android. content. componentname;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. serviceconnection;
Import Android. OS. Bundle; import Android. OS. ibinder;
Import Android. View. view; import Android. View. View. onclicklistener;
Import Android. widget. Button; import Android. widget. textview;

Public class main extends activity implements onclicklistener {
Private imyservice myservice = NULL;
Private button btninvokeaidlservice;
Private button btnbindaidlservice;
Private textview;
Private serviceconnection = new serviceconnection (){
@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
// Obtain the service object
Myservice = imyservice. stub. asinterface (service );
Btninvokeaidlservice. setenabled (true );
}
@ Override
Public void onservicedisconnected (componentname name ){
}
};
@ Override
Public void onclick (view ){
Switch (view. GETID ()){
Case R. Id. btnbindaidlservice:
// Bind the aidl Service
Bindservice (new intent ("net. blogjava. Mobile. aidl. imyservice"), serviceconnection, context. bind_auto_create );
Break;
Case R. Id. btninvokeaidlservice:
Try {
Textview. settext (myservice. getvalue ());
// Call the getvalue method of the server.
} Catch (exception e ){
}
Break;
}
}
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Btninvokeaidlservice = (button) findviewbyid (R. Id. btninvokeaidlservice );
Btnbindaidlservice = (button) findviewbyid (R. Id. btnbindaidlservice); btninvokeaidlservice. setenabled (false );
Textview = (textview) findviewbyid (R. Id. textview );
Btninvokeaidlservice. setonclicklistener (this );
Btnbindaidlservice. setonclicklistener (this );
}

}

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.