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

    }

  • 相关阅读:
    ubuntu用apt-get安装memcache
    Vagrant error: Your VM has become inaccessible.
    PHP数据类型转换
    vim 树形目录插件NERDTree安装及简单用法
    mysql 导入sql文件,source命令
    linux:vi 替换命令
    svn更改分支名字,move命令
    Subversion命令汇总
    不解压直接查看tar包内容
    ls按时间排序输出文件列表
  • 原文地址:https://www.cnblogs.com/jiww/p/5604427.html
Copyright © 2011-2022 走看看