Android studio 查看aidl定义的文件:当你进入你的AIDL文件并编写好了之后,点击AS上方菜单栏中的Build->Make Project,之后便可以在当前工程的app/build/generated/source/aidl/debug中找到系统为我们生成的.java文件了。

Service端

  <service android:name="com.atguigu.l07_service.remote.MyRemoteService">
            <intent-filter>
                <action android:name="com.atguigu.l07_service.remote.MyRemoteService.Action"/>
            </intent-filter>
        </service>
public class MyRemoteService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("TAG", "onBind()");
        return new StudentService();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TAG", "onUnbind()");
        return super.onUnbind(intent);
    }

    //处理Student相关的业务逻辑类
    class StudentService extends IStudentService.Stub {
        @Override
        public Student getStudentById(int id) throws RemoteException {
            Log.e("TAG", "Service getStudentById() "+id);
            return new Student(id, "Tom", 10000);
        }
    }

}

《------------------------------start定义aidl接口--------------------------------------------》

定义自定义类型Student
//必须实现Parcelable接口
public class Student implements Parcelable {
  private int id;
  private String name;

  get set。。。。

  public int describeContents() { return 0;}
  //将当前对象的属性数据写到Parcel包对象中(也就是打包) 打包解包在服务器端或client端都有可能,根据功能需求分类,如果服务器传出数据,则打包就在服务器端完成,如果客户端传输数据,则打包就在客户端完成
 public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.id);
    dest.writeString(this.name);
  }
  // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口
  public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
    public Student createFromParcel(Parcel source) {
     return new Student(source.readInt(), source.readString());
    }
    public Student[] newArray(int size) {
     return new Student[size];
    }
  };
}
创建文件:Student.aidl
package com.atguigu.service.test.remote;
parcelable Student;    

创建文件:IStudentService.aidl
package com.atguigu.service.test.remote;
import com.atguigu.service.test.remote.Student;

interface IStudentService {
       Student getStudentById(int id);
}

eclipse自动生成一个通信接口类package com.atguigu.service.test.remote;
public interface IStudentService extends android.os.IInterface{
    .......
}

《------------------------------end定义aidl接口--------------------------------------------》

client

public class MainActivity extends Activity {

    private EditText et_aidl_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_aidl_id = (EditText) findViewById(R.id.et_aidl_id);
    }

    private ServiceConnection conn;
    private IStudentService studentService;

    public void bindRemoteService(View v) {

        Intent intent = new Intent(
                "com.atguigu.l07_service.remote.MyRemoteService.Action");
        if (conn == null) {
            conn = new ServiceConnection() {

                @Override
                public void onServiceDisconnected(ComponentName name) {

                }

                @Override
                public void onServiceConnected(ComponentName name,
                        IBinder service) {
                    Log.e("TAG", "onServiceConnected()");
                    studentService = IStudentService.Stub.asInterface(service);
                }
            };
            bindService(intent, conn, Context.BIND_AUTO_CREATE);
            Toast.makeText(this, "绑定Service", 0).show();
        } else {
            Toast.makeText(this, "已经绑定Service", 0).show();
        }

    }

    public void invokeRemote(View v) throws RemoteException {
        if(studentService!=null) {
            int id = Integer.parseInt(et_aidl_id.getText().toString());
            Student student = studentService.getStudentById(id);
            Toast.makeText(this, student.toString(), 0).show();
        }
    }

    public void unbindRemoteService(View v) {
        if (conn != null) {
            unbindService(conn);
            conn = null;
            studentService = null;
            Toast.makeText(this, "解绑Service", 0).show();
        } else {
            Toast.makeText(this, "还未绑定Service", 0).show();
        }
    }
}

