Android通知栏前台服务的实现
作者:几圈年轮 发布时间:2022-10-13 22:57:46
标签:Android,通知栏,前台
一、前台服务的简单介绍
前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下——这就意味着通知只有在这个服务被终止或从前台主动移除通知后才能被解除。
最常见的表现形式就是音乐播放服务,应用程序后台运行时,用户可以通过通知栏,知道当前播放内容,并进行暂停、继续、切歌等相关操作。
二、为什么使用前台服务
后台运行的Service系统优先级相对较低,当系统内存不足时,在后台运行的Service就有可能被回收,为了保持后台服务的正常运行及相关操作,可以选择将需要保持运行的Service设置为前台服务,从而使APP长时间处于后台或者关闭(进程未被清理)时,服务能够保持工作。
三、前台服务的详细使用
创建服务内容,如下(四大组件不要忘记清单文件进行注册,否则启动会找不到服务);
public class ForegroundService extends Service {
private static final String TAG = ForegroundService.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "onBind");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
}
}
创建服务通知内容,例如音乐播放,蓝牙设备正在连接等:
/**
* 创建服务通知
*/
private Notification createForegroundNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 唯一的通知通道的id.
String notificationChannelId = "notification_channel_id_01";
// Android8.0以上的系统,新建消息通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//用户可见的通道名称
String channelName = "Foreground Service Notification";
//通道的重要程度
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
notificationChannel.setDescription("Channel description");
//LED灯
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
//震动
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
//通知小图标
builder.setSmallIcon(R.drawable.ic_launcher);
//通知标题
builder.setContentTitle("ContentTitle");
//通知内容
builder.setContentText("ContentText");
//设定通知显示的时间
builder.setWhen(System.currentTimeMillis());
//设定启动的内容
Intent activityIntent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
//创建通知并返回
return builder.build();
}
启动服务时,创建通知:
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "onCreate");
// 获取服务通知
Notification notification = createForegroundNotification();
//将服务置于启动状态 ,NOTIFICATION_ID指的是创建的通知的ID
startForeground(NOTIFICATION_ID, notification);
}
停止服务时,移除通知:
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
// 标记服务关闭
ForegroundService.serviceIsLive = false;
// 移除通知
stopForeground(true);
super.onDestroy();
}
判断服务是否启动及获取传递信息:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
// 标记服务启动
ForegroundService.serviceIsLive = true;
// 数据获取
String data = intent.getStringExtra("Foreground");
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
来源:https://www.cnblogs.com/jqnl/p/12599905.html


猜你喜欢
- 关于隐藏和覆盖的区别,要提到RTTI(run-time type identification)(运行期类型检查),也就是运行期的多态,当一
- 以一个web项目为例,代码是可以移植的首先要导入mail.jar包,然后创建自己的类1:HTMLSender类package com.txq
- EasyTouch摇杆插件使用,先给大家展示下效果图:Demo展示双指缩放在电脑端无法掩饰,竖屏将就看看吧;插件名叫EasyTouch,有需
- 在Android平台上面,应用程序OOM异常永远都是值得关注的问题。通常这一块也是程序这中的重点之一。这下我就如何解决OOM作一点简单的介绍
- 前言接下来是 Spring Boot 统⼀功能处理模块了,也是 AOP 的实战环节,要实现的课程⽬标有以下 3 个:统⼀⽤户登录权限验证统⼀
- 在Java中可以使用HttpServer类来实现Http服务器,该类位于com.sun.net包下(rt.jar)。实现代码如下:主程序类p
- 本文实例讲述了Android编程实现调用相册、相机及拍照后直接裁剪的方法。分享给大家供大家参考,具体如下:package com.cvte.
- 本文实例讲述了C#创建windows系统用户的方法。分享给大家供大家参考。具体如下:下面的代码可以通过c#创建一个windows的本地系统账
- 访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据
- @SpringBootTest加速单元测试小诀窍PreSpringBoot - 应用程序测试方案随着代码量的争夺,测试类的启动速度变得越来越
- 本文实例为大家分享了用JavaMail发送HTML模板邮件的具体代码,供大家参考,具体内容如下依赖<dependency>&nb
- 1.点击上传按钮进行如下操作,通过表单名称以及input名称获取相应的值,对于上传的文件,使用.files来获取,因为包含文件的上传,所以采
- AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProces
- 一、 序列化和反序列化概念Serialization(序列化)是一种将对象以一连串的字节描述的过程;反序列化deserialization是
- 先看下面的这组字符,如果输出来,它是无法靠右对齐: Source Codestring[] s1 = { "300",
- 在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来
- 概念所谓回文串,就是字符串反转以后和原串相同,如 abba 和 lippil。对于回文串还是比较容易去验证的,从字符数组的两端开始向中间靠拢
- C#将图片2值化示例代码,原图及二值化后的图片如下:原图:二值化后的图像:实现代码:using System;using System.Dr
- 容器适配器我们可以看出,栈中没有空间配置器(内存池),而是适配器适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目
- 本文实例讲述了C#使用round函数四舍五入的方法。分享给大家供大家参考。具体分析如下:C#中的round函数实际上不是真正的四舍五入函数,