Service初步
来源:互联网 发布:新页erp网络破解版 编辑:程序博客网 时间:2024/05/17 06:08
刚刚开始学Service,简短的写一点有关创建和进行数据交互的东西,以便自己以后学起来方便。
下面的这个小程序简单的描述了如何进行访问者和服务之间的交互,界面只有一个按钮,点击之后,会以Toast的形式弹出一个数值,表示当前运行在服务里的值(该值每半秒加1)。
步骤开始:
第一步:创建一个Service
public class ServiceTest extends Service {@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("onBind");return binder;}@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("onCreate");}@Overridepublic void onStart(Intent intent, int startId) {// TODO Auto-generated method stubsuper.onStart(intent, startId);System.out.println("onStart");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem.out.println("onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();System.out.println("onDestroy");quitFlag = true;}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubSystem.out.println("onUnbind");return super.onUnbind(intent);}}
需要注意的是,必须要实现的是onBind方法,返回一个IBinder对象(主要是这个对象来进行交互)。第二布:构造IBinder对象
在Service里构造IBinder对象,是很常见的实现逻辑。把Binder的实现作为Service的内部类。
public class MyBinder extends Binder{public long getCount(){return count;}}
具体应该有些什么方法,完全取决于具体业务。
第三步:在Service的onCreate或onStartCommand方法中,写业务逻辑
public void onCreate() {// TODO Auto-generated method stubsuper.onCreate();System.out.println("onCreate");new Thread(new Runnable(){public void run(){while(!quitFlag){try{count++;Thread.sleep(500);}catch(InterruptedException e){e.printStackTrace();}}}}).start();}
这个count的值,通过Binder返回到访问者方。第四步:在Activity中创建ServiceConnection对象
ServiceConnection用于监听访问者和Service的链接情况,链接成功时执行回调onServiceConnected(ComponentName, IBinder)方法,后面的IBinder方法就是Service中onBind方法返回的对象。断开链接时执行回调onServiceDisconnected方法。
先在Activity中声明Binder对象
ServiceTest.MyBinder binder;
然后声明ServiceConnection对象
ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubSystem.out.println("Service disconnected");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubbinder = (ServiceTest.MyBinder)service;System.out.println("Service & Activity Connected");}};
在onServiceConnected方法中,把Activity中的Binder对象赋值成从Service返回的对象。
之后就可以用Binder对象处理数据了。
第五步:绑定Activity和Service
bindService(intent, connection, BIND_AUTO_CREATE);
经过上面五步,就可以通过Binder对象来操作Service里面的数据了,实现了交互。
- Service初步
- Service初步
- Service初步
- Service初步
- Android学习--Service初步
- Android开发 --- Service 初步
- 初步了解Web Service
- Service初步了解
- Service初步认识
- Service生命周期初步了解
- Service 初步(1)
- android service初步使用
- 初步理解Service组件
- Service的初步理解
- AXIS Web Service 开发初步
- 关于web service 初步问题
- Java Message Service初步了解
- Android开发之Service初步
- What values do the I/O operators (<< and >>) return?
- PPLIVE源代码泄露及两个struts命令执行漏洞
- 我的java思想002:为窗口增加窗口监听器类的三种方法
- 密码修改
- MFC双缓冲绘图
- Service初步
- 功率W与dBm的对照表及关系
- VC中调用matlab引擎
- 使用webmin安装管理vsftpd
- c++ 前向引用详细解说
- 另类提权 虚拟机还原备份文件抓hash
- iPhone网页体验版
- 单鞭天线的长度计算方法
- update-script脚本语法说明(根据android源代码中的recovery.c进行的定义)