调用远程service aidl接口定义的更多相关文章

  1. android 中activity调用远程service中的方法之 aidl的使用

    服务端:只有服务,没有界面 1.编写interface文件,复制到 .aidl 文件中,并去掉其中的public 等修饰符.系统会自动在gen目录下生成对应的java文件  (对应本地调用中的接口文件 ...

  2. Android调用远程Service的参数和返回值都需要实现Parcelable接口

    import android.os.Parcel;import android.os.Parcelable; public class Person implements Parcelable{ pr ...

  3. AIDL —— Android接口定义语言

    AIDL:Android Interface Definition Language,即Android接口定义语言,是Android进程间通信比较常用的一种方式.翻译一下,就是为了让某个Service ...

  4. AIDL使用绑定启动远程Service出现Service Intent must be explicit: Intent

    Intent intent = new Intent(); intent.setAction("remote.MyRemoteService.Action"); 使用AIDL调用远 ...

  5. Android Activity与远程Service的通信学习总结

    当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...

  6. Android开发,Eclipse创建aidl接口时,出错

    Android开发中,当我们需要调用远程Service时,我们一般通过远程接口(RMI)来实现的,而Android的RMI需要AIDL(Android Interface Definition Lan ...

  7. 大仙说道之Android studio实现Service AIDL

    今天要开发过程中要用到AIDL的调用,之前用的eclipse有大量教程,用起来很方便,现在刚换了Android studio,不可否认studio真的很强大,只是很多功能还需要摸索. AIDL(And ...

  8. Android Studio实现Service AIDL

    Android Studio实现Service AIDL [日期:2015-01-02] 来源:Linux社区  作者:teenyboy [字体:大 中 小]       今天要开发过程中要用到AID ...

  9. 一个简单的demo学习Android远程Service(AIDL的使用)

    这是milo很早之前写在论坛上的一个帖子,现在整理出来,milo也复习一下一般来说Android 的四大组件都是运行在同一个进程中的,但远程Service运行在不同的进程里.这进程间的通信是使用了An ...

随机推荐

  1. 富文本ZSSRichTextEditor之趟坑集锦

    富文本ZSSRichTextEditor是iOS原生与网页交互的集大成者,各种交互.自然问题也是多多,这篇文文章陆续更新遇到的奇葩问题. 1.问题1:从头条这种文章里头复制粘贴的文章,里边有图片,我们 ...

  2. javascript总for of和for in的区别?

    for in是ES5标准,for of是ES6标准; for in是遍历对象属性,for of是遍历对象元素. for of兼容性还不够,移动端安卓微信浏览器貌似不支持,苹果的可以;web端IE支持也 ...

  3. Codeforces Gym101606 C.Cued In (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))

    C Cued In 这个题是打球的.都忘了写的什么了... 代码: 1 #include<iostream> 2 #include<cstring> 3 #include< ...

  4. Tyvj——P1952 Easy

    http://www.tyvj.cn/p/1952 描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:(我们来简化一下这个游戏的规则有n次点击要做,成功了就是o,失败了就是 ...

  5. OS | Socket

    TCP 创建socket: int socket(int domain, int type, int protocol); AF = Address FamilyPF = Protocol Famil ...

  6. 1007 Maximum Subsequence Sum

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  7. js获得checkbox选中值及input后面的文本

    原文:http://blog.csdn.net/u014079773/article/details/51865596 js如何获得多个checkbox选中的值及input后面所跟的文本 <!d ...

  8. uibutton 使用settitle后如何修改其中文字对齐方式

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];            btn.frame = CGRectMake(5, s ...

  9. 邁向IT專家成功之路的三十則鐵律 鐵律九:IT人社群互動之道-縮小自己

    身為一位專業的IT人士所要學習的東西實在非常的多,然而對於時間相當有限的我們,最快速的學習方法就是向他人學習,而向他人學習的首要態度就是「縮小自己」.唯有將自己縮小到別人的眼睛裡,才能夠讓他們真心誠意 ...

  10. kubernetes调度之资源耗尽处理配置

    系列目录 本篇将介绍如何使用kubelet处理资源耗尽的情况 当可用的计算机资源非常低的时候,kubelet仍然要保证节点的稳定性.当处理不可压缩的计算机资源(比如内存或磁盘空间)时,这尤其重要,当这 ...