当前位置:   article > 正文

android通过aidl调用第三方app提供的服务_android jar包和aidl

android jar包和aidl

        一个app需要调用第三方app提供的服务,除了提供jar,aar,contentprovider,activity跳转,scheme跳转,广播发送,还可以通过aidl (Android Interface Definition Language,即Android接口定义语言),通过service对外提供服务,底层原理是使用android系统的Binder,进行进程间的通信。

        本次试验,通过一个叫learn的app提供两个接口服务,1.判断用户名密码是否正确  2.返回用户信息,使用了简单对象和自定义对象两种实现方式,然后另外一个叫test的app来调用learn提供的服务,试验发现如果learn进程没有在运行,那么test是无法调起learn提供的服务的。所以aidl有这个局限性。提供sdk一般通过aar提供接口服务比较好。

       注意事项:在learn服务提供方,定义的aidl和类,在test项目中,包名必须和learn中一致,否则无法调起。

       一、服务端

关于aidl的创建,项目右键-->new-->AIDL-->AIDL file

 

       1.定义aidl

       

服务接口定义IUserAidlInterface.aidl
  1. // IUserAidlInterface.aidl
  2. package com.figo.learn;
  3. // Declare any non-default types here with import statements
  4. import com.figo.learn.User;
  5. interface IUserAidlInterface {
  6. /**
  7. * Demonstrates some basic types that you can use as parameters
  8. * and return values in AIDL.
  9. */
  10. // void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
  11. double aDouble, String aString);
  12. boolean isUserPwdRight(String name,String pwd);
  13. User getUserInfo(String name);
  14. }

        自定义对象User.aidl

  1. // User.aidl
  2. package com.figo.learn;
  3. parcelable User;

        注意自定义类User.java的包名和aidl所在的包名必须一致

  1. package com.figo.learn;
  2. import android.os.Parcel;
  3. import android.os.Parcelable;
  4. /**
  5. * 可序列化用户类
  6. */
  7. public class User implements Parcelable {
  8. private String userName;
  9. private String sex;
  10. private int age;
  11. private String mobile;
  12. private String address;
  13. public User()
  14. {
  15. }
  16. protected User(Parcel in) {
  17. userName = in.readString();
  18. sex = in.readString();
  19. age = in.readInt();
  20. mobile = in.readString();
  21. address = in.readString();
  22. }
  23. public static final Creator<User> CREATOR = new Creator<User>() {
  24. @Override
  25. public User createFromParcel(Parcel in) {
  26. return new User(in);
  27. }
  28. @Override
  29. public User[] newArray(int size) {
  30. return new User[size];
  31. }
  32. };
  33. public String getUserName() {
  34. return userName;
  35. }
  36. public void setUserName(String userName) {
  37. this.userName = userName;
  38. }
  39. public String getSex() {
  40. return sex;
  41. }
  42. public void setSex(String sex) {
  43. this.sex = sex;
  44. }
  45. public int getAge() {
  46. return age;
  47. }
  48. public void setAge(int age) {
  49. this.age = age;
  50. }
  51. public String getMobile() {
  52. return mobile;
  53. }
  54. public void setMobile(String mobile) {
  55. this.mobile = mobile;
  56. }
  57. public String getAddress() {
  58. return address;
  59. }
  60. public void setAddress(String address) {
  61. this.address = address;
  62. }
  63. @Override
  64. public int describeContents() {
  65. return 0;
  66. }
  67. @Override
  68. public void writeToParcel(Parcel dest, int flags) {
  69. dest.writeString(userName);
  70. dest.writeString(sex);
  71. dest.writeInt(age);
  72. dest.writeString(mobile);
  73. dest.writeString(address);
  74. }
  75. public void readFromParcel(Parcel dest) {
  76. //注意,此处的读值顺序应当是和writeToParcel()方法中一致的
  77. userName = dest.readString();
  78. sex = dest.readString();
  79. age = dest.readInt();
  80. mobile = dest.readString();
  81. address = dest.readString();
  82. }
  83. public String toString() {
  84. return "User{" +
  85. "userName='" + userName + '\'' +
  86. ", sex='" + sex + '\'' +
  87. ", age=" + age +
  88. ", mobile='" + mobile + '\'' +
  89. ", address='" + address + '\'' +
  90. '}';
  91. }
  92. }

          这时候,再Build-->ReBuild Project,就会自动生成相关的stub了。

      2.创建service,且注册到androidmanifest.xml

 

