當使用AsyncTask時,即使它在服務中,我也遇到了屏幕方向的問題。
我服務的樣子:AsynTask裏面的服務:處理屏幕方向
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。 我想知道爲什麼它可能發生在Service
裏面甚至AsyncTask
?
我已閱讀this post,但我不想鎖定屏幕方向或類似的東西。我比更喜歡IntentService
作爲Service
的靈活,我可以使用它與Binder來獲取Service實例。
所以,問題是,是否有任何方法可以在Service
而不是AsyncTask
內部進行線程安全?
如果你使用'Context.startService()'顯式啓動服務會怎麼樣? – Karakuri
@Karakuri我使用bindService通過服務連接中的活頁夾獲取服務實例 – R4j
您可以同時使用兩者。 – Karakuri