Tv App Develop of TvServer Service

The following article is mainly recording the following develop skills.
–>How to Create a service
–>How to bind/start a service
–>load static lib

1: TvServerService
create looper handler and thread

public class TvServerService extends Service {
    private static final String TAG = "TvServerService";
    private boolean isTvServerStarted = false;
    private TvServerServiceHandler mTvserverHandler = null;
    private static final int StartTvserverService = 1;

    public TvServerService () {
        HandlerThread thread = new HandlerThread("TvServerServiceLooper");
        thread.start();
        mTvserverHandler = new TvServerServiceHandler(thread.getLooper());
    }

    private class TvServerServiceHandler extends Handler {
        public TvServerServiceHandler(Looper looper) {
            super(looper);
        }

        public synchronized void handleMessage(Message msg) {
            if (msg.arg1 == StartTvserverService) {
                Log.d(TAG, "Creating DB wrapper");
                Log.d(TAG, "onBind:startTvServer");
                startTvServer();
                isTvServerStarted = true;
            }
        }
    };
}

2: bind the service

    private void startTvServerService(Context context) {
        Log.d(TAG, "Starting TvServerService");
        Intent tvServerIntent = new Intent(
                "org.droidtv.tvserver.TvServerService");
        context.startService(tvServerIntent);
    }

as the service received the intent, it will do

public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind > isTvServerStarted: " + isTvServerStarted);
        if (isTvServerStarted == false) {
            Message     msg = new Message();
            msg.arg1 = StartTvserverService;
            mTvserverHandler.sendMessage(msg);
        }
        return null;
    }

public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        Log.d(TAG, "onStartCommand > isTvServerStarted:" + isTvServerStarted);
        if (isTvServerStarted == false) {
            Message     msg = new Message();
            msg.arg1 = StartTvserverService;
            mTvserverHandler.sendMessage(msg);
        }
        return Service.START_STICKY;
    }

The Differences of onBind and onStartCommand, reference the following links.
http://www.cnblogs.com/mulisheng/p/4097968.html
http://blog.csdn.net/yuzhiboyi/article/details/7558176
3: load static lib

    private native void startTvServer();

    static {
        try {
            System.loadLibrary("TvMiddlewareCore");
            System.loadLibrary("tvserver");
        } catch (UnsatisfiedLinkError e) {
            Log.e(TAG, "cannot load library due to " + e.getLocalizedMessage());
        }
    }

and the jni implemented the startTvServer()

extern "C" {
    void Java_org_droidtv_tvserver_TvServerService_startTvServer(JNIEnv *env, jobject  obj);
}

void Java_org_droidtv_tvserver_TvServerService_startTvServer(JNIEnv *env, jobject  obj)
{
    LogPrint("starting TvServer");
    TvServerService::instantiate();
}

4: download addr
https://code.csdn.net/gonghuixue/philips_tv_apps/tree/master

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章