使用 AsyncTask 时,即使它在服务内部,我也遇到了屏幕方向问题。
我的服务看起来像:
public class RequestService extends Service {
private MyBinder binder;
public RequestService(){
binder = new MyBinder(RequestService.this);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class MyBinder extends Binder{
private final RequestService service;
public MyBinder(RequestService service){
this.service = service;
}
public RequestService getService(){
return this.service;
}
}
public <T> void sendRequest(Request<T> task, INotifyRequest<T> notify){
// Call excute the asynctask and notify result in onPostExcute
new TaskExecutor<T>(task, notify).execute();
}
}
更新:我像这样使用我的服务:
// start the service
final Intent intent = new Intent(context, serviceClass);
context.startService(intent);
// then bound the service:
final Intent intentService = new Intent(context, serviceClass);
// Implement the Service Connection
serviceConnection = new RequestServiceConnection();
context.getApplicationContext().bindService(intentService, serviceConnection,
Context.BIND_AUTO_CREATE);
当方向改变时,服务解除绑定然后重新绑定,AsyncTask
不通知更新 UI。我想知道为什么它甚至会发生AsyncTask
在里面Service
?
我已经阅读了这篇文章,但我不想锁定屏幕方向或类似的东西。我更喜欢Service
than IntentService
asService
的灵活,我可以将它与 Binder 一起使用来获取 Service 实例。
所以,问题是,有没有办法在Service
而不是里面做线程安全AsyncTask
?