一、服务的生命周期

和活动类似,服务也有自己的生命周期,前面使用到的onCreate()、onStartCommand()、 onBind()和onDestroy( )等方法都是在服务的生
命周期内可能回调的方法。

一旦在项目的任何位置调用了Context 的startService()方法,相应的服务就会启动起来,并回调onStartCommand()方法。

如果这个服务之前还没有创建过,onCreate()方法会先于
onStartCommand()方法执行。

服务启动了之后会一直保持运行状态,直到stopService()或
stopSelf()方法被调用。

注意,虽然每调用一次startService()方法,onStartCommand()就
会执行一次,但实际上每个服务都只会存在一个实例。所以不管调用了多少次startService()方法,只需调用一次stopService()或stopSelf()方法,服务就会停止下来了。

当调用了startService() 方法后,又去调用stopService() 方法,这时服务中的onDestroy()方法就会执行,表示服务已经销毁了。

类似地,当调用了bindService()方法后,又去调用unbindService()方 法,onDestroy()方法也会执行。

但是需要注意,我们是完全有可能对一个服务既调用了startService()方法,又调用 了bindService()方法的,这种情况下该如何才能让服务销毁掉呢?

根据Android系统的机制,一个服务只要被启动或者被绑定了之后,就会一直处于运行状态,必须要让以上两种条件同时不满足,服务才能被销毁。所以,这种情况下要同时调用stopService()和unbindService()方法,onDestroy()方法才会执行。

二、使用前台服务

2.1、前言:

服务几乎都是在后台运行的,但是服务的系统优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服务。

如果希望服务可以一直保持运行状态,而不会由于系统内存不足的原因导致被回收,就可以考虑使用前台服务。

前台服务和普通服务最大的区别就在于,它会一直有一个正在运行的图标在系统的状态栏显示,下拉状态栏后可以看到更加详细的信息,非常类似于通知的效果。

当然有时候也可能不仅仅是为了防止服务被回收掉才使用前台服务的,有些项目由于特殊的需求会要求必须使用前台服务,比如说某些天气预报应用,它的服务在后台更新天气数据的同时,还会在系统状态栏一直显示当前的天气信息。

2.2、创建一个前台服务

2.2.1、沿用上一篇博客“Service服务”的项目

2.2.2、修改MyService中的onCreate()方法如下

@Overridepublic void onCreate() {super.onCreate();Log.d("MyService" , "onCreate executed");Intent intent = new Intent(this, MainActivity.class);String CHANNEL_ONE_ID = "com.primedu.cn";String CHANNEL_ONE_NAME = "Channel One";NotificationChannel notificationChannel = null;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);notificationChannel.setShowBadge(true);notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.createNotificationChannel(notificationChannel);}PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);Notification notification = new NotificationCompat.Builder(this).setContentTitle("This is content title").setContentText("This is content text").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources() , R.mipmap.ic_launcher)).setContentIntent(pi).setChannelId(CHANNEL_ONE_ID).build();startForeground(1 , notification);}

2.2.3、Notification启动时,startForeground报错问题解决

第一个权限问题报错
startForeground requires android.permission.FOREGROUND_SERVICE

Manifest增加以下权限:

<manifest ...>...<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />...<application ...>...
</manifest>

第二个问题,Android 8.0以上需要Notification需要设置个Channel
android.app.RemoteServiceException: Bad notification for startForeground

String CHANNEL_ONE_ID = "com.primedu.cn";String CHANNEL_ONE_NAME = "Channel One";NotificationChannel notificationChannel = null;if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);notificationChannel.enableLights(true);notificationChannel.setLightColor(Color.RED);notificationChannel.setShowBadge(true);notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);manager.createNotificationChannel(notificationChannel);}

再新增一句.setChannelId(CHANNEL_ONE_ID)

2.3、启动程序

点击Start Service或Bind Service按钮,MyService 就会以前台服务的模式启动了,并且在系统状态栏会显示一个通知图标,下拉状态栏后可以看到该通知的详细内容,如下所示:

三、使用IntentService

