使用AIDL方式启动Service 实现跨进程通信
来源:互联网 发布:golang http 断点续传 编辑:程序博客网 时间:2024/04/29 09:57
创建aidl文件
注意:aidl文件中可以引用其它aidl文件中定义的接口,但是不能够引用java类文件中
定义的接口。创建时在Project模式在,选中src/main文件夹,new->adil文件新建的aidl
文件需要make才能编译生
// MusicAidlInterface.aidlpackage com.zdsoft.aidlservice1228;// Declare any non-default types here with import statementsinterface 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; } }}
1 0
- 使用AIDL方式启动Service 实现跨进程通信
- AIDL Service实现跨进程通信
- Android使用Aidl实现跨进程通信
- AIDL实现跨进程通信
- AIDL实现跨进程通信
- AIDL实现跨进程通信
- Android---44---使用AIDL Service 实现跨进程调用Service
- 使用AIDL跨进程通信
- AIDL跨进程通信和Service调用
- Android中跨进程通信方式之使用AIDL
- 使用AIDL实现跨进程Service的绑定
- 总结关于Service进程通信和跨进程通信的几种方式,AIDL,Messenger,Binder。
- 使用AIDL实现Android的跨进程通信
- 【随心笔录】Android AIDL使用,实现跨进程通信
- Android跨进程通信 AIDL方式
- AIDL详解(AIDL、Messenger两种方式实现跨进程通信,传递自定义对象)
- AIDL实现服务跨进程通信
- Aidl实现跨进程通信小例子
- Leetcode oj java 54. Spiral Matrix
- 复制文件显示进度
- android webview简单加载echart表格(折线,饼形,柱形)
- python中的多线程和多进程
- A1036. Boys vs Girls (25)
- 使用AIDL方式启动Service 实现跨进程通信
- apache ssl
- 本地文件的批量创建,修改工具类
- android异步的几种方式
- MUI开发时genymotion模拟器连不上Hbuilder解决办法
- Linux 系统安装 mysql
- makefile编写详解
- :before和 :after
- 严重: Servlet.service() for servlet dispatcherServlet threw exception java.lang.IllegalStateException: