c++ 層實現binder服務例子,binder例子
查看例子地址:https://github.com/langxianwen/binder-demo
研究了binder服務的實現,想寫個binder例子。
在網上和書上都看到有這類似的例子,照著寫後,發現都不是自己想要的,不太符合源碼的風格。
就從源碼的binder服務精簡了一個。
遇到的問題:
1 、frameworks/native/include/binder/IInterface.h:50: error: undefined reference to 'android::RefBase::~RefBase()'
原因:沒有添加只添加了libbinder 庫,沒添加libutils庫,
2 、ITest.h TestService.h 標頭檔放在哪裡,才能包含。
一開是我是放在framework/av/include/media。
引用時只需#include <media/ITest.h>即可。
但是能不能放在自己定義的任意目錄能?如:./test/include/test/ITest.h
#include "ITest.h" 編譯找不到標頭檔。還需要在Android.mk指定標頭檔路徑
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include/test即可。
3、我的例子裡面列印log是用ALOGE等。有的程式碼程式庫中是LOGE
寫例子心得:儘管幾行代碼我也折騰來兩三天。看會的,永遠不一定就會了,動手才是應道理。
代碼查看地址:https://github.com/langxianwen/binder-demo
git路徑:git clone https://github.com/langxianwen/binder-demo.git
android 在service中怎向activity傳遞資料
一種是像樓主所說的,在Activity裡註冊一個BroadcastReceiver,Service完成某個任務就可以發一個廣播,接收器收到廣播後通知activity做相應的操作。
另一種是,使用bindService來關聯Service和Application,應用.apk裡的所有組件一般情況都運行在同一個進程中,所以不需要用到IPC,bindService成功後,Service的Client可以得到Service返回的一個iBinder引用,具體的參見Service的文檔及onBind的例子,這樣Service的引用就可以通過返回的iBinder對象得到,如
public class LocalService extends Service {
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
之後Client通過這個iBinder對象得到Service對象引用之後,可以直接和Service通訊,比如讀取Service中的值或是調用Service的方法。
曆史上著名的電腦病毒都有什?舉幾個例子?
殭屍病毒