重複の可能性:
Android サービスによりアクティビティが応答しなくなる
サービスを使用して、データベースとビューの間でデータを同期します。しかし、サービスを使用して長いタスクを実行するたびに、サービスが正常に機能せず、ビュー停止応答 (UI でイベントを実行できません) が発生し、サービスが完了するのを待つ必要があります。これが私のサービスです:
public class SyncService extends Service{
private static final String TAG = "SyncService";
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent)
{
Log.d(TAG, "call onBind");
return new DataManagerBinder();
}
private class DataManagerBinder extends Binder implements IUserDataManager
{
@Override
public void doProcess(Activity mView)
{
//do some long task (not touch UI thread)
// this will cause the view not response
syncDB();
// update view after process completed
mView.updateViewOnComplete();
}
}
クライアントアクティビティでこのサービスをバインドしようとしています
//the interface to handle binder
IUserDataManager viewManager = null;
ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
Log.i(TAG, "connection closed unexpectedly");
viewManager = null;
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder)
{
Log.d(TAG, "serviceConnection onServiceConnected");
viewManager = (IUserDataManager) binder;
viewManager.doProcess(MyActivity.this);
}
};
Intent intent = new Intent(MyActivity.this, SyncService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
その解決策を教えてください。前もって感謝します!