Activity的上下文是不稳定的,当Acitivity被销毁后,子线程将失去管理。
Activity是和ui绑定的Context,于是引入了和Activity无关的Context Service。
Service是单例
image.png
image.png

startForegroundServcie必须上通知栏
Binder实现本地Service的通信
AIDL实现RemoteService的通信
创建aidl文件后build
Service内定义binder返回这个类的实例

class MyAIDL extends IMyAidlInterface.Stub{
}

Client内

public void onServiceConnected(ComponentName name, IBinder service) {  
    try {  
        IMyAidlInterface.Stub.asInterface(service);  
    } catch (RemoteException e) {  
        e.printStackTrace();  
    }  
}

程浩
21 声望2 粉丝