1.跨应用启动Service
设置启动Service的Intent
serviceIntent = new Intent();
serviceIntent.setComponent(new ComponentName("com.example.startservicefromanotherapp", "com.example.startservicefromanotherapp.AppService"));
com.example.startservicefromanotherapp是Service所在的包(package)名
com.example.startservicefromanotherapp.AppService是Service的名字
2.跨应用绑定Service
在Service所在的工程创建一个以aidl为后缀名的文件,eclipse会自动在gen目录下生成对应的java文件
例如
package com.example.startservicefromanotherapp;
interface IAppServiceRemoteBinder{
void setData(String data);
}
再新建一个工程,并把aidl文件拷贝到src目录下新建的相同的包名
3.跨应用Service通信
Service端的onBind方法
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;
}
};
在ServiceConnection的onServiceConnected方法中
binder = IAppServiceRemoteBinder.Stub.asInterface(service);
同步数据
binder.setData(edtInput.getText().toString());
4.所有代码
Service端
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 = "默认数据";
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文件
package com.example.startservicefromanotherapp;
interface IAppServiceRemoteBinder{
void setData(String data);
}
客户端
文件结构
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 will
// automatically handle clicks on the Home/Up button, so long
// as you specify a 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;
}
}