服务的启动、停止和与活动的通信
来源:互联网 发布:淘宝上外贸是正品吗 编辑:程序博客网 时间:2024/05/19 18:47
服务作为安卓的四大组件之一,一般运行在程序后台,适合用于执行不需要与用户交互又需要长期运行的任务。本文主要记录服务的启动与停止以及服务与活动之间的通信。
(1)服务的启动与停止
首先实现一个MyService类继承自Service类,因为目前只需要实现启动与停止服务的功能,所以只是简单地重写几个常用的方法,其中第一个是抽象方法必须要重写,后面讲到服务与活动的通信时会用到,其他三个都是很常见的方法,为了了解服务的启动过程,在logcat中打印出执行过程。
public class MyService extends Service { public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.d("MyService","onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService","onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Log.d("MyService","onDestroy"); }}
至此,服务类就定义完成了,既然服务是四大组件之一就需要在清单文件中注册:
<service android:name=".MyService"/>
接着需要在活动中启动服务,这里采用用两个按钮分别对应实现服务的启动与停止,然后设置单击事件,使用意图的方式实现活动的启动与停止,
public void onClick(View view){ switch (view.getId()){ case R.id.Button_Start: Intent startIntent = new Intent(this,MyService.class); startService(startIntent); break; case R.id.Button_Stop: Intent stopIntent = new Intent(this,MyService.class); stopService(stopIntent); break; default: break; } }
这样,就实现了第一个任务,服务的活动与启动:
(2)服务与活动的通信
服务启动后就默默地在后台运行着,显然如果活动能控制服务执行一些任务的话将使两者联系更加紧密,这就要借助刚才在MyService中的onBind()方法了。
首先讲下大致思路,为实现活动与服务的通信,也即是活动发出一条指令让服务执行下载,服务就去执行下载功能,发出指令同样可以采用启动服务的方式,通过点击一个按钮并设置单击事件来实现,同时用bindService()方法绑定服务和活动,这个方法接收三个参数(具体自己查,没啥难度的):
public void onClick(View view){ switch (view.getId()){ ... case R.id.Button_Bind: Intent bindIntent = new Intent(this,MyService.class); bindService(bindIntent,connection,BIND_AUTO_CREATE); break; case R.id.Button_Unbind: unbindService(connection); break; default: break; } }
其中第二个参数是ServiceConnection的实例,这里在活动中用一个匿名函数实现,ServiceConnection有两个方法onServiceConnected()和onServiceDisconnected()分别会在服务和活动绑定与解绑时执行,绑定时调用的两个方法就是活动要服务执行的两个功能,即开始下载和返回进度,而这两个功能则需要在MyService中预先定义的:
private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d("MainActivity","service connected"); downloadBinder = (MyService.DownloadBinder) service; downloadBinder.startDownload(); downloadBinder.getProgerss(); } @Override public void onServiceDisconnected(ComponentName name) { Log.d("MainActivity","seivice disconnected"); } };
MyService类中定义了上述两个方法:
public class MyService extends Service { private DownloadBinder downloadBinder = new DownloadBinder(); ... class DownloadBinder extends Binder{ public void startDownload(){ Log.d("DownloadBinder","startDownload"); } public void getProgerss(){ Log.d("DownloadBinder","getProgress"); } } @Override public IBinder onBind(Intent intent) { return downloadBinder; }}
这样通过点击按钮就实现了服务与活动通信的功能
- 服务的启动、停止和与活动的通信
- 服务的启动与停止
- Android活动与服务之间的通信
- 线程服务的停止与启动
- MySQL 服务的启动与停止
- tomcat服务启动与停止的监听
- svn服务的停止与启动
- 线程服务的停止与启动
- datastage服务的停止与启动
- 批处理设置服务的启动类型与启动/停止服务
- Oracle9i服务启动和停止的批处理文件
- oracle正常启动和停止的服务
- 管理weblogic服务的启动和停止
- oracle正常启动和停止的服务
- 服务(Service)的停止和启动
- 系统服务的启动和停止,禁用 和自动启动
- JBOSS服务的启动、停止
- 服务Service---并与活动、接收器之间的通信
- java源码分析(1)-Integer
- picasso的使用
- I People Counting
- Centos 6.5 安装Oracle 12C RAC
- Leetcode Reverse String 344
- 服务的启动、停止和与活动的通信
- java.lang.Class.isPrimitive()方法实例
- 电荷泵式开关电源的基本电路
- SQL中的cast和convert的用法和区别
- 党课心得一(杂谈)
- 百度前端学院第一阶段任务五
- 计算机网络:传输层
- eclipse设置自动提示功能
- Android WebView 获取网页数据(html)