跨应用启动服务:
在android5.0之前,想要开启其他应用的服务可以通过Intent设置服务的Action来隐式启动服务,但是在5.0之后,想要启动其他应用的服务只能通过Intent的显示启动来实现,如下:
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.example.administrator.coolweather","com.example.administrator.coolweather.MyService"));
//开启服务
startService(intent);
//关闭服务
stopService(intent);
通过设置组件名Component来显示启动service,Component的参数是程序的包名和服务的包名。
跨应用绑定服务:
首先在其他应用里创建一个AIDL文件:
// IMyAidlInterface.aidl
package com.example.administrator.downloadproject;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
然后在服务的onBind()方法里返回这个AIDL的实例:
@Override
public IBinder onBind(Intent intent) {
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
};
紧接着就是在自己应用里通过bindService绑定服务:
private Intent intent;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=new Intent();
intent.setComponent(new ComponentName("com.example.administrator.coolweather","com.example.administrator.coolweather.MyService"));
bindService(intent,connection, Context.BIND_AUTO_CREATE);
unbindService(connection);
}
跨应用绑定服务并通信:
在绑定服务的基础上,首先在其他应用的AIDL文件里添加需要的接口:
// IMyAidlInterface.aidl
package com.example.administrator.downloadproject;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
//添加的接口
void getData(String data);
}
其次,在服务中返回AIDL时实现了接口也要添加:
@Override
public IBinder onBind(Intent intent) {
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
//实现接口
@Override
public void getData(String data) throws RemoteException {
//接收传过来的数据data
}
};
在接着需要在自己的应用中创建一个AIDL的文件夹,然后创建一个包,包名是其他应用的包名,然后把其他应用的AIDL文件复制到这个包里,这样自己的应用也有了AIDL文件
图中蓝色的包名应该是其他应用的包名
然后是最关键的:在自己应用里获取实例:
//把其他应用的AIDL文件复制过来的目的就在这
private IMyAidlInterface iMyAidlInterface;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//转换方式和普通的不一样
iMyAidlInterface= IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
然后就可以通过iMyAidlInterface进行通信了:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent=new Intent();
intent.setComponent(new ComponentName("com.example.administrator.coolweather","com.example.administrator.coolweather.MyService"));
bindService(intent,connection, Context.BIND_AUTO_CREATE);
try {
//发送数据给其他应用
iMyAidlInterface.getData("数据");
} catch (RemoteException e) {
e.printStackTrace();
}
unbindService(connection);
}