c++ 層實現binder服務例子,binder例子

來源:互聯網
上載者:User

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的方法。
 
曆史上著名的電腦病毒都有什?舉幾個例子?

殭屍病毒
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.