AIDL

进行两个app之间的访问需要用到AIDL.

AIDL的使用类似第二种Service的启动和关联数据首先介绍Service项目:

AndroidStudio建AIDL文件比较简单。右击项目new-->AIDL然后命名即可:

void basicTypes为默认方法。helloAndroidAIDL为自定义接口


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

     String helloAndroidAIDL(String name);// 此次使用的方法
 }

建完AIDL文件后rebuild一下项目,才能方便调用,要不然项目找不到新建的aidl

然后新建一个Service如下

public class MyService extends Service {
    public class MyServiceImpl extends IMyAidlInterface.Stub {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                               double aDouble, String aString) throws RemoteException {

        }

        public String helloAndroidAIDL(String name) throws RemoteException {
            Log.e("aidl", "helloAndroidAIDL heard from : " + name);
            return "Rust: Service01 return value successfully!";
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return new MyServiceImpl();// 返回内部类实例
    }
}

别忘了在配置文件中配置,将AIDL文件配置到MySrevice中

<service android:name=".service.MyService">
    <intent-filter>
        <action android:name="com.mahui.aidlservice.IMyAidlInterface"/>
    </intent-filter>
</service>

MainActivity只需启动该Service即可


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent(this, MyService.class);
        startService(intent);
    }
/*
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent=new Intent(this, MyService.class);
        stopService(intent);
    }*/
}
执行服务的项目写好了接着是访问服务的项目

首先将服务的aidl连同文件夹复制到新项目中


文件的报名一定要跟发送服务app中的AIDL命名一致。

MainActivity如下

public class MainActivity extends AppCompatActivity {
    Button aidlBtn;
    IMyAidlInterface myService;// 服务
    String appName = "unknown";

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myService = IMyAidlInterface.Stub.asInterface(service);// 获取服务对象
            aidlBtn.setEnabled(true);
        }// 连接服务

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        aidlBtn = (Button) findViewById(R.id.aidl_1_btn);
        appName = getPackageName();

        // 我们没办法在构造Intent的时候就显式声明.
        Intent intent = new Intent("com.mahui.aidlservice.IMyAidlInterface");
        // 既然没有办法构建有效的component,那么给它设置一个包名也可以生效的
        intent.setPackage("com.mahui.aidlservice");// the service package
        // 绑定服务,可设置或触发一些特定的事件
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

        aidlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    // AIDL服务调用代码如下:
                    String msg = myService.helloAndroidAIDL(appName);
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

先执行发送Service项目然后执行接收项目点击按钮即可看到展示的吐司


发送服务项目https://github.com/Maliola/AIDLService点击打开链接

访问项目https://github.com/Maliola/AIDLDemo点击打开链接


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值