Service详解
2016-08-26 16:35
357 查看
Service生命周期:
如图:
代码测试:
继承Swevice:
public class Myservice extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.i("woyaokk", "====================onCreate==============");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("woyaokk","====================onBind==============");
return new MyBinder();
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.i("woyaokk", "====================onBind==============");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("woyaokk","====================onStartCommand==============");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("woyaokk","====================onUnbind==============");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i("woyaokk","====================onDestroy==============");
super.onDestroy();
}
class MyBinder extends Binder {
/**
* 获取Service的方法
*/
public Myservice getService(){
return Myservice.this;
}
}
}
注册:
<service android:name=".Myservice"></service>
启动:
Intent intent=new Intent(MainActivity.this,Myservice.class);
MainActivity.this.startService(intent);
停止:
Intent intent=new Intent(MainActivity.this,Myservice.class);
MainActivity.this.stopService(intent);
当使用两次启动方法,一次停止方法,返回:
由此可知:通过startService方法启动Service则会调用OnCreate方法和OnStartCommand方法,并且多次调用startService方法只会调用由此OnCreate,但会掉用多次onStartCommand,销毁时调用onDestory
绑定:
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 建立连接
Myservice.MyBinder binder = (Myservice.MyBinder) service;
myService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// 连接断开
}
};
unbindService(serviceConnection);
声明周期:
由此可知:通过启动通过绑定启动Service调用onCreate和onBind方法,解绑时调用onUnbind和onDestory方法;
注意:
Service和Thread本质上没有半毛钱关系,Service同样运行在主线程中,所以一样不能执行耗时操作,如果需要执行耗时操作,可以开启异步线程或者继承IntentService
如图:
代码测试:
继承Swevice:
public class Myservice extends Service {
@Override
public void onCreate() {
super.onCreate();
Log.i("woyaokk", "====================onCreate==============");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("woyaokk","====================onBind==============");
return new MyBinder();
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Log.i("woyaokk", "====================onBind==============");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("woyaokk","====================onStartCommand==============");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
Log.i("woyaokk","====================onUnbind==============");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.i("woyaokk","====================onDestroy==============");
super.onDestroy();
}
class MyBinder extends Binder {
/**
* 获取Service的方法
*/
public Myservice getService(){
return Myservice.this;
}
}
}
注册:
<service android:name=".Myservice"></service>
启动:
Intent intent=new Intent(MainActivity.this,Myservice.class);
MainActivity.this.startService(intent);
停止:
Intent intent=new Intent(MainActivity.this,Myservice.class);
MainActivity.this.stopService(intent);
当使用两次启动方法,一次停止方法,返回:
由此可知:通过startService方法启动Service则会调用OnCreate方法和OnStartCommand方法,并且多次调用startService方法只会调用由此OnCreate,但会掉用多次onStartCommand,销毁时调用onDestory
绑定:
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 建立连接
Myservice.MyBinder binder = (Myservice.MyBinder) service;
myService = binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
// 连接断开
}
};
Intent intent=new Intent(MainActivity.this,Myservice.class); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);解绑:
unbindService(serviceConnection);
声明周期:
由此可知:通过启动通过绑定启动Service调用onCreate和onBind方法,解绑时调用onUnbind和onDestory方法;
注意:
Service和Thread本质上没有半毛钱关系,Service同样运行在主线程中,所以一样不能执行耗时操作,如果需要执行耗时操作,可以开启异步线程或者继承IntentService
相关文章推荐
- Android IPC进程间通讯机制
- android之定时器AlarmManager
- axis备忘
- Windows XP Service Pack 3 RC1 v.3244 winxp补丁3 提供下载
- Run As Service runassrv.exe 详细参数第1/2页
- 安装MySQL在最后的start service停住了解决方法
- Android IPC机制绑定Service实现本地通信
- android使用Messenger绑定Service的多种实现方法
- asp.net Web Service 接口大量数据传输解决方案
- Android Service判断设备联网状态详解
- Silverlight中动态获取Web Service地址
- android调用web service(cxf)实例应用详解
- 在Android中 获取正在运行的Service 实例
- ASP.NET State service状态服务的问题解决方法
- 卸载ZkeysPHP 后iis网站出现Service Unavailable 解决办法
- IIS Admin Service 服务因 2149647636 (0x80210514) 服务性错误而停止
- Service Temporarily Unavailable的503错误是怎么回事?
- 深入剖析Android系统中Service和IntentService的区别
- Android中Service服务详解(一)
- android教程之service使用方法示例详解