How to use parcel:
Parcel can be thought of as a data package, and everything can be put in.
Generate the parcel object: parcel = Parcel. obtain ();
Assign a value to parcel:
Parcel. writeString ("abc ");
Parcel. writeInt (123 );
Parcel. writeFloat (1.4f );
Obtain the value of parcel:
Parcel. setDataPosition (0 );
String str = parcel. readString ();
Int I = parcel. readInt ();
Float f = parcel. readFloat ();
Here, we will explain that when we assign a value to parcel, we can consider it as a pointer assignment. Every time we pay a value, the pointer moves one digit backward and parcel when the value is taken. setDataPosition (0); it means to put the pointer to the first one, and then extract the value in sequence. The specific example will not be posted.
The following is a complete example of binding a service:
============= MainActivity. java ================================
Package com. yx. boundservicetransact;
Import android. OS. Binder;
Import android. OS. Bundle;
Import android. OS. IBinder;
Import android. OS. Parcel;
Import android. OS. RemoteException;
Import android. app. Activity;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. content. ServiceConnection;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Public class MainActivity extends Activity {
Private Button boundButton;
Private Button sendButton;
Private Binder binder;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
BoundButton = (Button) findViewById (R. id. boundButton );
SendButton = (Button) findViewById (R. id. sendButton );
BoundButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
Intent intent = new Intent ();
Intent. setClass (MainActivity. this, SecondService. class );
BindService (intent, conn, BIND_AUTO_CREATE );
}
});
SendButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Parcel data = Parcel. obtain (); // create an empty data
Parcel reply = Parcel. obtain (); // create an empty reply
Data. writeString ("data in avti.pdf"); // assign a value to data
Try {
Binder. transact (0, data, reply, 0); // here the onTransact method in MyBinder in SecondService is run and the value of reply can be obtained.
System. out. println ("reply data after activity returns:" + reply. readString ());
} Catch (RemoteException e ){
E. printStackTrace ();
}
}
});
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}
ServiceConnection conn = new ServiceConnection (){
@ Override
Public void onServiceDisconnected (ComponentName name ){
}
@ Override
Public void onServiceConnected (ComponentName name, IBinder service ){
System. out. println ("onServiceConnected ");
MainActivity. this. binder = (Binder) service;
}
};
}
====================== SecondService. java ==============================
Package com. yx. boundservicetransact;
Import android. app. Service;
Import android. content. Intent;
Import android. OS. Binder;
Import android. OS. IBinder;
Import android. OS. Parcel;
Import android. OS. RemoteException;
Public class SecondService extends Service {
@ Override
Public IBinder onBind (Intent arg0 ){
Return new MyBinder ();
}
Class MyBinder extends Binder {
@ Override
Protected boolean onTransact (int code, Parcel data, Parcel reply,
Int flags) throws RemoteException {
// Data is the data sent to the service, and reply is the data of the returned activity.
System. out. println ("data received in service -->" + data. readString ());
Reply. writeString ("reply data in service ");
Return super. onTransact (code, data, reply, flags );
}
}
}
============================= AndroidManifest. xml ================
Package = "com. yx. boundservicetransact"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
Android: minSdkVersion = "8"
Android: targetSdkVersion = "18"/>
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
Android: name = "com. yx. boundservicetransact. MainActivity"
Android: label = "@ string/app_name">