gpt4 book ai didi

安卓服务不能独立运行

转载 作者:行者123 更新时间:2023-11-29 22:06:37 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Android Service makes Activity not responding

我使用服务在数据库和 View 之间同步数据。但是服务不能正常工作,每当我使用服务来处理长任务时, View 停止响应(我不能在 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();
}

}

我尝试在客户端 Activity 中绑定(bind)此服务

//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);

请给我解决方案。提前致谢!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com