UserService.java
  1. package com.figo.learn.service;
  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.os.RemoteException;
  6. import com.figo.learn.IUserAidlInterface;
  7. import com.figo.learn.User;
  8. public class UserService extends Service {
  9. @Override
  10. public IBinder onBind(Intent intent) {
  11. return new UserBinder();
  12. }
  13. public class UserBinder extends IUserAidlInterface.Stub
  14. {
  15. @Override
  16. public boolean isUserPwdRight(String name, String pwd) throws RemoteException {
  17. //这里可以写查询后台逻辑,演示就直接判断了
  18. if("admin".equals(name)&&"123456".equals(pwd))
  19. {
  20. return true;
  21. }else {
  22. return false;
  23. }
  24. }
  25. @Override
  26. public User getUserInfo(String name) throws RemoteException {
  27. User user=new User();
  28. if("admin".equals(name))
  29. {
  30. user.setUserName("张三");
  31. user.setAge(18);
  32. user.setAddress("上海");
  33. user.setMobile("15812345678");
  34. user.setSex("男");
  35. }
  36. return user;
  37. }
  38. }
  39. }

注册服务

  1. <service
  2. android:name=".service.UserService"
  3. android:exported="true">
  4. <intent-filter>
  5. <action android:name="com.figo.learn.user"/>
  6. <category android:name="android.intent.category.DEFAULT"/>
  7. </intent-filter>
  8. </service>

         二.在test项目的客户端

1.将aidl和自定义类,learn中一样的包名,copy过来

同样的rebuild一下项目 

2.测试远程调用

AidlTestActivity.java
  1. package com.figo.test.activity;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.ComponentName;
  4. import android.content.Intent;
  5. import android.content.ServiceConnection;
  6. import android.os.Bundle;
  7. import android.os.IBinder;
  8. import android.os.RemoteException;
  9. import android.view.View;
  10. import android.widget.Toast;
  11. import com.figo.learn.IUserAidlInterface;
  12. import com.figo.learn.User;
  13. import com.figo.test.R;
  14. public class AidlTestActivity extends AppCompatActivity {
  15. private IUserAidlInterface iUserAidlInterface;
  16. private ServiceConnection conn=new ServiceConnection()
  17. {
  18. @Override
  19. public void onServiceConnected(ComponentName name, IBinder service)
  20. {
  21. Toast.makeText(AidlTestActivity.this, "远程服务已经连接,iUserAidlInterface创建成功", Toast.LENGTH_LONG).show();
  22. iUserAidlInterface = IUserAidlInterface.Stub.asInterface(service);
  23. if(iUserAidlInterface!=null)
  24. {
  25. String userName = "admin";
  26. String pwd = "123abc";
  27. boolean isPwdRight = false;
  28. User user=null;
  29. try {
  30. isPwdRight = iUserAidlInterface.isUserPwdRight(userName, pwd);
  31. user=iUserAidlInterface.getUserInfo("admin");
  32. } catch (RemoteException e) {
  33. e.printStackTrace();
  34. }
  35. if (isPwdRight) {
  36. Toast.makeText(AidlTestActivity.this, "用户名密码匹配", Toast.LENGTH_LONG).show();
  37. } else {
  38. Toast.makeText(AidlTestActivity.this, "用户名密码不匹配", Toast.LENGTH_LONG).show();
  39. }
  40. if (user!=null) {
  41. Toast.makeText(AidlTestActivity.this, "获取到用户信息"+user.toString(), Toast.LENGTH_LONG).show();
  42. } else {
  43. Toast.makeText(AidlTestActivity.this, "未获取到用户信息", Toast.LENGTH_LONG).show();
  44. }
  45. }
  46. }
  47. @Override
  48. public void onServiceDisconnected(ComponentName name)
  49. {
  50. iUserAidlInterface=null;
  51. Toast.makeText(AidlTestActivity.this, "远程服务已经断开", Toast.LENGTH_LONG).show();
  52. }
  53. };
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_aidl_test);
  58. //this.bindService();
  59. }
  60. private void bindService()
  61. {
  62. Intent intent=new Intent();
  63. intent.setAction("com.figo.learn.user");
  64. intent.setPackage("com.figo.learn");
  65. bindService(intent, conn , BIND_AUTO_CREATE);
  66. }
  67. public void aidlServiceTest(View view)
  68. {
  69. try {
  70. //如果onCreate未连接成功,再次连接一下看看
  71. // if(iUserAidlInterface==null)
  72. // {
  73. // this.bindService();
  74. // }else {
  75. // if (iUserAidlInterface != null) {
  76. //
  77. // } else {
  78. // Toast.makeText(AidlTestActivity.this, "iUserAidlInterface未创建成功", Toast.LENGTH_LONG).show();
  79. // }
  80. // }
  81. bindService();
  82. }catch (Exception e)
  83. {
  84. e.printStackTrace();
  85. }
  86. }
  87. @Override
  88. protected void onDestroy() {
  89. super.onDestroy();
  90. unbindService(conn);
  91. }
  92. }

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

闽ICP备14008679号