• 客户端访问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);
      }

    }

  • 相关阅读:
    页面框架布局
    socket、tcp、udp、http 的认识及区别
    servlet验证码的设置
    java换行符
    如何在jsp里禁止session
    EL和JSTL表达式
    C标签
    request与response
    文件上传与下载—>struts
    页面跳转
  • 原文地址:https://www.cnblogs.com/jiww/p/5604427.html
Copyright © 2020-2023  润新知