1. Create remote service classes
(1) Service
Public class myservice extends Service {
// Remote service
@ Override
Public ibinder onbind (intent ){
// Todo auto-generated method stub
Return new mybinder ();
}
Private class mybinder extends iservice. Stub {
@ Override
Public void callmethodinservice (string name) throws RemoteException {
Sayhelloinservice (name );
}
}
Public void sayhelloinservice (string name ){
Logoff. Prepare ();
Toast. maketext (this, "hello" + name, 1). Show ();
Logoff. Loop ();
}
@ Override
Public void oncreate (){
System. Out. println ("reomte oncreate ");
Super. oncreate ();
}
@ Override
Public void onstart (intent, int startid ){
// Todo auto-generated method stub
System. Out. println ("onstart ");
Super. onstart (intent, startid );
}
@ Override
Public void ondestroy (){
// Todo auto-generated method stub
System. Out. println ("ondestroy ");
Super. ondestroy ();
}
}
Configuration
<Service android: Name = ". myservice">
<Intent-filter>
<Action Android: Name = "cn. itcast. remoteservice"/>
</Intent-filter>
</Service>
</Application>
(2) interface iservice. aidl
Package CN. itcast. remoteservice;
Interface iservice {
Void callmethodinservice (string name );
}
(3) automatically generated file iservice. Java
Public interface iservice extends Android. OS. iinterface
{
/** Local-side IPC implementation stub class .*/
Public static abstract class stub extends Android. OS. Binder
Implements remoteservice. iservice
{
........................
}
Engineering Structure Diagram
2. Remote Access
Demoactivity. Java class
Public class demoactivity extends activity {
Intent intent;
Myremoteconn conn;
CN. itcast. remoteservice. iservice iremoteservice;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Conn = new myremoteconn ();
}
Private class myremoteconn implements serviceconnection {
@ Override
Public void onserviceconnected (componentname name, ibinder Service ){
Iremoteservice = cn. itcast. remoteservice. iservice. stub. asinterface (service );
}
@ Override
Public void onservicedisconnected (componentname name ){
}
}
@ Override
Protected void ondestroy (){
// Todo auto-generated method stub
Unbindservice (conn );
Super. ondestroy ();
}
// Bind button
Public void bindremoteservice (view ){
Intent service = new intent ();
Service. setaction ("cn. itcast. remoteservice ");
Bindservice (service, new myremoteconn (), bind_auto_create );
}
// Execute the service method
Public void callmethodinremoteservice (view ){
Try {
Iremoteservice. callmethodinservice ("remote zhangsan ");
} Catch (RemoteException e ){
E. printstacktrace ();
}
}
}
(2) interface iservice. aidl
Package CN. itcast. remoteservice;
Interface iservice {
Void callmethodinservice (string name );
}
(3) automatically generated file iservice. Java
Public interface iservice extends Android. OS. iinterface
{
/** Local-side IPC implementation stub class .*/
Public static abstract class stub extends Android. OS. Binder
Implements remoteservice. iservice
{
........................
}
Project file structure