当我的活动开始时,它会创建一个充当服务器的服务。服务器从我的桌面上的python脚本接收原始数据。该服务创建并维护DataManagers,该数据管理器处理来自我的python脚本的所有数据,并在一个哈希映射中键入DM的名称。在任何时候,我需要一个活动来绑定到服务请求从服务和进程的一些数据,当活动暂停,停止或被销毁时它将解除绑定。Android服务连接?
我相信我的绑定是正确的,但是当我启动服务时,传递的ServiceConnection总是返回null。任何想法是怎么回事?我的代码是直接从RemoteService类从Android的 由于被盗 〜Aedon
{代码编}
结合到PublicService
@Override public void onCreate(Bundle icicle) {
super.onCreate(icicle);
doBindService();
setContentView(R.layout.homescreen);
initView();
}
public void initView() {
workbenchs = (Gallery)findViewById(R.id.workbenchs);
workbenchs.setMinimumHeight(h/4);
workbenchs.setAdapter(new WorkBenchAdapter(this));
workbenchs.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent it = new Intent(HomeScreen.this, Controller.class);
it.putExtra("workbench", arg2);
startActivity(it);
}
});
}
// Gallery Adapter
public class WorkBenchAdapter extends BaseAdapter {
public WorkBenchAdapter(Context c) { }
public int getCount() {return mBoundService.getNumBenchs();}
public Object getItem(int position) {return position;}
public long getItemId(int position) {return position;}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(HomeScreen.this);
i.setImageBitmap(mBoundService.getWorkbench(position).toBitmap());
i.setLayoutParams(new Gallery.LayoutParams(w/4, h/4));
return i;
}
}
// Service Necessities
public boolean mIsBound = false;
private PublicService mBoundService;
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((PublicService.LocalBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
void doBindService() {
Log.d(TAG, "Binding...");
bindService(new Intent(this, PublicService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
Log.d(TAG, "Bound.");
}
void doUnbindService() {
if (mIsBound) {
Log.d(TAG, "Unbinding...");
unbindService(mConnection);
mIsBound = false;
Log.d(TAG, "Unbound.");
}
}
我的问题的主屏幕活动来自于工作台适配器。当适配器被创建时,它调用get count(它从0开始),但它一直说mBoundService为空。我已经尝试在绑定之前启动服务,并且不会改变任何内容...
服务
<application>
您可以发布有关相关内容的代码? – xandy 2010-12-16 00:33:18对不起这个烂摊子,该死的东西拒绝阻止我给了它。例如/ ** * /注释假设为长行***** – AedonEtLIRA 2010-12-16 16:14:29
要了解如何发布代码,请阅读以下内容:http://stackoverflow.com/editing-help – Cristian 2010-12-16 16:44:34