Android(安卓)Service详解(三) AIDL使用解析
16lz
2022-03-22
aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口。
通过aidl我们可以完成从服务端到客户端的数据通信
在aidl中我们可以声明任意多个方法,除了内建类型(int boolean等)都需要导入,规则如下:
1、Java 原始类型不需要导入。
2、String、Lsit、Map 和 CharSequence 不需要导入。
创建aidl文件,New->file->文件名.aidl,添加如下代码:
package com.example.new1;interface INewService { void setAge(int age); int getAge(); void setName(String name); String getName();}
点击保存,刷新工程,会在gen下自动产生java代码。
(产生的代码有时候没有缩进,可以右键->Source->Format进行设置)
在生成的代码中又一个Stud类,他继承于IBinder。可以把它作为Service的onBind的返回值,一旦这个Service被其他程序的服务绑定,就将这个IBinder类实例发送出去,同样这个实例里重写的方法数据也跟着一起发送出去。
新建一个Servers,命名为NewService.java
里面声明一个Stub类,完成上面定义的四个函数,代码如下:
package com.example.new1;import com.example.new1.INewService.Stub;import android.app.Service;import android.content.Intent;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;import android.widget.Toast;public class NewService extends Service { private String name="www.51ct0.com"; private int age=18; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub Toast.makeText(NewService.this, "onBind", Toast.LENGTH_LONG).show(); Log.i("SERVICE","onbind"); return mbinder; //返回接口 } public void onCreate() { super.onCreate(); Log.i("SERVICE","oncreat"); } public void onStart(Intent intent,int startId) { Log.i("SERVICE","onstart"); } public void onDestroy() { Log.i("SERVICE","ondestory"); } private INewService.Stub mbinder = new Stub() { @Override //实现接口定义的函数 public void setAge(int age) throws RemoteException { // TODO Auto-generated method stub NewService.this.age = age; } @Override public int getAge() throws RemoteException { // TODO Auto-generated method stub return NewService.this.age; } @Override public void setName(String name) throws RemoteException { // TODO Auto-generated method stub NewService.this.name=name; } @Override public String getName() throws RemoteException { // TODO Auto-generated method stub return NewService.this.name; } };}
到目前为止,已经实现了接口中的全部函数,下面,将实现客户端的调用:
新建一个Activity.java,代码如下:
package com.example.new1;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;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.util.Log;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import android.os.Build;public class MainActivity extends Activity { private TextView textview; private INewService inewservice;//声明接口 private ServiceConnection conn=new ServiceConnection() { @Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { // TODO Auto-generated method stub inewservice = INewService.Stub.asInterface(arg1);//获得接口 try { inewservice.setName("我是Activity"); //调用函数 inewservice.setAge(25); textview.setText(inewservice.getName()+inewservice.getAge()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.i("SERVICE","success" ); } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub Log.i("SERVICE","errer"); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1=(Button)this.findViewById(R.id.btn1); textview=(TextView)this.findViewById(R.id.mytext1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub bindService(new Intent(MainActivity.this,NewService.class),conn,BIND_AUTO_CREATE); } }); }}
运行结果如下:
更多相关文章
- 从Android项目学习Kotlin(一)
- android的binder机制研究(C++部分)
- 如何获取Android源码
- kotlin协程库报错“Program type already present”解决
- Android之GridView简单说明
- Android基于XMPP Smack Openfire开发IM(2)登录openfire
- Android高手进阶教程(二)之----Android(安卓)Launcher抽屉类Slid
- android aidl摘要
- Android应用的跨语言调用小结