我们知道,服务中的代码都是默认运行在主线程当中的,如果直接在服务里去处理一些耗时的逻辑,就很容易出现ANR ( Application Not Responding)的情况。

所以这个时候就需要用到Android多线程编程的技术了,在服务的每个具体的方法里开启一个子线程,然后在这里去处理那些耗时的逻辑。因此,一个比较标准的服务就可以写成
如下形式:

public class MyService extends Service{...
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {new Thread(new Runnable() {@Overridepublic void run() {//处理具体的逻辑}}).start();return super.onStartCommand(intent , flags , startId);}
}

但是,这种服务一旦启动之后,就会一直处于运行状态,必须调用stopService()或者stopSelf()方法才能让服务停止下来。所以,如果想要实现让一个服务在执行完毕后自动停止
的功能,就可以这样写:

 @Overridepublic int onStartCommand(Intent intent, int flags, int startId) {new Thread(new Runnable() {@Overridepublic void run() {//处理具体的逻辑stopSelf();}}).start();return super.onStartCommand(intent , flags , startId);}

为了可以简单地创建一个异步的、会自动停止的服务,Android 专门提供了一个IntentService类。

新建一个MyIntentService类继承自IntentService,代码如下所示:

package com.example.servicetest;import android.app.IntentService;
import android.content.Intent;
import android.util.Log;import androidx.annotation.Nullable;public class MyIntentService extends IntentService {public MyIntentService(){super("MyIntentService");//调用父类的有参构造函数}@Overrideprotected void onHandleIntent(@Nullable Intent intent) {//打印当前线程的idLog.d("MyIntentService" , "Thread id is" + Thread.currentThread().getId());}@Overridepublic void onDestroy() {super.onDestroy();Log.d("MyIntentService" , "onDestroy executed");}
}这里首先要提供一个无参的构造函数,并且必须在其内部调用父类的有参构造函数。然后要在子类中去实现onHandleIntent()这个抽象方法,在这个方法中可以去处理一些 具体的逻辑,而且不用担心ANR的问题,因为这个方法已经是在子线程中运行的了。这里为了证实一下,我们在onHandleIntent( )方法中打印了当前线程的id。另外根据IntentService的特性,这个服务在运行结束后应该是会自动停止的,所以我们又重写了onDestroy()方法, 在这里也打印了一行日志,以证实服务是不是停止掉了。

由于这里是手动创建的Service,所以需要到AndroidManifest.xml中注册,如下:

修改activity_ main.xml 中的代码,加入一个用于启动MyIntentService这个服务的按钮,如下所示:

 <Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/start_intent_service"android:text="Start IntentService"/>

修改MainActivity 中的代码,如下所示:

package com.example.servicetest;import androidx.appcompat.app.AppCompatActivity;import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private MyService.DownloadBinder downloadBinder;private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) {downloadBinder = (MyService.DownloadBinder)iBinder;downloadBinder.startDownload();downloadBinder.getProgress();}@Overridepublic void onServiceDisconnected(ComponentName componentName) {}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
............Button startIntentService = (Button) findViewById(R.id.start_intent_service);startIntentService.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()){..........case R.id.start_intent_service://打印主线程的idLog.d("MainActivity" , "Thread id is " + Thread.currentThread().getId());Intent intent = new Intent(this, MyIntentService.class);startService(intent);break;default:break;}}
}

在Start IntentService按钮的点击事件里面去启动MyIntentService这个服务,并在这里打印了一下主线程的id,稍后用于和IntentService进行比对。

点击Start IntentService按钮后,效果如下:

可以看到,不仅MyIntentService和MainActivity所在的线程id不一样,而且onDestroy()方法也得到了执行,说明MyIntentService 在运行完毕后确实自动停止了。

服务的生命周期和更多技巧相关推荐

  1. [Dubbo3.0.8源码解析系列]-12-全局视野来看Dubbo3.0.8的服务启动生命周期

    目录 12 全局视野来看Dubbo3的服务启动生命周期 12.1 启动方法简介 12.2 启动器启动方法的调用逻辑start() 12.3 应用程序发布器DefaultApplicationDeplo ...

  2. android学习笔记---42_服务的生命周期

    42_服务的生命周期 ------------------------- 与采用Context.startService()方法启动服务有关的生命周期方法 onCreate() onStart() ...

  3. 云服务全生命周期管理

    云服务全生命周期管理主要包括的主要包括需求与设计.开发与封装.发布与上架.服务变更.服务下架等过程. 需求与设计 通过用户访谈了解.挖掘真实需求:根据用户的真实需求进行需求分析,形成<需求分析报 ...

  4. android启动服务的生命周期,android Service启动运行服务 生命周期

    Service Android中的service类似于windows中的service,service一般没有用户操作界面,它运行于系统中不容易被用户发觉, 可以使用它开发如监控之类的程序. 一.步骤 ...

  5. android启动服务的生命周期,Android Service的两种启动方式以及生命周期

    Service的两种启动方式: 1.startService 2.bindService 注意: .在Android 5.0之后google出于安全的角度禁止了隐式声明Intent来启动Service ...

  6. android服务进阶,我的Android进阶之旅------Android服务的生命周期回调方法

    先引用一段官网上的文字 ======================================================================================== ...

  7. 神策数据张涛:企业服务客户全生命周期运营三步曲:执行反馈

    本文根据神策数据副总裁张涛关于企业服务客户全生命周期系列的直播内容整理,共 3 篇,本篇主要内容如下: 解决方案的落地与执行 解决方案执行效果反馈与分析 反向检验关键问题和解决方案的有效性 全生命周期 ...

  8. Android---Service(生命周期、启动方式、服务通信、实战演练、思维导图、高级音乐播放器-源码)

    目   录 一.服务的创建 二.服务的生命周期 三.服务的启动方式 (1)startService 方式 启动 服务 实战演练---startService (2)bindService 方式 启动 ...

  9. 18_Android中Service的生命周期,远程服务,绑定远程服务,aidl服务调用,综合服务案例,编写一个应用程序调用远程支付宝远程服务场景

    ============================================================================ 服务的生命周期: 一.采用start的方式开始 ...

最新文章

  1. mysql导入分卷_php实现mysql备份恢复分卷处理的方法_PHP
  2. python爬虫框架排行榜-Python爬虫框架--pyspider初体验
  3. python语言自学-为什么建议大家都学习Python语言?原因在这
  4. 什么是1st tier conference?
  5. 【转】在ASP.NET Web API 2中使用Owin基于Token令牌的身份验证
  6. 1年排名前进13位 ,这个论题成顶会新宠!
  7. .Net Mvc Automated Migration 数据迁移
  8. 2018年宝鸡市三检文科数学题目解答
  9. phpstudy 提示安装VC9 VC14等,MySQL启动失败红色小点点,解决办法。PHP VC版本下载集合
  10. 在线文本字符串转十六进制工具
  11. 权限系统组织管理—详细设计说明书
  12. deepinIDEA快捷方式无法启动解决
  13. JDF bean模块想法交流
  14. 2008 go server sql 批处理_Transact-SQL批处理
  15. 红外额温枪方案开发技术支持
  16. 多种方法解决计算机程序中出现.DLL文件丢失的问题
  17. 特殊矩阵——n阶对称矩阵
  18. div添加阴影(盒子阴影)box-shadow各参数含义
  19. 【转】dd命令详解及利用dd测试磁盘性能
  20. Python安装Tensorflow因为版本低而出现的错误提示conda activate tensorflow-cpu Could not find conda environment: tenso

热门文章

  1. java weka 分类_Weka – 分类
  2. element Tooltip背景颜色样式修改
  3. Linux内核驱动学习(一)编写最简单Linux内核模块HelloWorld
  4. python制作冰花_神奇的冰花
  5. Cesium结合Echarts的使用
  6. 让梦想照进现实-dom事件补充
  7. prototype 小结
  8. Classifying Relations via Long Short Term Memory Networks along Shortest Dependency Paths
  9. Dynamics CRM 365 电子邮箱配置
  10. Android 支付宝快捷支付集成及ALI64错误的有效解决