我知道绑定服务仅在活动或任何组件需要它之前存在。我们必须从 onBind 函数调用 MyLocalBinder 类。为什么会这样?为什么不能直接调用呢?
public class MyService extends Service {
private final IBinder myBinder = new MyLocalBinder();
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}
public String getCurrentTime(){
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss", Locale.UK);
return (df.format(new Date()));
}
public class MyLocalBinder extends Binder{
MyService getService(){
return MyService.this;
}
}
}