-
Service的类型
本片按地点划分种类
-
开启关闭本地服务
public static void bindLocalService(Context context, ServiceConnection serviceConnection) {
if (context == null || null == serviceConnection) {
return;
}
//通过Intent指定服务端的服务名称和所在包,与远程Service进行绑定
Intent intent = new Intent();
intent.setClass(context, LocalService.class);
Bundle bundle = new Bundle();
bundle.putString("name", "张三");
bundle.putString("age", "25");
intent.putExtras(bundle);
//绑定服务,传入intent和serviceConnection对象
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
/**
* 解绑服务
*
* @param context
*/
public static void unBindLocalService(Context context, ServiceConnection serviceConnection) {
if (context == null || null == serviceConnection) {
return;
}
try {
// 当需要多次调用doSomething()方法的时候,如果直接bindService是会报错的
context.unbindService(serviceConnection);
} catch (Exception e) {
e.printStackTrace();
}
}
- 开启关闭远程服务
/**
* 绑定远程服务
*
* @param context
* @param serviceConnection
*/
public static void bindTheRemoteService(Context context, ServiceConnection serviceConnection) {
if (context == null || null == serviceConnection) {
return;
}
//通过Intent指定服务端的服务名称和所在包,与远程Service进行绑定
Intent intent = new Intent();
intent.setClass(context, TheRemoteService.class);
Bundle bundle = new Bundle();
bundle.putString("Fruitname", "苹果");
bundle.putString("Fruitage", "今年");
intent.putExtras(bundle);
//绑定服务,传入intent和serviceConnection对象
context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
/**
* 解绑远程服务
*
* @param context
* @param serviceConnection
*/
public static void unBindTheRemoteService(Context context, ServiceConnection serviceConnection) {
if (context == null || null == serviceConnection) {
return;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190401093115241.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2dpdGh1Yl8zNDQwMjM1OA==,size_16,color_FFFFFF,t_70) try {
// 当需要多次调用doSomething()方法的时候,如果直接bindService是会报错的
context.unbindService(serviceConnection);
} catch (Exception e) {
e.printStackTrace();
}
}
想要开启service还需要在manifest注册,这样你就可以开启service了。
<!--本地 service-->
<service android:name=".service.LocalService">
<intent-filter>
<action android:name=" com.aidl.myapplication.service.LocalService" />
</intent-filter>
</service>
<!--远程 service-->
<service android:name=".service.TheRemoteService"
android:process=":FruitLive"
android:exported="true">
<intent-filter>
<action android:name="c com.aidl.myapplication.service.TheRemoteService" />
</intent-filter>
</service>
private TheRemoteInterface theRemoteInterface;
//创建ServiceConnection的匿名类
ServiceConnection serviceTheRemoteConnection = new ServiceConnection() {
//重写onServiceConnected()方法和onServiceDisconnected()方法
//在Activity与Service建立关联时调用
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//使用BindInterface.Stub.asInterface()方法将传入的IBinder对象传换成了BindInterface对象
theRemoteInterface = TheRemoteInterface.Stub.asInterface(service);
try {
//通过该对象调用在bindInterface文件中定义的接口方法,从而实现跨进程通信
theRemoteInterface.registerCallback(new TheRemoteCallBack());
} catch (RemoteException e) {
e.printStackTrace();
}
}
//在Activity与Service解除关联的时候调用
@Override
public void onServiceDisconnected(ComponentName name) {
try {
if (null != theRemoteInterface) {
theRemoteInterface.unRegisterCallback();
}
} catch (RemoteException e) {
e.printStackTrace();
}
theRemoteInterface = null;
}
};
具体详情请参考demoService之AIDL的传值
/**
* service回调
*/
class TheRemoteCallBack extends com.aidl.myapplication.TheRemoteCallBack.Stub {
@Override
public void fruitCallBack(FruitBean mFruitBean) throws RemoteException {
showFruitUserInfo(mFruitBean.getFruit1(), mFruitBean.getFruit2());
}
}
/**
* activity传值service
*/
class TheRemoteInterface extends com.aidl.myapplication.TheRemoteInterface.Stub {
@Override
public void registerCallback(TheRemoteCallBack callback) throws RemoteException {
theRemoteCallBack = callback;
}
@Override
public void unRegisterCallback() throws RemoteException {
theRemoteCallBack = null;
}
@Override
public void sendFruitMessage(String name) throws RemoteException {
Log.e("sendFruitMessage", "sendFruitMessage name " + name);
}
}