调用远程service aidl接口定义

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();
        }
    }
}

 

转载于:https://www.cnblogs.com/znsongshu/p/9355893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值