<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                AIDL (Android Interface Definition Language) 是一種IDL 語言,用于生成可以在Android設備上兩個進程之間進行進程間通信(interprocess communication, IPC)的代碼。如果在一個進程中(例如Activity)要調用另一個進程中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。本文簡單介紹AIDL的使用。 1.新建IRemoteService.aidl ~~~ package com.tang.remoteservicedemo; interface IRemoteService { String getInfo(); } ~~~ 從內容中也可以看出,這東西類似一個接口。既然定義了這么一個玩意,那么我們就要去實現它。 2.新建IService“實現”IRemoteService“接口” ~~~ package com.tang.remoteservicedemo; import java.util.Date; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class IService extends Service { private IBinder iBinder = new IRemoteService.Stub() { @Override public String getInfo() throws RemoteException { // TODO Auto-generated method stub return new Date(System.currentTimeMillis()).toLocaleString()+" 來自遠程服務的信息!"; } }; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return iBinder; } } ~~~ 基于Binder的不同進程間通信,Client與Service在不同的進程中,對用戶程序而言當調用Service返回的IBinder接口后,訪問Service中的方法就如同調用自己的函數一樣。 3.配置IService供其他進程調用 ~~~ <service android:name="com.tang.remoteservicedemo.IService" android:process=":remote"> <intent-filter> <action android:name="com.tang.remoteservicedemo.IService"/> </intent-filter> </service> ~~~ 配置一個所屬進程名和一個action。 通過前面三個步驟,這個含有getInfo()方法的service就可以給別人調用了,下面在客戶端調用它 4.新建一個ClientDemo工程將含有IRemoteService.aidl的那個包全部拷貝到src下,只留下aidl文件,其他全刪除。 ![](https://box.kancloud.cn/2016-08-18_57b550bc12ace.jpg) ??????????????![](https://box.kancloud.cn/2016-08-18_57b550bc2b9c9.jpg) 5.新建MainActivity,其他就是綁定service的操作了 ~~~ package com.tang.clientdemo; import java.util.Timer; import java.util.TimerTask; import com.tang.remoteservicedemo.IRemoteService; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MainActivity extends Activity { private IRemoteService iService = null; private boolean isBinded =false; ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub isBinded = false; iService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub iService = IRemoteService.Stub.asInterface(service); isBinded = true; } }; public void doBind() { Intent intent = new Intent("com.tang.remoteservicedemo.IService"); bindService(intent, conn, Context.BIND_AUTO_CREATE); } public void doUnbind() { if (isBinded) { unbindService(conn); iService = null; isBinded = false; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub doBind(); } }).start(); Timer timer = new Timer(); timer.schedule(task, 0, 2000); } TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub if(iService!=null) { try { Log.i("AAA", iService.getInfo()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Log.i("AAA", "iService!=null"); } } }; @Override protected void onDestroy() { // TODO Auto-generated method stub doUnbind(); task.cancel(); super.onDestroy(); } } ~~~ 執行之后的Log如下: ![](https://box.kancloud.cn/2016-08-18_57b550bc42a16.jpg) 為什么會有一個為空的Log呢,因為綁定也是要時間的嘛.... 簡單的AIDL的使用就這么多了 [源碼下載](http://download.csdn.net/detail/tangnengwu/8124283)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看