转载请注明出处 http://blog.youkuaiyun.com/f123210f/article/details/79375076
service
今天我们来聊一聊 Service,Service 是 Android 四大组件之一,他的使用频率仅次于 Activity。
Service 翻译过来是服务,它是一般都是在后台的默默的为我们完成任务。
开启方式
service 的开启方式有两种。代码如下
// 1.直接开启服务
Intent intent = new Intent(this,TestService.class);
startService(intent);
// 2.绑定服务
Intent intent = new Intent(this,TestService.class);
mConn = new Conn();
bindService(intent, mConn, BIND_AUTO_CREATE);
private class Conn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
// 绑定成功之后的回调方法
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
结束方式
- 在 Service 中结束时,调用 stopSelf() 即可。
- 在 Activity 中结束的话,调用 stopService() 即可。
生命周期
根据开启方式不同,生命周期表现也不同
如果是直接开启,那么生命周期方法主要是 onCreate() -> onStartCommand() -> onDestroy()
如果是绑定开启,那么分为两种情况。
- 一种是服务已经开启,那么只执行 onbind() 方法;
- 另一种是服务还未开启,那么会先执行 onCreate(),然后执行onbind。
Service 具体的生命周期方法如下:
// 创建时调用
@Override
public void onCreate() {
super.onCreate();
System.out.println("creat");
}
// 开启服务时调用
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
// 绑定时调用
@Nullable
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return new MIBinder();
}
// 解绑时调用
@Override
public boolean onUnbind(Intent intent) {
System.out.println("onUnBind");
return super.onUnbind(intent);
}
// 销毁时调用
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
各个方法的调用时机已经清楚的标注了,需要注意几个小问题。
- 绑定时,如果 service 还未开启,那么会自动执行 onCreate() 方法,然后在执行 onBind()
- 解绑时,如果是 上边 1 中说到的情况,那么执行 onUnbind() 之后会自动执行 onDestroy() 方法
- 服务只能被绑定一次,之后绑定是无效的
- 只能解绑一次,多次解绑是会抛出异常的
- 绑定之后,如果activity销毁,那么service会跟着销毁
我们知道,四大组件是不能 new 的。那么如果我们在 Activity 中需要调用 service 中的方法该怎么办?
此时我们使用绑定的开启方式。这样可以使用 Binder 来实现,具体代码如下。
// 开启代码
Intent intent = new Intent(this,TestService.class);
mConn = new Conn();
bindService(intent, mConn, BIND_AUTO_CREATE);
private class Conn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
ITestService mIBinder1 = (ITestService) iBinder;
mIBinder1.doSomething();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
service 代码
public class TestService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new MIBinder();
}
public class MIBinder extends Binder implements ITestService{
@Override
public void doSomething() {
doS();
}
}
private void doS() {
System.out.println("dosomething");
}
}
就是这么简单,现在这个 doS() 方法被我们在 Activity 中调用了。
使用细节
细节一
现在,我们就可以使用 Service 了。当然还有许多小细节是在平时的实践过程中发现的。
这里给大家分享几个小细节。
我们知道,Android 中进程分为五种。分别是:
- 前台进程
- 可视进程
- 服务进程
- 后台进程
- 空进程
当系统内存不足,或者其他意外情况时,系统是按照从下到上的顺序回收的。我们的服务在中间,如何提升他的“地位”呢?
可以将以下代码添加在 onCreate() 方法中
//设为前台进程
startForeground(1, new Notification());
细节二
service 其实也是依赖在主线程的,这样我们就不能做特别耗时的操作。当然我们可以自己开启一个子线程来执行操作。这种就不说了,这里给大家介绍一个 Android 为我们提供好的,IntentService
IntentService 继承于 Service 它有着 Service 的全部特性。唯一的却别是它会在 onCreate() 中自动开启一个线程让我们执行耗时的操作。
IntentService有以下特点:
- 它创建了一个独立的工作线程来处理所有的通过 onStartCommand() 传递给服务的 intents。
- 创建了一个工作队列,来逐个发送 intent 给 onHandleIntent()。
- 不需要主动调用 stopSelf() 来结束服务。因为,在所有的 intent 被处理完后,系统会自动关闭服务。
- 默认实现的 onBind() 返回 null
- 默认实现的 onStartCommand() 的目的是将 intent 插入到工作队列中
虽谨小慎微,但难免疏漏。如有错误,欢迎大家留言指正。
如果对你起到了一丢丢的帮助的话,记得点赞哦!!!
转载请注明出处 http://blog.youkuaiyun.com/f123210f/article/details/79375076