Online voluminous articles are introduced to the Android remote service, one will binder mechanism, aidl speak well, but few people can give a clear example of how to use the fastest way to learn to write and invoke an Android remote service. If you're just trying to write or invoke the Android remote service and don't bother to understand how the binder mechanism works, this article is just right for you. After all, everyone can drive now, but few people understand how the engine works.
Preliminary knowledge
Readers should have basic Java knowledge, and the development experience of Android simple app.
Environment
Code Run Environment:
1.adt2014 version;
2.android:minsdkversion= "8"; android:targetsdkversion= "20"
APPCOMPATV7 has been generated in 3.workspace, its version is android-22;
Remote Service Development Tutorial
Before you start developing, figure out a few concepts:
1. IPC: interprocess communication, you just need to know that Android relies on this thing to make remote service calls.
2. Binder mechanism : An IPC mechanism invented by Android, which is said to be very, very good, you just think it is a black box, through this black box can make remote service calls, and Android many of the mechanisms are implemented through it.
3. Aidl language : A language specifically designed to write a remote interface, read its name and know that the Android Interface Definition
Language. The Aidl language can be compiled into Java source code by the Android-supplied compiler, which will be used by both service and client to simplify the remote service development process. If you've ever played CORBA, you'll know more about the IDL language.
4. IInterface interface, IBinder interface, IBinder class and so on: are used to implement the binder mechanism of the interface and class, in this tutorial, you will be a binder black box as part of it, do not need to understand.
Again, Android offers a standard example of a remote service in the Apidemos, but one is that it does not separate the server from the client, and the second is that there are so many other features in the example that it is difficult to understand. This example is Com.example.android.apis.app.RemoteService, interested in the article can be read after the detailed study.
The first step is to create a common Android application
Application named Wxbremoteservice, this application can delete its activity class, but for simplicity, we keep all the code created automatically.
Step two, write Aidl
The syntax of the Aidl language is very similar to Java, you can even write a Java interface, then delete public, protected, private these permission qualifier. Examples are as follows Iwxbservice.aidl:
Package com.dumaisoft.wxbremoteservice;
Interface Iwxbservice {
void SetName (String name);
String getName ();
}
Note Some points:
1. The interface name and Aidl file name are the same.
2. Interface and method without access modifier public,private,protected, and can not use final,static.
The 3.AIDL default supported type packages are Java base types (int, long, Boolean, and so on) and (String, List, Map, charsequence) that do not require an import declaration when using these types. The element types in the list and map must be aidl supported types. If you use a custom type as a parameter or return value, the custom type must implement the Parcelable interface.
4. Other interface types generated by custom types and aidl should be explicitly import in the aidl description file, even if the class and the defined package are in the same package.
5. All non-Java base type parameters in the Aidl file must be added in, out, and inout tags to indicate whether the parameter is an input parameter, an output parameter, or an input or output parameter.
6.Java The original type is marked in by default and cannot be a different tag
The location of the Iwxbservice.aidl file is in the Com.dumaisoft.wxbremoteservice package, and as long as the syntax is correct, the Java file is generated in the Com.dumaisoft.wxbremoteservice package in the ADT Gen directory Iwxb Service.java.
IWXBSERVICE.AIDL defines a remote interface that contains two methods GetName and SetName.
The third step is to write the service class
Add a Wxbservice class, it inherits the service class, the source code is as follows:
Package com.dumaisoft.wxbremoteservice;
Import com.dumaisoft.wxbremoteservice.IWxbService.Stub;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
Import android.os.RemoteException;
public class Wxbservice extends Service {
private serviceimpl Serviceimpl;
Inherits the class class
Serviceimpl extends stub{generated by Iwxbservice.aidl com.dumaisoft.wxbremoteservice.IWxbService.Stub
private String _name;
@Override public
void SetName (String name) throws RemoteException {
_name = name;
}
@Override public
String GetName () throws RemoteException {return
_name;
}
}
Make Serviceimpl a simple single case mode
private Serviceimpl getinstance () {
if (Serviceimpl = = null) {
Serviceimpl = New Serviceimpl ();
}
return serviceimpl;
}
@Override public
IBinder onbind (Intent Intent) {return
getinstance ();
}
}
By studying code, the biggest difference between a remote service class and a typical service class is that it has a member variable named Serviceimpl, which inherits the stub class and implements the GetName and SetName methods of the stub class. This stub class is provided by the Iwxbservice.java generated by Iwxbservice.aidl. We don't have to study the source code, just know its usage:
First: Let the service of a member variable inherit stub, and implement the method of remote interface;
Second: Returns an instance of a stub subclass in the Onbind method of the service.
Step Fourth, configure Androidmanifest.xml
Add the following code:
<service android:name= "Wxbservice" >
<intent-filter>
<action android:name= " Com.dumaisoft.wxbremoteservice.REMOTE_SREVICE "/>
</intent-filter>
</service>
Note that the name of the action is "Com.dumaisoft.wxbremoteservice.REMOTE_SREVICE", which is guaranteed by the developer to be a duplicate.
Step Fifth, install the app on the phone
Once the installation is complete, your remote service is registered in the binder black box, and any client who knows your remote service action name and interface will be able to bind the service and invoke the interface.
Remote Service Call Tutorial
The first step is to create an Android application
The Com.dumaisoft.wxbremoteserviceclient package is automatically generated in the application name WXBREMOTESERVICECLIENT,SRC package.
The second step is to introduce a remote service aidl file
Create the Com.dumaisoft.wxbremoteservice package in the SRC package (in order to be the same as the package name on the server), and then copy the Iwxbservice.aidl file written above into this directory. Obviously, the Iwxbservice.java file is also generated in the Gen directory of this project.
The third step is to write code that invokes the remote service
The code is as follows:
Package com.dumaisoft.wxbremoteserviceclient;
Import Com.dumaisoft.wxbremoteservice.IWxbService;
Import android.app.Activity;
Import Android.app.Service;
Import Android.content.ComponentName;
Import android.content.Intent;
Import android.content.ServiceConnection;
Import Android.os.Bundle;
Import Android.os.IBinder;
Import android.os.RemoteException;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Mainactivity extends activity {private Button btnbind;
Private Button Btnsetname;
Private Button Btngetname; Private Iwxbservice serviceproxy; Proxy private serviceconnection for remote Service conn = new Serviceconnection () {@Override public void onservicedisconnected (Com Ponentname name {} @Override public void onserviceconnected (componentname name, IBinder service) {//Get remote service generation
Daniel Serviceproxy = IWxbService.Stub.asInterface (service);
}
}; @Override protected void OnCreate (BundleSavedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Btnbind = (Button) This.findviewbyid (R.id.btnbind);
Btnsetname = (Button) This.findviewbyid (r.id.btnsetname);
Btngetname = (Button) This.findviewbyid (r.id.btngetname); Btnbind.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Intent service = new
Intent ();
Remote Service Action name service.setaction ("Com.dumaisoft.wxbremoteservice.REMOTE_SREVICE");
Bindservice (SERVICE, Conn, service.bind_auto_create);
}
}); Btnsetname.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {try {service
Proxy.setname ("MyName");
catch (RemoteException e) {e.printstacktrace ();
}
}
}); Btngetname.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {try {String
Name = Serviceproxy.getname (); Toast.maketext (GetapplicationcOntext (), name, Toast.length_long). Show ();
catch (RemoteException e) {e.printstacktrace ();
}
}
});
}
}
Note Some points:
First, create a serviceconnection anonymous subclass that obtains the remote service proxy object Serviceproxy in its Onserviceconnected method. In fact, the Onserviceconnected method is invoked when the Bindservice method is invoked, thus ensuring that the proxy object for the remote service is guaranteed to be obtained;
Second, IWxbService.Stub.asInterface (service) The method is also provided by the Iwxbservice.java file, and its internal mechanism does not need to be studied, only to know that it returns an object of the Iwxbservice interface that can invoke the SetName and GetName methods of the remote service through the binder black box;
Third, use intent to specify action as "Com.dumaisoft.wxbremoteservice.REMOTE_SREVICE", the correct bind to remote service.
After the success of BIND, you can use the function of remote service through the proxy object of remote service.
Summary
At this point, readers should be able to quickly develop a remote service, and can write the client easy to invoke it. It is also important to note that, in addition to using Aidl to write and invoke remote services, you can use interfaces and classes such as IBinder, Binder, and so on to write calls directly from the remote service.
The above is the entire content of this article, I hope to help you learn.