bindservice用法
来源:互联网 发布:webshell提权 编辑:程序博客网 时间:2024/04/27 17:25
package cn.itcast.studentquery;import cn.itcast.domain.Student;public interface IStudnetQueryService {public Student queryStudent(int no);}
package cn.itcast.studentquery;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.EditText;import android.widget.TextView;import cn.itcast.domain.Student;public class MainActivity extends Activity { /** Called when the activity is first created. */private EditText et_no;private TextView tv_info;private MyServiceConnection conn;private IStudnetQueryService ibinder; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_no = (EditText) findViewById(R.id.et_no); tv_info = (TextView) findViewById(R.id.tv_info); conn = new MyServiceConnection(); //绑定服务 Intent intent = new Intent(this,StudentService.class); bindService(intent, conn, BIND_AUTO_CREATE); } private final class MyServiceConnection implements ServiceConnection{public void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubibinder = (IStudnetQueryService) service;}public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubconn = null;ibinder = null;} } public void query(View v){ String no = et_no.getText().toString(); Student student = ibinder.queryStudent(Integer.valueOf(no)); tv_info.setText(student.toString()); } @Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();unbindService(conn);} }
package cn.itcast.studentquery;import cn.itcast.domain.Student;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;public class StudentService extends Service {private StudentBinder ibinder = new StudentBinder();private Student[] students = new Student[]{new Student(1, "张三丰", 149),new Student(2, "张无忌", 25),new Student(3, "张国荣", 36)};@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn ibinder;}private final class StudentBinder extends Binder implements IStudnetQueryService{public Student queryStudent(int no) {// TODO Auto-generated method stubreturn query(no);}}//查询学生private Student query(int no){return students[no -1];}}
package cn.itcast.domain;public class Student {private int id;private String name;private int age;public Student() {super();// TODO Auto-generated constructor stub}public Student(int id, String name, int age) {super();this.id = id;this.name = name;this.age = age;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}
0 0
- bindservice用法
- bindService用法
- 安卓bindService用法
- BindService的用法
- bindService
- BindService
- BindService
- BindService
- BindService
- BindService
- bindService
- BindService
- BindService()
- 理解BindService
- BindService总结
- AIDL bindService
- 10 BindService
- startService bindService
- 6.地址
- servlet && js 对cookie的操作
- hdu3966 Aragorn's Story 树链剖分
- [leetcode] 学习记录——Compare Version Numbers
- OpenSSL中的STACK_OF
- bindservice用法
- 初识Hadoop
- infomatica错误记录
- 自动选中某行datagrid
- spring与mybatis三种整合方法
- Java UIManager
- 用lua配置你的应用程序
- Java的checked和unchecked异常
- android状态栏一体化(改变状态栏的背景颜色)开源工程推荐