1. 程式人生 > >使用AIDL方式啟動Service 實現跨程序通訊

使用AIDL方式啟動Service 實現跨程序通訊

建立aidl檔案
注意:aidl檔案中可以引用其它aidl檔案中定義的介面,但是不能夠引用java類檔案中
定義的介面。建立時在Project模式在,選中src/main資料夾,new->adil檔案新建的aidl
檔案需要make才能編譯生

// MusicAidlInterface.aidl
package com.zdsoft.aidlservice1228;

// Declare any non-default types here with import statements

interface MusicAidlInterface {
     void play();
     void pause();
     void stop();
}
Service中實現介面
package com.zdsoft.aidlservice1228;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;

import java.io.IOException;

public class MusicService extends Service {
    private MediaPlayer mediaPlayer;

    public MusicAidlInterface.Stub binder = new MusicAidlInterface.Stub() {
        @Override
        public void play() throws RemoteException {
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(MusicService.this, R.raw.zoutianyan);
            }
            if (mediaPlayer != null) {
                mediaPlayer.start();
            }
        }

        @Override
        public void pause() throws RemoteException {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
        }

        @Override
        public void stop() throws RemoteException {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
                try {
                    mediaPlayer.prepare();
                    mediaPlayer.seekTo(0);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    };

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

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }
}
AndroidManifest.xml中註冊service並配置intent-filter,
exported="true"允許被其他程式呼叫
<service
            android:name=".MusicService"
            android:enabled="true"
            android:exported="true">
            <intent-filter >
                <action android:name="com.zdsoft.aidlservice.MUSIC_SERVICE"/>
            </intent-filter>
編寫客戶端app呼叫,把xxx.aidl檔案拷貝一份到客戶端(注意包路徑要一致)

在activity中實現播放音樂功能

package com.zdsoft.aidlclient1228;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.zdsoft.aidlservice1228.MusicAidlInterface;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_play, bt_pause, bt_stop, bt_exit, bt_exit_stop;

    private MusicAidlInterface musicAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        listener();

        Intent intent = new Intent();
        intent.setAction("com.zdsoft.aidlservice.MUSIC_SERVICE");
        intent.setPackage("com.zdsoft.aidlservice1228");
        bindService(intent, connection, Service.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (connection != null) {
            unbindService(connection);
        }

    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (musicAidlInterface == null) {
                musicAidlInterface = MusicAidlInterface.Stub.asInterface(service);
            }

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            if (musicAidlInterface != null) {
                musicAidlInterface = null;
            }
        }
    };

    private void initView() {
        bt_play = (Button) findViewById(R.id.bt_play);
        bt_pause = (Button) findViewById(R.id.bt_pause);
        bt_stop = (Button) findViewById(R.id.bt_stop);
        bt_exit = (Button) findViewById(R.id.bt_exit);
        bt_exit_stop = (Button) findViewById(R.id.bt_exit_stop);
    }

    private void listener() {
        bt_play.setOnClickListener(this);
        bt_pause.setOnClickListener(this);
        bt_stop.setOnClickListener(this);
        bt_exit.setOnClickListener(this);
        bt_exit_stop.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_play:
                try {
                    musicAidlInterface.play();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_pause:
                try {
                    musicAidlInterface.pause();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_stop:
                try {
                    musicAidlInterface.stop();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt_exit:
                finish();
                break;
            case R.id.bt_exit_stop:
                try {
                    musicAidlInterface.stop();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                finish();
                break;
        }

    }
}