欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用AIDL方式實(shí)現(xiàn)播放音樂案例

 更新時(shí)間:2018年04月17日 11:56:49   作者:楊天福  
這篇文章主要介紹了Android使用AIDL方式實(shí)現(xiàn)播放音樂案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android使用AIDL方式實(shí)現(xiàn)播放音樂的具體代碼,供大家參考,具體內(nèi)容如下

思路:

① 新建兩個(gè)APP項(xiàng)目或者M(jìn)odule,一個(gè)作為服務(wù)端,一個(gè)作為客戶端,在服務(wù)端創(chuàng)建service  

② 在兩個(gè)module的main文件中分別新建兩個(gè)aidl文件(接口),里邊定義處理音樂的方法

③ 在兩個(gè)AIDL文件定義過方法后在任務(wù)欄給他們makeproject,編譯成Java文件,才能在service和acvitity中使用
interface.Stub需要實(shí)例化,實(shí)現(xiàn)遠(yuǎn)程方法

④.Service中onbind方法,返回的是:interface.Stub。

sevice需要設(shè)置action,不然客戶端service運(yùn)行時(shí)會(huì)報(bào)空指針異常
先處理服務(wù)端:

Mainfest文件中,為intent隱式調(diào)用添加action 

 <service
   android:name=".MusicService"
   android:enabled="true"
   android:exported="true">
   <intent-filter>
   <action android:name="com.work.MusicService"></action>
 </intent-filter>

MusicService。Java

public class MusicService extends Service {

  private MediaPlayer player = null;

  public MusicService() {
  }

  //①實(shí)現(xiàn)已經(jīng)makeproject過的adil接口,重寫里邊自定義定義的三個(gè)方法
  MusicAidlInterface.Stub stub = new MusicAidlInterface.Stub() {
    @Override
    public void paly() throws RemoteException {
      if (player == null) {
        player = MediaPlayer.create(MusicService.this, R.raw.hckz);
      }
      if (player != null && !player.isPlaying()){
        player.start();
      }

    }

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

    }

    @Override
    public void stop() throws RemoteException {
      if(player!=null){
        player.stop();
      }
      try {
        player.prepare();//重新準(zhǔn)備下次播放
      } catch (IOException e) {
        e.printStackTrace();
      }

    }
  };

  @Override
  public IBinder onBind(Intent intent) {
    return stub;
  }
  //② 重寫service銷毀時(shí)的方法

  @Override
  public void onDestroy() {
    super.onDestroy();
    if(player!=null){
      player.stop();
      player.release();//釋放資源,防止失去依托溢出,發(fā)生異常
    }
  }

處理客戶端

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  private Button btn_play, btn_pause, btn_stop, btn_stopservice, btn_stopacvitity;
   MusicAidlInterface service = null;

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

  }

  private void intiView() {
    btn_play = (Button) findViewById(R.id.btn_play);
    btn_pause = (Button) findViewById(R.id.btn_pause);
    btn_stopservice = (Button) findViewById(R.id.btn_stopservice);
    btn_stopacvitity = (Button) findViewById(R.id.btn_exitacvitity);
    btn_stop = (Button) findViewById(R.id.btn_stop);

    btn_play.setOnClickListener(this);
    btn_pause.setOnClickListener(this);
    btn_stop.setOnClickListener(this);
    btn_stopservice.setOnClickListener(this);
    btn_stopacvitity.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    try {
      switch (v.getId()) {
        case R.id.btn_play:
          service.paly();
          break;
        case R.id.btn_pause:
          service.paus();
          break;
        case R.id.btn_stop:
          service.stop();
          break;
        case R.id.btn_stopservice:
          if (conn != null) {
            unbindService(conn);
          }
          break;
        case R.id.btn_exitacvitity:
          finish();
          break;
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    }

  }

  //連接service
  ServiceConnection conn = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder iBinder) {
      service = MusicAidlInterface.Stub.asInterface(iBinder);
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
      service = null;
    }
  };

  //獲取連接BindService的方法
  private void connect() {
    //使用intent的隱式調(diào)用方法
    Intent intent = new Intent("com.work.MusicService");
    //設(shè)置目標(biāo)service的包名
    intent.setPackage("com.work.musicservice");
    bindService(intent, conn, BIND_AUTO_CREATE);
  }
  //重寫acvitity銷毀時(shí)候解除綁定的方法

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (conn != null) {//解除綁定
      unbindService(conn);
    }
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論