zoukankan      html  css  js  c++  java
  • aidl 中通过RemoteCallbackList 运用到的回调机制: service回调activity的方法

    说明:我没有写实例代码,直接拿项目中的代码,有点懒了,这里我省略贴出两个aidl文件。

      TtsService extends Service

    1. private final RemoteCallbackList<ITtsCallback> mCallbacks  
    2.            = new RemoteCallbackList<ITtsCallback>();  


    1. private final android.speech.tts.ITts.Stub mBinder = new Stub() {  
    2.   
    3.     public int registerCallback(String packageName, ITtsCallback cb) {  
    4.         if (cb != null) {  
    5.             mCallbacks.register(cb);  
    6.             mCallbacksMap.put(packageName, cb);  
    7.             return TextToSpeech.SUCCESS;  
    8.         }  
    9.         return TextToSpeech.ERROR;  
    10.     }  
    11.   
    12.     public int unregisterCallback(String packageName, ITtsCallback cb) {  
    13.         if (cb != null) {  
    14.             mCallbacksMap.remove(packageName);  
    15.             mCallbacks.unregister(cb);  
    16.             return TextToSpeech.SUCCESS;  
    17.         }  
    18.         return TextToSpeech.ERROR;  
    19.     }  
    20.   
    21.   
    22.     public int speak(String callingApp, String text, int queueMode, String[] params) {  
    23.         ArrayList<String> speakingParams = new ArrayList<String>();  
    24.         if (params != null) {  
    25.             speakingParams = new ArrayList<String>(Arrays.asList(params));  
    26.         }  
    27.         return this.speak(callingApp, text, queueMode, speakingParams);  
    28.     }  
    1. private void dispatchProcessingCompletedCallback(String packageName) {  
    2.     ITtsCallback cb = mCallbacksMap.get(packageName);  
    3.     if (cb == null){  
    4.         return;  
    5.     }  
    6.     //Log.i("TtsService", "TTS callback: dispatch started");  
    7.     // Broadcast to all clients the new value.  
    8.     final int N = mCallbacks.beginBroadcast();  
    9.     try {  
    10.         cb.processingCompleted();  
    11.     } catch (RemoteException e) {  
    12.         // The RemoteCallbackList will take care of removing  
    13.         // the dead object for us.  
    14.     }  
    15.     mCallbacks.finishBroadcast();  
    16.     //Log.i("TtsService", "TTS callback: dispatch completed to " + N);  
    17. }  
    1. @Override  
    2. public void onDestroy() {  
    3.     super.onDestroy();  
    4.   
    5.     // TODO replace the call to stopAll() with a method to clear absolutely all upcoming  
    6.     // uses of the native synth, including synthesis to a file, and delete files for which  
    7.     // synthesis was not complete.  
    8.     stopAll();  
    9.   
    10.     // Unregister all callbacks.  
    11.     mCallbacks.kill();  
    12. }  


    在activity中

    1. mITtscallback = new ITtsCallback.Stub() {  
    2.                 public void processingCompleted() throws RemoteException {  
    3.                     if (listener != null) {  
    4.                         listener.onProcessingCompleted();  
    5.                     }  
    6.                 }  
    7.             };  
    8.   
    9.   result = mITts.registerCallback(mPackageName, mITtscallback);  


    上面只是一个贴代码没有做任何说明,基本的意思我想大家也能通过代码来看懂。


    1. // int N = mCallbacks.beginBroadcast();  
    2. // try {  
    3. // for (int i = 0; i < N; i++) {  
    4. // mCallbacks.getBroadcastItem(i).showResult(mSlot);  
    5. // }  
    6. // } catch (RemoteException e) {  
    7. // Log("" + e);  
    8. // }  
    9. // mCallbacks.finishBroadcast();  


    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    上传一个写的工作中用到的demo


    1. package com.pateo.aidl;  
    2.   
    3. interface ICallback {  
    4.   
    5. void showResult(String result);  
    6.   
    7. }  
    8.   
    9.   
    10. package com.pateo.aidl;  
    11. import com.pateo.aidl.ICallback;  
    12.   
    13. interface IMyService {  
    14.       
    15.     void init(String packageName,String slot);  
    16.     void registerCallback(String packageName,ICallback cb);  
    17.     void unregisterCallback(String packageName,ICallback cb);  
    18.       
    19. }  
    1. package com.pateo.service;  
    2.   
    3. import java.util.HashMap;  
    4.   
    5. import android.app.Service;  
    6. import android.content.Intent;  
    7. import android.os.Handler;  
    8. import android.os.IBinder;  
    9. import android.os.Message;  
    10. import android.os.RemoteCallbackList;  
    11. import android.os.RemoteException;  
    12.   
    13. import com.pateo.aidl.ICallback;  
    14. import com.pateo.aidl.IMyService;  
    15.   
    16. public class VuiService extends Service {  
    17.   
    18.     private RemoteCallbackList<ICallback> mCallbacks = new RemoteCallbackList<ICallback>();  
    19.     private HashMap<String, ICallback> mCallbacksMap = new HashMap<String, ICallback>();  
    20.     private String mSlot = "";  
    21.     private String mPackageName = "";  
    22.   
    23.     @Override  
    24.     public void onStart(Intent intent, int startId) {  
    25.         super.onStart(intent, startId);  
    26.     }  
    27.   
    28.     @Override  
    29.     public IBinder onBind(Intent intent) {  
    30.         return remoteBinder;  
    31.     }  
    32.   
    33.     @Override  
    34.     public void onDestroy() {  
    35.         mHandler.removeMessages(0);  
    36.         mCallbacks.kill();  
    37.         super.onDestroy();  
    38.     }  
    39.   
    40.     @Override  
    41.     public boolean onUnbind(Intent intent) {  
    42.         return super.onUnbind(intent);  
    43.     }  
    44.   
    45.     public void onRebind(Intent intent) {  
    46.         super.onRebind(intent);  
    47.     }  
    48.   
    49.     private IMyService.Stub remoteBinder = new IMyService.Stub() {  
    50.   
    51.         @Override  
    52.         public void init(String packageName,String slot) throws RemoteException {  
    53.             mSlot = slot;  
    54.             mPackageName = packageName;  
    55.               
    56.             //Ä£Ä⿪ʌÆô¶¯Ê¶±ð£¬ÕâÀïµÄ4000ºÁÃëÏ൱ÓÚžøÓèµÄʶ±ð¹ý³ÌµÄʱŒä£¬Õâžö¿ÉÒÔÔÚʶ±ðœá¹ûÀïÃæÈ¥µ÷Óà 
    57.             mHandler.sendEmptyMessageDelayed(0, 4000);  
    58.         }  
    59.   
    60.         @Override  
    61.         public void unregisterCallback(String packageName, ICallback cb) {  
    62.             if (cb != null) {  
    63.                 mCallbacks.unregister(cb);  
    64.                 mCallbacksMap.remove(packageName);  
    65.             }  
    66.         }  
    67.   
    68.         //°üÃû×¢²áµÄ·œÊœ£¬ÕâÑùŸÍ±ÜÃâÁ˺ܶàÓŠÓÃ×¢²á¶ŒÈ¥»Øµ÷£¬ÕâÀïÍš¹ýÓŠÓÞøÓèµÄpackageNameÀŽÆ¥ÅäŸßÌå»Øµ÷ÄÄÒ»žöÓŠÓõÄcallback  
    69.         @Override  
    70.         public void registerCallback(String packageName, ICallback cb) {  
    71.             if (cb != null) {  
    72.                 mCallbacks.register(cb);  
    73.                 mCallbacksMap.put(packageName, cb);  
    74.             }  
    75.         }  
    76.     };  
    77.   
    78.     //ÕâÀïÍš¹ýÓŠÓÞøÓèµÄpackageNameÀŽÆ¥ÅäŸßÌå»Øµ÷ÄÄÒ»žöÓŠÓõÄcallback  
    79.     private void dispatchProcessingCompletedCallback() {  
    80.         ICallback cb = mCallbacksMap.get(mPackageName);    
    81.             if (cb == null){    
    82.                 return;    
    83.             }    
    84.             final int N = mCallbacks.beginBroadcast();    
    85.             try {    
    86.                 cb.showResult(mSlot);  
    87.             } catch (RemoteException e) {    
    88.             }    
    89.             mCallbacks.finishBroadcast();    
    90.     }  
    91.   
    92.     private Handler mHandler = new Handler() {  
    93.         @Override  
    94.         public void handleMessage(Message msg) {  
    95.             dispatchProcessingCompletedCallback();  
    96.             super.handleMessage(msg);  
    97.         }  
    98.     };  
    99. }  
      1. package com.pateo;  
      2.   
      3. import com.pateo.service.VuiService;  
      4. import com.pateo.aidl.ICallback;  
      5. import com.pateo.aidl.IMyService;  
      6. import com.pateo.R;  
      7.   
      8. import android.app.Activity;  
      9. import android.content.ComponentName;  
      10. import android.content.Context;  
      11. import android.content.Intent;  
      12. import android.content.ServiceConnection;  
      13. import android.os.Bundle;  
      14. import android.os.Handler;  
      15. import android.os.IBinder;  
      16. import android.os.Message;  
      17. import android.os.RemoteException;  
      18. import android.view.View;  
      19. import android.view.View.OnClickListener;  
      20. import android.widget.Button;  
      21. import android.widget.TextView;  
      22.   
      23. public class AppActivity extends Activity {  
      24.       
      25.     TextView tv;  
      26.     IMyService myservice ;  
      27.     String mResult ;  
      28.       
      29.     @Override  
      30.     public void onCreate(Bundle savedInstanceState) {  
      31.         super.onCreate(savedInstanceState);  
      32.         setContentView(R.layout.main);  
      33.         tv = (TextView) findViewById(R.id.tv);  
      34.           
      35.       //Ä£Äâ°ŽmodeŒü  
      36.         Button btn =  (Button) findViewById(R.id.startBtn);  
      37.         btn.setOnClickListener(new OnClickListener() {  
      38.             @Override  
      39.             public void onClick(View v) {  
      40.                 Intent intent = new Intent(AppActivity.this,VuiService.class);  
      41.                 bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);  
      42.             }  
      43.         });  
      44.     }  
      45.       
      46.     private ServiceConnection serviceConnection = new ServiceConnection() {  
      47.           
      48.         @Override  
      49.         public void onServiceDisconnected(ComponentName name) {  
      50.             myservice = null;    
      51.         }  
      52.           
      53.         @Override  
      54.         public void onServiceConnected(ComponentName name, IBinder service) {  
      55.             myservice = IMyService.Stub.asInterface(service);  
      56.             try {  
      57.                 if(myservice != null){  
      58.                     myservice.registerCallback("com.pateo",mCallback);  
      59.                     myservice.init("com.pateo","ÖØÌý|»ØžŽ");//ΪÁËÓïÒô¿òŒÜœÓÊÕµœÕâЩŽÊºó°ÑÕâÁœžöŽÊ²åÈë²å²Û£¬ÕâÑùŸÍ¿ÉÒÔ·¶Î§Ð¡Ìážßʶ±ð  
      60.                     //ÕâÀﻹ¿ÉÒÔ×ö°ÑÒ»Œ¶ŽÊÌõÒ²·¢¹ýÈ¥£¬²åÈë²å²Û¡£  
      61.                 }  
      62.             } catch (RemoteException e) {  
      63.             }  
      64.         }  
      65.     };  
      66.       
      67.      /** 
      68.      * serviceµÄ»Øµ÷·œ·š 
      69.      */  
      70.     private ICallback.Stub mCallback = new ICallback.Stub() {  
      71.   
      72.         //µÈŽýʶ±ðœá¹ûÈ»ºóshow³öÀŽ  
      73.         @Override  
      74.         public void showResult(String result) {  
      75.             try {  
      76.                 mResult = result;  
      77.                 Message msgget = Message.obtain();  
      78.                 msgget.what = 1;  
      79.                 mHandler.sendMessage(msgget);  
      80.             } catch (Exception e) {  
      81.             }  
      82.         }  
      83.     };  
      84.       
      85.     private Handler mHandler = new Handler() {  
      86.   
      87.         @Override  
      88.         public void handleMessage(Message msg) {  
      89.             super.handleMessage(msg);  
      90.             switch (msg.what) {  
      91.             case 1:  
      92.                 tv.setText("result : " + mResult);  
      93.                 break;  
      94.             }  
      95.         }  
      96.     };  
      97.       
      98.     @Override  
      99.     protected void onDestroy() {  
      100.         unbindService(serviceConnection);  
      101.         super.onDestroy();  
      102.     }  
      103.   
  • 相关阅读:
    资料工作手册
    这么点破玩艺,昨天我为了学会它,花了六小时
    压力太大,使人过早衰老
    这是真的么。
    我可怜的好友。。。
    操了,上个网怎么就这么憋屈呢
    编程习惯,代码风格,其实很重要
    其实,我并不喜欢脚本语言
    咱也起一卦,看看北京是否还会继续下雨
    我一点不偏激,只是,我从不抱有侥幸思想
  • 原文地址:https://www.cnblogs.com/bluestorm/p/4906913.html
Copyright © 2011-2022 走看看