基本步骤
1、构造查询界面:一个输入框 + 一个按钮 + 一个显示框
2、在Activity的onCreate()方法:添加按钮点击事件
3、定义一个数据查询接口IStudent的一个方法 queryStudent()
4、新建Service子类StudentService,在其内部定义一个内部类StundentBinder,
StundentBinder实现IStudent并继承Binder。 >>>>此步是重中之重,非常关键。
5、实现StudentService的onBind(),和具体的查询方法
6、在Activity中新建一个内部类,实现ServiceConnection接口,
添加未实现的方法onServiceConnected和onServiceDisconnected(暂不添加处理逻辑)
7、在Activity中添加IStundent的属性iStundent,在方法onServiceConnected和onServiceDisconnected进行赋值和销毁
8、在Activity中添加ServiceConnection的属性conn,并在onCreate()中实现bindService(),
conn是bindService()所必须的参数。
9、在点击事件处理逻辑中,调用iStundent的查询方法,从服务中获得数据。
10、在Activity中覆写onDestroy,调用unbindService()
===================================================================
下面我们附加上代码:
1、构造查询界面:一个输入框 + 一个按钮 + 一个显示框
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/studentno" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/resultView" />
2、在Activity的onCreate()方法:添加按钮点击事件
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());
3、定义一个数据查询接口IStudent的一个方法 queryStudent()
public interface IStundent { public String queryStudent(int no); }
4、新建Service子类StudentService,在其内部定义一个内部类StundentBinder,
StundentBinder实现IStudent并继承Binder。 >>>>此步是重中之重,非常关键。
public class StudentService extends Service{ private class StundentBinder extends Binder implements IStundent{ public String queryStudent(int no) { return query(no); } } }
5、实现StudentService的onBind(),和具体的查询方法
public String query(int no){ String[] names = {"张飞","李小龙","赵薇"}; if(no>0 && no<4){ return names[no - 1]; } return null; } @Override public IBinder onBind(Intent intent) { return new StundentBinder(); }
6、在Activity中新建一个内部类,实现ServiceConnection接口,
添加未实现的方法onServiceConnected和onServiceDisconnected(暂不添加处理逻辑)
private class StudentServiceConnection implements ServiceConnection{ public void onServiceConnected(ComponentName name, IBinder service) { } public void onServiceDisconnected(ComponentName name) { } }
7、在Activity中添加IStundent的属性iStundent,在方法onServiceConnected和onServiceDisconnected进行赋值和销毁
private IStundent iStundent; public void onServiceConnected(ComponentName name, IBinder service) { iStundent = (IStundent)service; } public void onServiceDisconnected(ComponentName name) { iStundent = null; }
8、在Activity中添加ServiceConnection的属性conn,并在onCreate()中实现bindService(),
conn是bindService()所必须的参数。
private ServiceConnection conn = new StudentServiceConnection(); Intent intent = new Intent(this, StudentService.class); bindService(intent, conn, BIND_AUTO_CREATE);
9、在点击事件处理逻辑中,调用iStundent的查询方法,从服务中获得数据。
public void onClick(View v) { String no = studentno.getText().toString(); String name = iStundent.queryStudent(Integer.valueOf(no)); resultView.setText(name); }
10、在Activity中覆写onDestroy,调用unbindService()
protected void onDestroy() { unbindService(conn); super.onDestroy(); }
相关推荐
目录里AIDLClient为service的使用方,MyApplication为service的提供方;服务很简单,仅提供加法计算;
多次调用bindService()方法并不会导致多次创建服务及绑定(也就是说onCreate()和onBind()方法并不会被多次调用,但是多次执行bindService)
代码实现:activity进行操作,之后Service中打印...2、在Service中要自定义一个Binder类继承自Binder,并在此内部类中写好设置数据的方法 3、activity中获取Binder对象,并完成强转 4、在activity中完成上层数据的设置
音乐播放器学习下bindservice和startService。
简单绑定本地service服务
bindService时序图,可供下载参考,对应博客https://blog.csdn.net/sinat_20059415/article/details/81041522#t0
Android中bindService调用服务的核心代码,有问题欢迎提出,共同进步。
StartService和BindService实现播放音乐的样例
bindService的demo
最近在开发过程中遇到相关问题,在此记录过程及解决思路。
android数据传输的Service BindService的一个demo,Service添加一个计数器,Acticity可以获取当前状态
Android中有两种主要方式使用Service,通过调用Context的startService方法或调用Context的bindService方法,本文只探讨纯bindService的使用,不涉及任何startService方法调用的情况。如果想了解startService相关的...
bindService()方式去使用服务的示例
实例详解Android中startService()和bindService()的区别。 说明详见http://blog.csdn.net/a343902152/article/details/50857316
Android Service与BroadcastReceiver、bindService示例详解,代码非常详细,详解博客网址:http://blog.csdn.net/sunboy_2050/article/details/7364024