开发者

Android service connecting?

When my activity is started it creates a service that acts as a server. The server receives raw data from my python script on my desktop. The Service creates and maintains DataManagers that handle all the data from my python script in a hash map keyed to the DM's name. At any given time I need an activity to bind to the service request some data from the service and process, when the activity pauses, stops or is destroyed it will unbind.

I believe I have the binding correct, but when I start the service the passed ServiceConnection is always returned null. Any idea's whats going on? my code was stolen directly from the RemoteService class from Android Thanks ~Aedon

{Code addendum}

The HomeScreen Activity that binds to the 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 m开发者_如何转开发IsBound = 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."); 
    }
}

My problem comes from the Workbench adapter. When the adapter is created it call get count (which starts as 0), but it keeps saying that the mBoundService is null. I have tried starting the service before binding and that changes nothing...


Did you specify the service tag in your manifest?

In <application> you need to specify the service like this <service android:name="namespace.ServiceName"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