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

}

最新文章

  1. 匹夫细说C#:可以为null的值类型,详解可空值类型
  2. Html5三维全景
  3. MyEclipse修改项目名称后,部署到tomcat问题
  4. shiro连接数据库
  5. Crosswalk入门
  6. MapReduce/Hbase进阶提升(原理剖析、实战演练)
  7. [LeetCode62]Unique Paths
  8. MyEclipse中导出javadoc文档
  9. tcp为什么要三次握手,而不能二次握手?
  10. win7下,使用django运行django-admin.py无法创建网站
  11. scrapy发送邮件
  12. codeforces#1011C. Fly (二分,注意精度)
  13. PureMVC 官方文档翻译(一)
  14. python3+redis问题求解
  15. SET NOCOUNT { ON | OFF }
  16. 我的java学习之旅--一些基础
  17. Annoy 近邻算法
  18. boto3--通过Python的SDK连接aws
  19. PAT甲题题解-1106. Lowest Price in Supply Chain (25)-(dfs计算树的最小层数)
  20. unique_ptr与std::move的使用

热门文章

  1. iOS - Swift NSProcessInfo 系统进程信息
  2. JS 命名冲突
  3. 集群--LVS的DR模型配置
  4. iframe页面调用父窗口JS函数
  5. Android是如何绘制View的
  6. js跨域问题的解决
  7. eclipse里maven install时,报错提示jdk为无效的目标版本:1.7
  8. Sqlserver_时间用法
  9. html文本框(input)不保存缓存记录
  10. [转]Android各大网络请求库的比较及实战