• Android 中 Service AIDL使用


     
     
     1.创建两个项目创建两个.aidl文件
    2.在传递值的类里面创建Service并且返回接口:
    服务返回值onBind
    public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return new IMyAidlInterface.Stub() {
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

    }
    };
    }
     
     
     
    3.启动项目一service1将.aidl文件配置到项目中;
     
     
    4.启动项目二service2:启动服务bindService
     
    intentt = new Intent();
    intentt.setComponent(new ComponentName("com.example.com.myapplication", "com.example.com.myapplication.MyService"));
    启动服务
    bindService(intentt, this, Context.BIND_AUTO_CREATE);
    销毁服务
    unbindService(this);
    5.返回一个iMyAidlInterface接口
    /**
    *
    * 连接
    * @param name
    * @param service
    */
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

    iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service);


    }
    6.传递数据
    if(iMyAidlInterface!=null) {
    try {
    iMyAidlInterface.setDate(et.getText().toString());
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    }
    7.接受数据并且应用
    return new IMyAidlInterface.Stub() {
        @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

    }

    @Override
    public void setDate(String data) throws RemoteException {

    MyService.this.data=data;
    }
    };
    
    
     
     
     
     
     
     
    一,简单的启动
     
    1.创建一个项目,创建一个类继承Service
    重写方法
     
     <service android:name="com.example.android_aidl.MyServer"
                android:process=":remote"
               android:exported="true"
                ></service>
     
    2.创建一个项目,启动第一个项目的服务Service
     intentt = new Intent();
     intentt.setComponent(new ComponentName("com.example.android_aidl", "com.example.android_aidl.MyServer"));
    参数为:启动项目的包名,启动项目包名的服务Service
    startService(intentt);
    stopService(intentt);
     
     
     
    二,跨应用绑定Service:AIDL:android接口定义语言
     
    创建.AIDL文件
     
    服务返回值onBind
    public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return new IMyAidlInterface.Stub() {
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

    }
    };
    }
    启动服务
    bindService(intentt, this, Context.BIND_AUTO_CREATE);
    销毁服务
    unbindService(this);
    /**
    * 连接成功
    * @param name
    * @param service
    */
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    // 打印输出
    Log.e("App2 BInd service", "StartService");

    }

    /**
    * 连接失败
    * @param name
    * @param
    */
    @Override
    public void onServiceDisconnected(ComponentName name) {

    }
     
    二,通过:AIDL:传值:
     
    创建两个项目中包名,类名一样的.aidl文件用来交互并且内容也一样
     
    在p2里面向p里面传值:
     
    按钮监听
    if(iMyAidlInterface!=null) {
    try {
    iMyAidlInterface.setDate(et.getText().toString());
    } catch (RemoteException e) {
    e.printStackTrace();
    }
    }
    返回时接受的接口
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {

    iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service);


    }
    在p里面的Service中实现接口并且传值
    @Override
    public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return new IMyAidlInterface.Stub() {
    @Override
    public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

    }

    @Override
    public void setDate(String data) throws RemoteException {

    MyService.this.data=data;
    }
    };
    }
    在Myservic中使用:
     
    @Override
    public void onCreate() {
    super.onCreate();

    new Thread(new Runnable() {
    @Override
    public void run() {

    run=true;
    while (run){

    try {
    Thread.sleep(1000);
    Log.e("service", data);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }


    }

    }
    }).start();



    Log.e("service", "startServier");
    }
     
     
  • 相关阅读:
    redis的安装使用
    重大需求分析开发第一天
    学习进度条-第六周
    学习进度条-第五周
    软件工程个人课程总结
    学习进度条-第十六周
    团队冲刺第二阶段第十天
    团队冲刺第二阶段第十一天
    响当当队-Beta版总结会议
    学习进度条-第十五周
  • 原文地址:https://www.cnblogs.com/huihuizhang/p/5222431.html
Copyright © 2020-2023  润新知