Code example for ContextWrapper

Methods: bindServicestartServicesetClass

    public static ServiceToken bindToService(Activity context, ServiceConnection callback) {
        Activity realActivity = context.getParent();
        if (realActivity == null) {
            realActivity = context;
        ContextWrapper cw = new ContextWrapper(realActivity);
        cw.startService(new Intent(cw, DemoService.class));
        ServiceBinder sb = new ServiceBinder(callback);
        if (cw.bindService((new Intent()).setClass(cw, DemoService.class), sb, 0)) {
            sConnectionMap.put(cw, sb);
            return new ServiceToken(cw);
        logMsg("Failed to bind to service"); 
        return null; 
    public static void unbindFromService(ServiceToken token) {
        if (token == null) {
        	logMsg("Trying to unbind with null token");