Aidl 定义为接口描述语言,由于Android系统之间不能共享内存,apk与apk之间的通信怎么办呢?这就需要定义一套机制那就是Aidl
少罗嗦直接上效果图和步骤 :开发工具AndroidStudio
1.创建AidlService端
这样创建避免出错 文件的可以随便命名,也可以使用默认的文件名称 点击ok 就创建了一个接口,注意视图和java平级,如图:
在生成的接口文件中编写自己要实现的抽象方法
// IMyAidlInterface.aidl package lg.fangzhuzhu.com.aidl; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); //自动生成的 ,就是说可以传递8种java基本数据类型 double doCalculate(double a,double b); //提供远程的加法 ,自己提供的方法 }
2.搞定后在AndroidStudio编译工程Rebuild Project 编译完成后 我们在看如图所示 就成功了
3 .上面是服务端就完事了 ,其实也很简单哦
4.编写AidlClient代码,创建一个新的Module 为AidlClient ,把刚才在服务端的Aidl文件复制一份到AidlClient 和java视图平级,如图所示:
5.编译module 完后看有和服务端一样的界面说明就成功
6.在客户端创建service
package lg.fangzhuzhu.com.aidlclientdemo; import android.app.Service; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.os.RemoteException; import android.support.annotation.Nullable; import android.util.Log; import lg.fangzhuzhu.com.aidl.IMyAidlInterface; /** * 绑定服务 * Created by lingyuan on 2018/12/9. */ public class myService extends Service { @Override public void onCreate() { super.onCreate(); } @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; //注意这里返回 } @Override public void unbindService(ServiceConnection conn) { super.unbindService(conn); } @Override public void onDestroy() { super.onDestroy(); } protected final IMyAidlInterface.Stub mBinder= new IMyAidlInterface.Stub() { //注意这里 是.stub @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public double doCalculate(double a, double b) throws RemoteException {//这是我在服务端定义的抽象方法在这里实现 Log.d("TAG","远程计算中"); return a+b; } }; } 7.就是绑定服务
package lg.fangzhuzhu.com.aidlclientdemo; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.graphics.Color; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import lg.fangzhuzhu.com.aidl.IMyAidlInterface; public class MainActivity extends AppCompatActivity { EditText et_a,et_b; TextView tv_result; IMyAidlInterface mservice; ServiceConnection connection=new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d("TAG","连接成功"); mservice= IMyAidlInterface.Stub.asInterface(service); } @Override public void onServiceDisconnected(ComponentName name) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bundle args = new Bundle(); Intent intent = new Intent(); intent.setComponent(new ComponentName("lg.fangzhuzhu.com.aidlclientdemo","lg.fangzhuzhu.com.aidlclientdemo.myService"));// 包名,类名 intent.putExtras(args); bindService(intent, connection,BIND_AUTO_CREATE); initView(); } private void initView() { et_a= (EditText) findViewById(R.id.ed_a); et_b= (EditText) findViewById(R.id.et_b); tv_result= (TextView) findViewById(R.id.tv_result); } public void onClick(View view) { double a = Double.parseDouble(et_a.getText().toString().trim()); double b = Double.parseDouble(et_b.getText().toString().trim()); try { if (mservice!=null){ String answer ="计算结果为:"+ mservice.doCalculate(a, b); tv_result.setTextColor(Color.GREEN); tv_result.setText(answer); }else { Toast.makeText(this,"接口为空",Toast.LENGTH_SHORT).show(); } } catch (RemoteException e) { e.printStackTrace(); } } }
8.在清单文件注册 不要忘记
/这里要声明 才能远程调用哦