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

    }

  • 相关阅读:
    牛客网·剑指offer 从尾到头打印链表(JAVA)
    牛客网·剑指offer 替换空格(JAVA)
    简单的用户登录后台程序编写
    牛客网&剑指offer 二维数组中的查找(JAVA)
    洛谷 P1603 斯诺登的密码(JAVA)
    【回溯法】八皇后问题(递归和非递归)
    如何使用SecureCRT让Vim有颜色?
    js 转base64字符串为文件
    springboot 测试类
    oracle 登录、重启服务
  • 原文地址:https://www.cnblogs.com/jiww/p/5604427.html
Copyright © 2011-2022 走看看