public class MainActivity extends Activity {
private EditText studentno;
private ServiceConnection conn = new StudentServiceConnection();
private IStundent iStundent;
private TextView resultView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); resultView = (TextView) this.findViewById(R.id.resultView);
studentno = (EditText) this.findViewById(R.id.studentno);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new ButtonClickListener());
Intent service = new Intent(this, StudentService.class);
bindService(service, conn, BIND_AUTO_CREATE);
} private class StudentServiceConnection implements ServiceConnection{
public void onServiceConnected(ComponentName name, IBinder service) {
iStundent = (IStundent)service;
}
public void onServiceDisconnected(ComponentName name) {
iStundent = null;
}
} @Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
} private final class ButtonClickListener implements View.OnClickListener{
public void onClick(View v) {
String no = studentno.getText().toString();
String name = iStundent.queryStudent(Integer.valueOf(no));
resultView.setText(name);
}
}
}
public class StudentService extends Service{
private String[] names = {"张飞","李小龙","赵薇"};
private IBinder binder = new StundentBinder(); public String query(int no){
if(no>0 && no<4){
return names[no - 1];
}
return null;
} @Override
public IBinder onBind(Intent intent) {
return binder;
} private class StundentBinder extends Binder implements IStundent{
public String queryStudent(int no) {
return query(no);
}
} }
public interface IStundent {
public String queryStudent(int no);
}

服务bindService()方法启动服务的更多相关文章

  1. Android(java)学习笔记227:服务(service)之服务的生命周期 与 两种启动服务的区别

    1.之前我们在Android(java)学习笔记171:Service生命周期 (2015-08-18 10:56)说明过,可以回头看看: 2.Service 的两种启动方法和区别: (1)Servi ...

  2. Android(java)学习笔记170:服务(service)之服务的生命周期 与 两种启动服务的区别

    1.之前我们在Android(java)学习笔记171:Service生命周期 (2015-08-18 10:56)说明过,可以回头看看: 2.Service 的两种启动方法和区别: (1)Servi ...

  3. 批处理启动vm虚拟机服务 vm12启动无界面启动vm虚拟机系统 windows上如何操作服务 sc net启动关闭服务

    windows(win10)批处理脚本 打开vm虚拟机的服务,并且开启无界面虚拟机 @echo off net start "vds" net start "VMAuth ...

  4. C#检查服务状态和启动关闭服务

    WinForm 判断服务状态,显示服务名称和状态 https://blog.csdn.net/u013063880/article/details/78626200 C#获得服务,判断服务状态,启动服 ...

  5. Linux将程序添加到服务的方法(通用)

    一:咱们通过这篇文章来演示怎么将某个程序作为服务(就类似Windows服务可以开机自动启动),这里以tomcat为例,已经亲测过: 二:步骤(最好用root用户来做这种事情,切换root用户记得su ...

  6. MySQL Install--CentOS 7配置MySQL服务和开启启动

    创建MySQL服务 编辑文件: vim /usr/lib/systemd/system/mysql.service 录入下面内容: PS: 注意修改ExecStart脚本 [Unit]Descript ...

  7. svn启动服务

    bin目录添加到环境变量classpathsvn --version 查看版本svnadmin create D:\\xx 创建本地中央仓库启动svn服务 cmd命令 svnserve -d -r D ...

  8. centos7也支持service命令启动服务吗,对于centos7 中的systemctl和旧的service命令的区别和联系

    一.centos7也支持service命令启动服务吗 CentOS 7.0中一个最主要的改变,就是切换到了systemd.它用于替代红帽企业版Linux前任版本中的SysV和Upstart,对系统和服 ...

  9. WCF服务自我寄宿 Windows服务

    WCF寄宿有自我寄宿跟IIS寄宿 服务代码: [ServiceContract] ---服务契约 public interface ICustomerService { [OperationContr ...

随机推荐

  1. RemoteDisconnected: Remote end closed connection without response

  2. 62.Longest Valid Parentheses(最长的有效括号)

    Level:   Medium 题目描述: Given a string containing just the characters '(' and ')', find the length of ...

  3. 40.Unique Binary Search Trees(不同的二叉搜索树)

    Level:   Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...

  4. vue中 aixos 常用配置 aixos拦截器 interceptors的使用

                                axios的配置   公共路径配置    拦截器的使用 //这个文件是根组件 new Vue,所有所需的模块和对象都要在new Vue之前配置好 ...

  5. vs2010管理员运行

    VS2010  Configuation->Linker->Manifest File->UAC Execution Level-> requireAdministrator

  6. openssl部分解读

    前言 openssl是个开源的加密库.可以对文件进行加密解密. 小知识 术语: 单词:   Encryption  加密   Decryption   解密   ssl 安全socket层 tsl 最 ...

  7. oracle exp不生成dumpfile,预估出实际导出文件的大小。

    目的:在不创建dumpfile前预估出需要的导出文件大小.  适用于export     实验步骤如下:OS:  Linux test20 2.6.18-238.el5 #1 SMP Sun Dec ...

  8. Spring整合Struts2的配置与测试

    整合目的 让Spring的IOC容器管理Struts2的Action 整合步骤 1.新建一个Web项目 2.加入Spring的jar包和添加Spring的配置文件 3.在Web.xml中配置Conte ...

  9. zabbix监控nginx status页面

    在需要添加监控的nginx配置文件中添加下列配置 #zabbix监控nginx配置 location /nginx_status { stub_status on; access_log off; a ...

  10. Java中JDBC连接池&JDBCTemplate

    数据库连接池 概念:其实就是一个容器(集合),存放数据库连接的容器. 当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归 ...