进程之间的通信AIDL

来源:互联网 发布:软件随想录读后感 编辑:程序博客网 时间:2024/06/06 09:16
在Android应用程序中我们想从一个程序调用另一个程序中的方法获取数据或者使用另一个程序的功能,我们可以通过互发广播或者通过AIDL来实现,现在我们就来说说这个AIDL怎么实现。AIDL通信是通过远程连接Service实现两个进程之间的通信,首先我们在src中创建一个 .aidl系统会自动在gen中生成相关的文件, .aidl 文件中我们可以创建接口来实现我们需要的功能,代码如下:
package com.example.aidldemo;interface example{ int getMax(); void setName(String str);}
接下来创建一个Service,服务代码如下:
import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class MyService extends Service {private Intent intent;public class MyServiceImp extends example.Stub {//提供另一个程序获取数据@Overridepublic int getMax() throws RemoteException {return 10;}//接受另一个程序设置的数据@Overridepublic void setName(String str) throws RemoteException {//通过发送广播传递数据更新UIintent.putExtra("name", str);            sendBroadcast(intent); }}@Overridepublic void onCreate() {super.onCreate();intent = new Intent();          intent.setAction(MainActivity.ACTION_UPDATEUI);  }@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubreturn super.onStartCommand(intent, flags, startId);}@Overridepublic IBinder onBind(Intent intent) {//绑定服务return new MyServiceImp();}}

在清单文件中注册设置一个action,通过action来启动服务

        <service android:name="com.example.aidldemo.MyService" >            <intent-filter>                <action android:name="forServiceAidl" >                </action>            </intent-filter>        </service>

主活动调用代码如下:

import android.app.Activity;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class MainActivity extends Activity {private TextView textView;public static final String ACTION_UPDATEUI = "action.updateUI";UpdateUIBroadcastReceiver broadcastReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView) findViewById(R.id.show_name);// 动态注册广播IntentFilter filter = new IntentFilter();filter.addAction(ACTION_UPDATEUI);broadcastReceiver = new UpdateUIBroadcastReceiver();registerReceiver(broadcastReceiver, filter);}public void startOnClick(View v) {switch (v.getId()) {case R.id.bindService://启动服务(aidl 必须先启动服务才能传递数据)startService(new Intent("forServiceAidl"));break;}}/** * 定义广播接收器(内部类) */private class UpdateUIBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {textView.setText(intent.getStringExtra("name"));}}}

另一个程序怎么来调用,首先创建一个和调用aidl一样的的文件夹,然后把aidl文件拷贝到这个文件夹下,接下来就是主活动的调用代码如下:

import android.app.Activity;import android.app.Service;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.View;import android.widget.TextView;import com.example.aidldemo.example;public class MainActivity extends Activity {private TextView textView;private example Iexample;private int number;//绑定服务接受数据private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Iexample =example.Stub.asInterface(service);if (Iexample != null) {try {number = Iexample.getMax();textView.setText(number +"");Iexample.setName("小马贼扣");} catch (RemoteException e) {e.printStackTrace();}}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView) findViewById(R.id.show_result);}public void startClick (View v){bindService(new Intent("forServiceAidl"), connection, Service.BIND_AUTO_CREATE);}}




0 0
原创粉丝点击