采用aidl访问远程服务里面的方法

来源:互联网 发布:同性约会软件 编辑:程序博客网 时间:2024/06/05 17:59

一个应用程序一个进程里面 定义一个IService的接口来描述方法

 

如果我们要调用另一个进程服务里面的方法 aidl

(android interface definatioin language)


首先我们定义一个aidl接口

把所有的修饰符去掉。

IService.aidl

package org.china.lee.server;interface IService {void callMethodService();}

服务接口

package org.china.lee.server;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class RemoteServer extends Service {@Overridepublic IBinder onBind(Intent intent) {return new MyBinder();}private class MyBinder extends IService.Stub{@Overridepublic void callMethodService() throws RemoteException {sayHelloInService();}}private void sayHelloInService() {System.out.println("Hello In Service");}@Overridepublic void onCreate() {System.out.println("**** onCreate()");super.onCreate();}@Overridepublic void onDestroy() {System.out.println("**** onDestroy()");super.onDestroy();}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("**** onStartCommand()");return super.onStartCommand(intent, flags, startId);}}


Activity

package org.china.lee.aidldemo;import org.china.lee.server.IService;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class AidlActivity extends Activity {private Button btn;private IService iService;private class MyConn implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {iService=IService.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);super.setContentView(R.layout.activity_aidl);Intent intent=new Intent();intent.setAction("org.china.lee.server.RemoteServer");bindService(intent,new MyConn(),BIND_AUTO_CREATE);btn=(Button) super.findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {iService.callMethodService();} catch (RemoteException e) {e.printStackTrace();}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.aidl, menu);return true;}}

系统清单文件配置

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="org.china.lee.aidldemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="org.china.lee.aidldemo.AidlActivity"            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="org.china.lee.server.RemoteServer"            >            <intent-filter                 >                <action android:name="org.china.lee.server.RemoteServer"/>            </intent-filter>        </service>    </application></manifest>


0 0