AIDL (2): Transmission of complex objects, aidl transmission of Objects
IPC transmits complex objects through AIDL
1. Define data transmission objects
Person. aidl file:
In the Person. java file:
(1) Implement the parcelable Interface
(2) provide a static final attribute named CREATOR
Package com. liujun. aidl;
Import android. OS. Parcel;
Import android. OS. Parcelable;
Public class Person implements Parcelable {
Private String name;
Private int sex;
Public Person (){
}
Public Person (Parcel source ){
ReadFromParcel (source );
}
// A static final attribute named CREATOR must be provided. This attribute must implement the android. OS. Parcelable. Creator <T> interface.
Public static final Parcelable. Creator <Person> CREATOR = new Parcelable. Creator <Person> (){
@ Override
Public Person createFromParcel (Parcel source ){
Return new Person (source );
}
@ Override
Public Person [] newArray (int size ){
Return new Person [size];
}
};
@ Override
Public int describeContents (){
Return 0;
}
// Note that the order of reading and writing variables should be the same, otherwise the correct result will not be obtained.
@ Override
Public void writeToParcel (Parcel dest, int flags ){
Dest. writeString (name );
Dest. writeInt (sex );
}
// Note that the order of reading and writing variables should be the same, otherwise the correct result will not be obtained.
Public void readFromParcel (Parcel source ){
Name = source. readString ();
Sex = source. readInt ();
}
//////////////////////////////////////// //////////////////////////
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getSex (){
Return sex;
}
Public void setSex (int sex ){
This. sex = sex;
}
}
2. Define remote service interfaces and service components
IGreetService. aidl file:
Package com. liujun. aidl;
Import com. liujun. aidl. Person;
Interface IGreetService {
String greet (in Person person );
}
AIDLService. java file:
Package com. liujun. service;
Import com. liujun. aidl. IGreetService;
Import com. liujun. aidl. Person;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Import android. OS. RemoteException;
Import android. util. Log;
Public class AIDLService extends Service {
Private static final String TAG = "liujun ";
@ Override
Public void onCreate (){
Log. I (TAG, "onCreate () called ");
Super. onCreate ();
}
@ Override
Public IBinder onBind (Intent arg0 ){
Log. I (TAG, "onBind () called ");
Return stub;
}
// Service interface instance object
IGreetService. Stub stub = new IGreetService. Stub (){
@ Override
Public String greet (Person person) throws RemoteException {
Log. I (TAG, "greet (Person) called ");
Return ServiceMethod (person );
}
};
@ Override
Public boolean onUnbind (Intent intent ){
Log. I (TAG, "onUnbind () called ");
Return true;
}
@ Override
Public void onDestroy (){
Super. onDestroy ();
Log. I (TAG, "onDestroy () called ");
}
/////////////////-----------------------------------
/**
* Service component method
* @ Param person
* @ Return
*/
Public String ServiceMethod (Person person ){
String greeting = "hello," + person. getName ();
Switch (person. getSex ()){
Case 0:
Greeting = greeting + ", you're handsome."; project code
Break;
Case 1:
Greeting = greeting + ", you're beautiful .";
Break;
}
Return greeting;
}
}
Registration Service:
<! -- Configure service components -->
<Service android: name = "com. liujun. service. AIDLService">
<Intent-filter>
<Action android: name = "android. intent. action. AIDLService"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Service>
3. The client program copies the data transmission objects and interface files of the server program.
4. Bind Remote services, call remote service methods, and transmit complex objects
Package com. liujun. parcelableclient; 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. OS. remoteException; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import com. example. parcelableclient. r; import com. liujun. aidl. IGreetService; import com. liujun. aidl. person; public class MainActivity extends Activity {// control private Button bindBtn; private Button greetBtn; private Button unbindBtn; private boolean mBound = false; // whether to bind a remote service // The remote service interface object private IGreetService iService; // obtain the remote service interface object private ServiceConnection conn = new ServiceConnection () {@ Overridepublic void onServiceConnected (ComponentName name, IBinder service) {iService = IGreetService. stub. asInterface (service); mBound = true ;}@ Overridepublic void evaluate (ComponentName name) {mBound = false; iService = null ;}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); bindBtn = (Button) this. findViewById (R. id. bindBtn); greetBtn = (Button) this. findViewById (R. id. greetBtn); unbindBtn = (Button) this. findViewById (R. id. unbandBtn); // register and click the listener MyListener listener = new MyListener (); bindBtn. setOnClickListener (listener); greetBtn. setOnClickListener (listener); unbindBtn. setOnClickListener (listener);}/*** event processor * @ author asus **/private class MyListener implements OnClickListener {@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. bindBtn: // bind the remote service Intent intent = new Intent ("android. intent. action. AIDLService "); bindService (intent, conn, Context. BIND_AUTO_CREATE); // set the button status bindBtn. setEnabled (false); greetBtn. setEnabled (true); unbindBtn. setEnabled (true); break; case R. id. greetBtn: try {Person person = new Person (); person. setName ("liujun"); person. setSex (0); String retVal = iService. greet (person); Toast. makeText (MainActivity. this, retVal, Toast. LENGTH_SHORT ). show ();} catch (RemoteException e) {Toast. makeText (MainActivity. this, "error", Toast. LENGTH_SHORT ). show ();} break; case R. id. unbandBtn: unbindService (conn); bindBtn. setEnabled (true); greetBtn. setEnabled (false); unbindBtn. setEnabled (false); break ;}}@ Overrideprotected void onDestroy () {super. onDestroy (); if (mBound) {unbindService (conn); iService = null ;}}}
Engineering Code http://download.csdn.net/detail/u010739551/7710977
How can I use AIDL to design remote interfaces in Android?
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.
It is tedious to implement this data transmission process through code. Android provides the AIDL tool to handle this task. AIDL (AndroidInterfaceDefinitionLanguage) is an IDL Language used to generate code for inter-process communication (IPC) between two processes on the Android device. If you want to call operations on objects in another process (such as Service) in a process (such as Activity), you can use AIDL to generate serializable parameters. The AIDLIPC mechanism is interface-oriented, like COM or Corba, but more lightweight. It uses a proxy class to transmit data on the client and the implementation end.
1. Use AIDL to implement IPC (ImplementingIPCUsingAIDL)
To use AIDL to implement the IPC service, follow these steps: 1. Create the. aidl file. This file (YourInterface. aidl) defines available client methods and data interfaces. Second, add the. aidl file to the makefile file (the ADT plug-in Eclipse provides the management function ). Android includes a compiler named AIDL, which is located in the tools/folder. Third, the implementation interface-The AIDL compiler uses the Java language to create interfaces from the AIDL interface file. This interface has an inherited internal abstract class named Stub (some additional methods for calling IPC have been implemented). You must create an internal abstract class that inherits from YourInterface. stub class, and implemented in. methods declared in the aidl file. Fourth, open interfaces to the client. If you are writing a Service, you should inherit the Service and reload Service. onBind (Intent) to return the object instance that implements the interface.
2. Create a. aidl file (Createan. aidlFile)
AIDL uses a simple syntax to declare an interface and describe the parameters and return values of its methods and methods. These parameters and return values can be of any type, or even interfaces generated by other AIDL. Note that all non-built-in types must be imported. AIDL supports the following types of data: first, the main types of Java programming language (int, boolean, etc.) do not require the import Statement. Second, the import Statement declaration is required for the APIS generated by other AIDL passed in reference mode. Third, to implement Parcelableprotocol and custom classes passed by value, you must declare the import statement. In addition, there are some import statements not required, such as String.
3. ImplementingtheInterface)
AIDL and. if you use the Eclipse plug-in, aidl runs automatically as part of the compilation process (you do not need to run AIDL before compiling the Project). If there is no plug-in, you need to run AIDL first.
The generated interface contains an abstract internal class named Stub, which declares all. the methods described in aidl, Stub also defines a small number of auxiliary methods, especially asInterface (), through which IBinder (when applicationContext. when bindService () is successfully called, it is passed to the onServiceConnected () of the client and an interface instance used to call the IPC method is returned. To implement your own interface, start with YourInterface. stub class inheritance, and then implement the relevant methods (you can create. aidl files implement the stub method instead of intermediate compilation. The Android compilation process is. java file processing. aidl file ).
After implementing the interface, you need to expose the interface to the client, that is, publish the service. The implementation method is to inherit the Service, and then implement
Struts2 Data Transmission Problems
When an Action sets an attribute value, struts2 blocks all these attribute values in a com. opensymphony. xwork2.util. in the ValueStack object, and then the ValueStack object is named "struts. valueStack is set in the request attribute of the request (request. setAttribute ("struts. valuestack ");), the tag can be passed through the request attribute" struts. valueStack "removes the attribute set in the action (org. apache. struts2.views. jsp. tagUtils. getStack (PageContext pageContext )). For details, see the source code of the struts2 tag ......