android aidl

    技术2022-05-20  35

     

     

     

     

     

     

    AIDL(AndRoid接口描述语言)是一种借口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象.

     

     

    1.定义:src/package/IPlayer.aidl文件。

    如:

     

    package com.chonwhite.mplayer;     //--要有包的全称。 interface IPlayer{     void play();     void pause();     void stop();     int getDuration();     int getCurrentTime();     void setCurrent(int cur);     boolean isPlaying(); }

     

    2.定义Service 服务

    public class LocalService extends Service {     private NotificationManager mNM;     private MediaPlayer mPlayer;     IPlayer.Stub stub = new IPlayer.Stub() {         public void play() throws RemoteException {             mPlayer.start();                    }         public void pause() throws RemoteException {             mPlayer.pause();         }         public void stop() throws RemoteException {             mPlayer.stop();         }         public int getDuration() throws RemoteException {             return mPlayer.getDuration();         }         public int getCurrentTime() throws RemoteException {             return mPlayer.getCurrentPosition();         }         public void setCurrent(int cur) throws RemoteException {             mPlayer.seekTo(cur);         }         public boolean isPlaying() throws RemoteException {             return mPlayer.isPlaying();         }     };     @Override     public void onCreate() {         mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);         mPlayer = MediaPlayer.create(this, R.raw.fly);         mPlayer.start();                 showNotification();     }       @Override     public int onStartCommand(Intent intent, int flags, int startId) {         Log.i("LocalService", "Received start id " + startId + ": " + intent);         // We want this service to continue running until it is explicitly         // stopped, so return sticky.         return START_STICKY;     }       @Override     public void onDestroy() {         // Cancel the persistent notification.         mNM.cancel("ServiceStarted", 0);           // Tell the user we stopped.         Toast.makeText(this, "ServiceStoped", Toast.LENGTH_SHORT).show();     }        @Override     public IBinder onBind(Intent intent) {                 return stub;     }       @Override     public boolean onUnbind(Intent intent) {         return super.onUnbind(intent);     }     /**      * Show a notification while this service is running.      */     private void showNotification() {         // In this sample, we'll use the same text for the ticker and the expanded notification         CharSequence text = "LocalServiceStarted";           // Set the icon, scrolling text and timestamp         Notification notification = new Notification(R.drawable.icon, text,                 System.currentTimeMillis());           // The PendingIntent to launch our activity if the user selects this notification         PendingIntent contentIntent = PendingIntent.getActivity(this, 0,                 new Intent(this, Main.class), 0);           // Set the info for the views that show in the notification panel.         notification.setLatestEventInfo(this, "ServiceLable",                        text, contentIntent);           // Send the notification.         // We use a layout id because it is a unique number.  We use it later to cancel.         mNM.notify("LocalServiceStarted", 0, notification);     }      }

     

     

    3.调用Service

        @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);

     

            bindService(new Intent(Main.this, LocalService.class), mConnection, Context.BIND_AUTO_CREATE);

    }

     

     

        void doUnbindService() {          unbindService(mConnection);     }          @Override     protected void onDestroy() {         super.onDestroy();         doUnbindService();     }

     

        private ServiceConnection mConnection = new ServiceConnection() {         public void onServiceConnected(ComponentName className, IBinder service) {             mPlayer = IPlayer.Stub.asInterface(service);             // Tell the user about this for our demo.             Toast.makeText(Main.this, getString(R.string.serviceconnected),                     Toast.LENGTH_SHORT).show();         }         public void onServiceDisconnected(ComponentName className) {             mPlayer = null;             Toast.makeText(Main.this, getString(R.string.serviedisconnected),                     Toast.LENGTH_SHORT).show();         };     };

     

     

    4:Android.mk

     

    添加:

    LOCAL_SRC_FILES := $(call all-java-files-under, src) /     src/com/chonwhite/mplayer/IPlayer.aidl

     

     

     

     

     

     

     

     

     


    最新回复(0)