Android中AIDL跨进程访问机制
来源:互联网 发布:mysql 0xc000007b 编辑:程序博客网 时间:2024/05/29 08:30
AILD机制:
通俗的讲就是访问别人的服务。
使用情况:
访问别人的第三方服务框架
例如:
自己的Activity访问别人的service
案例:跨应用进程绑定访问service
服务端
1、编写服务端
android_day11_service
PlayMusicService.java
package com.example.android_day11_service;import com.example.android_day11_service.MusicInterface.Stub;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class PlayMusicService extends Service{ /** * 当调用bindService时执行 */ public IBinder onBind(Intent intent) { return new MyBinder(); } //等第二步编写完接口之后 class MyBinder extends Stub{ public void play() throws RemoteException { Log.i("info", "play..."); } public void next() throws RemoteException { Log.i("info", "next..."); } public void pre() throws RemoteException { Log.i("info", "pre..."); } }}
2、编写接口也就是AIDL(MusicInterface.aidl)
MusicInterface.aidl
后缀名改了之后根文件自动生成文件
package com.example.android_day11_service;/** * 当前接口 就是 aidl接口 * 在该接口中需要定义供客户端访问的抽象方法 */interface MusicInterface { void play(); void next(); void pre();}
点击进入根文件自动生成的文件:
3、在清单文件中注册
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android_day11_service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <service android:name="com.example.android_day11_service.PlayMusicService"> <intent-filter > <action android:name="com.tarena.action.MUSIC_SERVICE"/> </intent-filter> </service> </application></manifest>
4、运行安装在手机上
客户端
android_day11_phone
1、新建Activity(PlayMusicActivity.java)
package com.example.android_day11_phone;import com.example.android_day11_service.MusicInterface;import com.example.android_day11_service.MusicInterface.Stub;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.app.Activity;import android.app.Service;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.view.Menu;import android.view.View;public class PlayMusicActivity extends Activity { private ServiceConnection conn; private MusicInterface musicInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_music); // 执行service绑定 Intent intent = new Intent("com.tarena.action.MUSIC_SERVICE"); conn = new ServiceConnection() { public void onServiceDisconnected(ComponentName name) { } // 绑定成功时执行 public void onServiceConnected(ComponentName name, IBinder service) { musicInterface = Stub.asInterface(service); } }; this.bindService(intent, conn, Service.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); this.unbindService(conn); } public void doClick(View view) { try { switch (view.getId()) { case R.id.button1: musicInterface.play(); break; case R.id.button2: musicInterface.next(); break; case R.id.button3: musicInterface.pre(); break; } } catch (RemoteException e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.play_music, menu); return true; }}
2、客户端想要用,要将服务端AIDL文件复制到客户端(包名一致)
3、运行
代码下载:http://download.csdn.net/detail/prince77qiqiqq/9628751
0 0
- Android中AIDL跨进程访问机制
- Android-AIDL(跨进程访问
- android 跨进程访问service方法(AIDL)
- Android跨进程访问(AIDL服务)
- AIDL跨进程访问
- Android中使用aidl跨进程通信
- Android34_跨进程访问AIDL
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android IPC机制(三)在Android Studio中使用AIDL实现跨进程方法调用
- Android Studio中的跨进程访问(aidl)
- Android跨进程访问-Service跨进程访问(AIDL服务)
- Android 中的Binder跨进程通信机制与AIDL
- Android跨进程通信-AIDL
- Android跨进程通信-AIDL
- Android AIDL跨进程通讯
- Android AIDL跨进程通信
- Android AIDL跨进程通信
- 线性代数Lec02: 矩阵消元
- 9.10考试
- 自己写一个可以通过命令安装的cordova插件
- struts报错严重: Dispatcher initialization failed Unable to load configuration.
- uva1639 Candy
- Android中AIDL跨进程访问机制
- 通过经典题目来理解多种闭包实现方式
- 线性代数Lec03:矩阵乘法和逆
- 用struts2框架怎样实现页面载入时即进行查询(经过action)并显示结果
- 【linux和Windows 下的eclipse的安装、配置,eclipse常用快捷键、eclipse断点单步调试】
- ZR facetest
- 文章标题
- amazeui/swiper大法
- 千牛插件开发,千牛SDK开发。