zoukankan      html  css  js  c++  java
  • 客户端访问AIDLService(远程绑定Service)

    import android.os.Bundle;
    import android.os.IBinder;
    import android.os.RemoteException;
    import android.app.Activity;
    import android.app.Service;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class AIDLClient extends Activity {
      private ICat catService;
      private Button get;
      EditText color;
      EditText weight;
      private ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
          catService = null;

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
          //获取远程Service的onBind方法返回的对象的代理
          catService = ICat.Stub.asInterface(service);
        }
      };

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aidlclient);
        get = (Button) findViewById(R.id.get);
        color = (EditText) findViewById(R.id.color);
        weight = (EditText) findViewById(R.id.weight);
        //创建所需绑定服务的Intent
        Intent intent = new Intent();
        intent.setAction("mediaprovider.action.AIDL_SERVICE");
        //绑定远程服务
        bindService(intent, conn, Service.BIND_AUTO_CREATE);
        get.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            try {
              // 获取并显示远程Service的状态
              color.setText(catService.getColor());
              weight.setText(catService.getWeight()+"");
            } catch (RemoteException e) {
              e.printStackTrace();
            }
            }
        });
      }

      @Override
      protected void onDestroy() {
        super.onDestroy();
        //解除绑定
        this.unbindService(conn);
      }

    }

  • 相关阅读:
    launch edge和latch edge延迟
    FPGA中如何对管脚输入输出信号进行处理?
    黑盒、白盒、灰盒测试的基本概念
    cadence学习三----->焊盘设计
    cadence学习二----->Allegro基本概念
    cadence学习一------>介绍
    zynq DMA控制器
    AXI_DMA IP学习
    AXI4 STREAM DATA FIFO
    The base and high address of the custom IP are not correctly reflected in xparameters.h in SDK
  • 原文地址:https://www.cnblogs.com/jiww/p/5604427.html
Copyright © 2011-2022 走看看