当前位置:   article > 正文

安卓使用android studio跨进程通信之AIDL_android studio 在两个应用中使用aidl通信

android studio 在两个应用中使用aidl通信

我写这篇文章不想从最基础的介绍开始,我直接上步骤吧.

1.创建服务端

1.1:创建服务端项目:我的as版本比较高,页面就是这样的

1.2:创建AIDL文件,右键项目,选中aidl

 aidl名字可以自定义也可以默认

basicTypes是自带的,可以删掉,也可以不删,然后把你自己所需的接口写上去

 1.3:创建aidl调用的service:

1.4:根据aidl生成java文件供service调用:点击Rebuild Project等待完成

这个就是生成的java文件

1.5:编辑service,实现aidl的接口:如果没有生成java的话,会找不到IReceiverAidl

  1. package com.howfor.receiver
  2. import android.app.Service
  3. import android.content.Intent
  4. import android.graphics.Bitmap
  5. import android.os.IBinder
  6. import android.util.Log
  7. class MyService : Service() {
  8. var catBinder = object: IReceiverAidl.Stub() {
  9. override fun basicTypes(
  10. anInt: Int,
  11. aLong: Long,
  12. aBoolean: Boolean,
  13. aFloat: Float,
  14. aDouble: Double,
  15. aString: String?
  16. ) {
  17. Log.e("TAG","basicTypessetTime===========anInt")
  18. }
  19. override fun setTime(time: Long) {
  20. Log.e("TAG","setTime===========$time")
  21. }
  22. override fun reboot() {
  23. Log.e("TAG","reboot===========")
  24. }
  25. override fun shutdown() {
  26. Log.e("TAG","shutdown===========")
  27. }
  28. override fun install(fullPath: String?) {
  29. Log.e("TAG","install===========$fullPath")
  30. }
  31. override fun beat(aBoolean: Boolean) {
  32. Log.e("TAG","beat===========$aBoolean")
  33. }
  34. override fun setPower(power: MutableList<String>?) {
  35. Log.e("TAG","setPower===========${power!!.size}")
  36. }
  37. override fun updateFirmware(localFile: String?) {
  38. Log.e("TAG","updateFirmware===========${localFile}")
  39. }
  40. override fun getDeviceId(): String {
  41. Log.e("TAG","getDeviceId===========")
  42. return ""
  43. }
  44. override fun getName(): Array<String> {
  45. Log.e("TAG","getName()===========")
  46. val arrayEmpty = emptyArray<String>()
  47. return arrayEmpty
  48. }
  49. override fun getPath(): Array<String> {
  50. Log.e("TAG","getPath()===========")
  51. val arrayEmpty = emptyArray<String>()
  52. return arrayEmpty
  53. }
  54. override fun shouldClean(path: String?): Boolean {
  55. Log.e("TAG","shouldClean()===========")
  56. return true
  57. }
  58. override fun getBitmap(): Bitmap {
  59. Log.e("TAG","getBitmap()===========")
  60. var bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
  61. return bitmap
  62. }
  63. override fun getVersion(): String {
  64. Log.e("TAG","getVersion()===========")
  65. return ""
  66. }
  67. }
  68. override fun onBind(intent: Intent): IBinder {
  69. return catBinder
  70. }
  71. }

1.6:设置service的属性:name会在客户端绑定使用到,所以我们会先自定义一个,不需要跟着你的Myservice一样

  1. <service
  2. android:name=".MyService"
  3. android:exported="true">
  4. <intent-filter>
  5. <action android:name="com.howfor.receiver.service.ReceiverService" />
  6. </intent-filter>
  7. </service>

2.客户端使用:

2.1:创建客户端项目

2.2:复制服务端的aidl到客户端中:必须一模一样的,建议直接复制粘贴

2.3:生成java文件,与服务端1.4一样的步骤:

2.4:使用aidl并且绑定服务端的service:这里的com.howfor.receiver.service.ReceiverService就是1.6设置的属性,包名就是服务端的包名:

  1. package com.example.aidlkhute
  2. import android.annotation.SuppressLint
  3. import android.content.ComponentName
  4. import android.content.Context
  5. import android.content.Intent
  6. import android.content.ServiceConnection
  7. import android.os.Bundle
  8. import android.os.IBinder
  9. import android.os.RemoteException
  10. import android.util.Log
  11. import android.widget.Button
  12. import androidx.appcompat.app.AppCompatActivity
  13. import com.howfor.receiver.IReceiverAidl
  14. class MainActivity : AppCompatActivity() {
  15. var mIReceiverAidl: IReceiverAidl? = null
  16. var mConnections = object : ServiceConnection {
  17. override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
  18. Log.e("TAG", "onServiceConnected")
  19. //注释3:
  20. mIReceiverAidl = IReceiverAidl.Stub.asInterface(service)
  21. }
  22. override fun onServiceDisconnected(name: ComponentName?) {
  23. Log.e("TAG", "onServiceDisconnected")
  24. mIReceiverAidl = null
  25. }
  26. }
  27. @SuppressLint("MissingInflatedId")
  28. override fun onCreate(savedInstanceState: Bundle?) {
  29. super.onCreate(savedInstanceState)
  30. setContentView(R.layout.activity_main)
  31. val intent = Intent()
  32. intent.setPackage("com.howfor.receiver")
  33. intent.action = "com.howfor.receiver.service.ReceiverService"
  34. try {
  35. var se = bindService(intent, mConnections, Context.BIND_AUTO_CREATE)
  36. Log.e("TAG", "se=========$se")
  37. } catch (e: RemoteException) {
  38. Log.e("TAG", "e=========${e.message}")
  39. }
  40. }
  41. override fun onDestroy() {
  42. super.onDestroy()
  43. unbindService(mConnections)
  44. }
  45. }

添加两个按钮使用里面的接口 

  1. findViewById<Button>(R.id.test).setOnClickListener {
  2. if (mIReceiverAidl != null) {
  3. try {
  4. mIReceiverAidl!!.reboot()
  5. } catch (e: RemoteException) {
  6. Log.e("TAG", "e=========${e.message}")
  7. }
  8. } else {
  9. Log.e("TAG", "mIReceiverAidl====null")
  10. }
  11. }
  12. findViewById<Button>(R.id.test1).setOnClickListener {
  13. if (mIReceiverAidl != null) {
  14. try {
  15. mIReceiverAidl!!.shutdown()
  16. } catch (e: RemoteException) {
  17. Log.e("TAG", "e=========${e.message}")
  18. }
  19. } else {
  20. Log.e("TAG", "mIReceiverAidl====null")
  21. }
  22. }

先运行服务端,可以不用做任何操作,然后再运行客户端,点击对应的接口,就会打印出对应的log:

  1. se=========true //代表服务绑定成功
  2. reboot=========== //点击第一个按钮打印
  3. shutdown=========== //点击第二个按钮打印

 还有个小问题:在高版本(targetSdk 33)上会出现绑定失败的情况,然后查看了一下,在安卓11以上,google对系统做了一些操作,具体的可以自行百度:解决方案,在客户端添加红框中的内容即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/182011
推荐阅读
相关标签
  

闽ICP备14008679号