1. Launch service across Apps
Set the intent of the start service
Serviceintent = new Intent ();
Serviceintent.setcomponent (New ComponentName ("Com.example.startservicefromanotherapp", " Com.example.startservicefromanotherapp.AppService "));
Com.example.startservicefromanotherapp is the package name where the service resides
Com.example.startservicefromanotherapp.AppService is the name of the service.
2. Bind service across Apps
Create a file with the Aidl suffix in the project where the service is located, and Eclipse will automatically generate the corresponding Java file in the Gen directory
For example
Package Com.example.startservicefromanotherapp;
Interface iappserviceremotebinder{
void SetData (String data);
}
Create a new project and copy the Aidl file to the same package name created under the SRC directory
3. Cross-app service communication
Onbind Method for service side
Public IBinder Onbind (Intent Intent) {
Todo:return the communication channel to the service.
return new Iappserviceremotebinder.stub () {
@Override
public void SetData (String data) throws RemoteException {
TODO auto-generated Method Stub
AppService.this.data = data;
}
};
In the onserviceconnected method of Serviceconnection
Binder = IAppServiceRemoteBinder.Stub.asInterface (service);
Synchronizing data
Binder.setdata (Edtinput.gettext (). toString ());
4. All code
Service side
Package Com.example.startservicefromanotherapp;
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 Appservice extends Service {
Public Appservice () {
}
Private String data = "Default data";
Private Boolean running = false;
@Override
Public IBinder Onbind (Intent Intent) {
Todo:return the communication channel to the service.
return new Iappserviceremotebinder.stub () {
@Override
public void SetData (String data) throws RemoteException {
TODO auto-generated Method Stub
AppService.this.data = data;
}
};
}
@Override
public void OnCreate () {
Super.oncreate ();
SYSTEM.OUT.PRINTLN ("service OnCreate");
running = true;
New Thread () {
public void Run () {
while (running) {
try {
Sleep (1000);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN (data);
}
};
}.start ();
};
@Override
public void OnDestroy () {
TODO auto-generated Method Stub
Super.ondestroy ();
running = false;
SYSTEM.OUT.PRINTLN ("service OnDestroy");
}
}
Aidl file
Package Com.example.startservicefromanotherapp;
Interface iappserviceremotebinder{
void SetData (String data);
}
Client
File structure
Package Com.example.anotherapp;
Import android.support.v7.app.ActionBarActivity;
Import Com.example.startservicefromanotherapp.IAppServiceRemoteBinder;
Import Android.annotation.SuppressLint;
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.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.EditText;
@SuppressLint ("Newapi")
public class Anotherapp extends actionbaractivity implements Onclicklistener, serviceconnection {
Intent serviceintent;
EditText Edtinput;
Private Iappserviceremotebinder binder = null;
@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_another_app);
Serviceintent = new Intent ();
Serviceintent.setcomponent (New ComponentName ("Com.example.startservicefromanotherapp", " Com.example.startservicefromanotherapp.AppService "));
Edtinput = (EditText) Findviewbyid (r.id.etinput);
Findviewbyid (R.id.btnstartservice). Setonclicklistener (this);
Findviewbyid (R.id.btnstopservice). Setonclicklistener (this);
Findviewbyid (R.id.btnbindservice). Setonclicklistener (this);
Findviewbyid (R.id.btnunbindservice). Setonclicklistener (this);
Findviewbyid (r.id.btnsyncdata). Setonclicklistener (this);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.another_app, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Switch (V.getid ()) {
Case R.id.btnstartservice:
StartService (serviceintent);
Break
Case R.id.btnstopservice:
StopService (serviceintent);
Break
Case R.id.btnbindservice:
Bindservice (Serviceintent, this, bind_auto_create);
Break
Case R.id.btnunbindservice:
Unbindservice (this);
Break
Case R.id.btnsyncdata:
if (binder! = null) {
try {
Binder.setdata (Edtinput.gettext (). toString ());
} catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
};
Break
Default
Break
}
}
@Override
public void onserviceconnected (componentname name, IBinder service) {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Service Connected");
Binder = IAppServiceRemoteBinder.Stub.asInterface (service);
}
@Override
public void onservicedisconnected (componentname name) {
TODO auto-generated Method Stub
Binder = null;
}
}
Understanding and use of aidl in Android