AIDL代碼案例

環(huán)境

開發(fā)工具:Android studio

整體的文件目錄

Paste_Image.png

server端編寫

1、在Java那邊新建aidl文件,會自動生成和Java并列的aidl文件夾,包名也是一致的

Paste_Image.png

會自動生成代碼

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

不用管,加上自己的測試代碼

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
    //測試代碼
    void myTest();
}

2、新建service文件,主要就是返回IBinder對象

public class myService  extends Service{
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("myService--->","onCreate");
    }

    private IBinder iBinder= new IMyAidlInterface.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public void myTest() throws RemoteException {
            Log.e("iiiiiiiii--->","yeyeyyeyeyeyeye");
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return iBinder;
    }
}

3、注冊service

<service android:name="com.renrun.service.myService"
                 android:enabled="true"
                 android:exported="true"
                 android:process=":remote">
            <intent-filter>
                <action android:name="com.renrun.service.myService"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

</service>

android:enabled  當前Activity是否可以被另一個Application的組件啟動:true允許被啟動;false不允許被啟動。
只要有filter,默認值就是true,否則是false

android:exported 其它應用的組件是否可以喚醒service或者和這個service進行交互:true可以,false不可以
只要有filter,默認值就是true,否則是false

android:process 標志在哪個線程,:開頭表示是私有的,會補上包名,和com.renrun.service.remote相比,就是公有和私有的區(qū)別。

action android:name 表示可以通過這個名稱來啟動這個service
category android:name 是默認的模式

client端編寫

1、拷貝service端的aidl整個文件夾到同樣的地方。
2、調用服務,初始化變量

  IMyAidlInterface iMyAidl;
        private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("cyqlog", "onServiceConnected success");
            iMyAidl=IMyAidlInterface.Stub.asInterface(service);  //  1

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("cyqlog", "onServicedisConnected ");
            iMyAidl=null;
        }
    };

調用

 Intent intent = new Intent();
        //綁定服務端的service
        intent.setAction("com.renrun.service.myService");
//        intent.setPackage("com.renrun.service");
        //新版本(5.0后)必須顯式intent啟動 綁定服務
        intent.setComponent(new ComponentName("com.renrun.service","com.renrun.service.myService"));
        startService(intent);
        //綁定的時候服務端自動創(chuàng)建
        bindService(intent,conn, Context.BIND_AUTO_CREATE);

        Boolean b = bindService(intent,conn, Context.BIND_AUTO_CREATE);
        Log.e("i00---00-90------>","1-->"+b);
        new Handler().postDelayed(new Runnable(){
            public void run() {
                try {
                    iMyAidl.myTest();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }, 1000);

特別注意:

按照正常的流程startService(intent);是不需要的,
但是魅族是個奇葩,沒有startService(intent); bindService返回綁定失敗,所以通用寫法一定要加上。

測試運行

先運行server,再運行oo,能看到service日志打印。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,242評論 25 708
  • Jianwei's blog 首頁 分類 關于 歸檔 標簽 巧用Android多進程,微信,微博等主流App都在用...
    justCode_閱讀 5,962評論 1 23
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • 本次講的是學習能力這一的“分析整理知識”, 以前不知道信息與知識的區(qū)別 ,聽了趙周老師的拆書學院的課程才知道其中的...
    陽光下奔跑的孩子閱讀 159評論 0 0
  • 關注“我” ①白描勾勒普通職場人一天工作; ②擇業(yè)面試,職場適應等獨道見解; ③職場媽媽的高效工作、育兒、旅游 享...
    妙樂辣媽職場閱讀 1,496評論 0 1