我们假定两个应用程序为A和B。

我的意图是在B应用工程中创建Aidl,然后在这里实现 aidl里面的方法,然后在A应用工程中去远程调用:

好了:我先去创建 B应用程序:packagecom.king.review;

创建一个aidl文件:IService.aidl (com.king.review.aidl.IService)

package com.king.review.aidl;interface IService{    //设置地址    void setAddress(String address);        //设置国家    void setCountry(String country);        String disPlay();}

在Aidl程序中不要任何访问修饰符,也不要 static,final 来修饰 ,另外其它的数据类型会有所不同,后期再说:

好了,创建完这个aidl文件,然后就需要我去去实现这个aidl接口的方法(这里我把实现aidl接口的方法与暴露接口的服务写在了一起,方便关联):

RemoteService.java (com.king.review.aidl.RemoteService)

package com.king.review.aidl;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;/** * author: king.liu * data:2013-10-9 上午7:07:32 * do:TODO */public class RemoteService extends Service {    public static final String MY_SERIVCE = "com.king.android.MY_REMOTE_SERIVCE";    @Override    public IBinder onBind(Intent arg0) {        return stub;    }    

  ps:这里我把实现aidl文件的方法与定义的属性写在了暴露接口的远程服务中,方便关联与使用。 IService.Stub stub
= new IService.Stub() { String mCountry; String mAddress; @Override public void setCountry(String country) throws RemoteException { this.mCountry = country; } @Override public void setAddress(String address) throws RemoteException { this.mAddress = address; } @Override public String disPlay() throws RemoteException { return "address:"+mAddress+"--------------mCountry:"+mCountry; } };}

文件结构如上图,其中gen目录下的会自动生成,不用管。

另外需要在Androidmenifest.xml文件中去注册这个服务。以及添加过滤(MY_SERVICE)。。。

这样在B程序程序中的准备工作做完了。。。

A工程中准备工作:

直接把B应用程序中aidl文件所在的包以及,这个Aidl文件copy到 A应用程序中。并保存。。。

好了,这样我们就可以去在A应用程序中调用B应用程序提供的接口方法。。。

package com.king.otherreview;import com.king.review.aidl.IService;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.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    public static final String MY_SERIVCE = "com.king.android.MY_REMOTE_SERIVCE";//服务指定的动作    private IService iPerson;        @Override    protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);        Button btn = new Button(this);        setContentView(btn);        btn.setWidth(150);        btn.setHeight(150);        btn.setText("aidl Test");        btn.setVisibility(View.VISIBLE);        btn.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setAction(MY_SERIVCE);                bindService(intent, conn, Service.BIND_AUTO_CREATE);            }        });    }                private ServiceConnection conn = new ServiceConnection() {        @Override        synchronized public void onServiceConnected(ComponentName name, IBinder service) {            iPerson = IService.Stub.asInterface(service);            if(null!=iPerson){                //RPC调用方法                try {                    iPerson.setAddress("shenzhen.guangdong");                    iPerson.setCountry("china");                    String msg = iPerson.disPlay();                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();                } catch (RemoteException e) {                                        e.printStackTrace();                }            }        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };}

文件结构如图。

在onServiceConnedted中传入需要的参数。

然后去绑定这个服务即可。

更多相关文章

  1. Android(安卓)Studio配置javah自动生成头文件
  2. Android开机自启动+屏蔽系统功能
  3. Android中自定义Adapter的基本原理
  4. android sdcard的使用
  5. Android(安卓)29 创建文件夹失败
  6. Android中屏幕适配和样式主题的使用
  7. Android(安卓)Service的生命周期2
  8. 32位机器Ubuntu系统编译android 内核注意修改点
  9. Android(安卓)Binder分析三:Natvie Service的获取和调用

随机推荐

  1. Android夜间模式实现(系统自带)
  2. android 禁止viewpager预加载
  3. 监控android binder size
  4. A20修改顶部状态栏 禁止跳转设置界面
  5. LGame(Android及J2SE游戏引擎)入门示例—
  6. 2019-11-07 Android谷歌支付SDK集成问题
  7. StagefrightPlayer&&AwesomePlayer 初步
  8. cvc-complex-type.2.4.d: 发现了以元素 '
  9. Laravel 创建可以传递参数 Console服务的
  10. laravel 根据不同组织加载不同视图的实现