Remote services enable communication between threads in the Android system

Source: Internet
Author: User

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

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.