android_58_service_remote_AIDL调用远程服务
来源:互联网 发布:海淘网转运 知乎 编辑:程序博客网 时间:2024/04/29 23:41
AIDL: 安卓接口定义语言,用于进程间通信
步骤:
1.来到提供远程服务的project的文件目录, 将抽取的接口OpenInterface.java直接改成.aidl后缀
2.来到项目中,刷新一下, 注意在aidl文件中,所有东西都是public,所以删除掉报错的public关键字
3.此时,在gen目录的包下,多了一个自动生成的OpenInterface.java
里面有一个public static abstract class Stub extends Binder implements OpenInterface
4.因此,服务里的 中间人 只要extends Stub即可
5.把提供服务的 .aidl文件,直接拷贝到 别的项目里 (注意,.aidl所在的包名不能变, 即需新建一个同名的包)
此时,会出动生成一个OpenInterface.java, 与 提供远程服务的项目中的 是一模一样,内也有中间人 Stub
6.通过Stub.asInterface(IBinder service)转成中间人
效果:
远程服务app:
清单文件:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.sg31.remoteservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> // 注册自定义的 远程服务 <service android:name=".SGRemoteService"> <intent-filter > <action android:name="com.sg31.sgremoteservice"/> </intent-filter> </service> </application></manifest>
OpenInterface.aidl: (注意所在的包)
package com.sg31.remoteservice; interface OpenInterface {void call_tequan();}
服务:
package com.sg31.remoteservice;import com.sg31.remoteservice.OpenInterface.Stub;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class SGRemoteService extends Service {@Overridepublic void onCreate() {// TODO Auto-generated method stubSystem.out.println("sg__create");super.onCreate();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("sg__startCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {System.out.println("bind方法");return new MiddlePerson();}// 中间人class MiddlePerson extends Stub {@Overridepublic void call_tequan() {inner_teQuanMethod();}}public void inner_teQuanMethod() {System.out.println("sg__特权内部的方法");}}
另一个app,通过拷贝过来的aidl文件,调用远程服务内的方法
清单:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sg31.callremoteservice.MainActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动远程服务" android:onClick="startRemoteServiceBtnClicked" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止远程服务" android:onClick="stopRemoteServiceBtnClicked" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绑定远程服务" android:onClick="bindRemoteServiceBtnClicked" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="解绑远程服务" android:onClick="unBindRemoteServiceBtnClicked" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="远程办证" android:onClick="callRemoteServiceMethodBtnClicked" /></LinearLayout>
控制器: (必须指定远程服务的包名)
package com.sg31.callremoteservice;import com.sg31.remoteservice.OpenInterface;import com.sg31.remoteservice.OpenInterface.Stub;import android.support.v7.app.ActionBarActivity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.Menu;import android.view.MenuItem;import android.view.View;public class MainActivity extends ActionBarActivity {private SGServiceConnection conn;OpenInterface openInterface;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);conn = new SGServiceConnection();}public void startRemoteServiceBtnClicked(View v) {// 启动远程服务Intent intent = new Intent();intent.setAction("com.sg31.sgremoteservice");intent.setPackage("com.sg31.remoteservice");startService(intent);}public void stopRemoteServiceBtnClicked(View v) {// 停止远程服务Intent intent = new Intent();intent.setAction("com.sg31.sgremoteservice");intent.setPackage("com.sg31.remoteservice");stopService(intent);}public void bindRemoteServiceBtnClicked(View v) {Intent intent = new Intent();intent.setAction("com.sg31.sgremoteservice");intent.setPackage("com.sg31.remoteservice");bindService(intent, conn, BIND_AUTO_CREATE);}public void unBindRemoteServiceBtnClicked(View v) {unbindService(conn);}class SGServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {System.out.println("sg__on__service__connected");// 把Ibinder中间人对象强转成publicbusinessopenInterface = Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {System.out.println("sg__on__service__disconnected");}}public void callRemoteServiceMethodBtnClicked(View v) {try {// 通过aidl中间人,调用远程服务内的特权方法openInterface.call_tequan();} catch (Exception e) {e.printStackTrace();}}}
0 0
- android_58_service_remote_AIDL调用远程服务
- 远程调用服务
- Hessian-远程服务调用
- aidl远程服务调用
- 调用远程服务
- RPC调用远程服务
- android_59_service_aidl调用远程服务
- jvisualvm 调用远程服务
- Spring的远程服务调用
- 远程服务调用-JMS协议
- python远程调用webservice服务
- java远程服务调用方法
- dubbo 远程服务无法调用
- 采用AIDL调用远程服务
- Java RMI远程服务调用
- dubbo 远程服务调用流程
- JFinal+Hessian调用远程服务
- hessian远程服务调用例子
- TextView不依附ScrollView滚动
- error/utility.c/SystemCommand/2094.
- android中多个LinearLayout平分布局
- 3维迷宫的bfs
- vmstat详解
- android_58_service_remote_AIDL调用远程服务
- NPM常用命令
- LINUX命令总结
- 【前端学习连载日志】BOM顺利学完了
- android 4.4 以太网网络共享功能研究和实现
- c# 关于网站读取excel文档
- spark core 2.0 Executor ClassLoader
- 第一篇博客
- RegisterClass