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