Android通知栏前台服务的实现

作者:几圈年轮 时间:2022-10-13 22:57:46 

一、前台服务的简单介绍

前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务。前台服务必须给状态栏提供一个通知,它被放到正在运行(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

标签:Android,通知栏,前台
0
投稿

猜你喜欢

  • Java使用条件语句和循环结构确定控制流(实例)

    2022-07-04 04:52:35
  • Android 实现图片生成卷角和圆角缩略图的方法

    2021-08-12 16:14:12
  • Java中checkbox实现跨页多选的方法

    2023-10-14 11:01:20
  • java后台验证码生成的实现方法

    2021-10-01 05:59:26
  • java类加载相关知识总结

    2023-10-19 13:31:57
  • flutter仿微信底部图标渐变功能的实现代码

    2023-08-18 14:31:39
  • JAVA递归生成树形菜单的实现过程

    2023-07-15 08:57:22
  • C/C++ Crypto密码库调用的实现方法

    2021-10-25 23:59:24
  • Unity中的PostProcessScene实用案例深入解析

    2021-06-09 04:57:28
  • EditText限制小数点前后位数的实例

    2023-04-10 03:56:14
  • spring boot2结合mybatis增删改查的实现

    2023-07-07 01:17:31
  • 基于Avalonia实现自定义弹窗的示例详解

    2022-02-27 16:30:45
  • 散列表的原理与Java实现方法详解

    2023-06-06 06:46:29
  • 详解Java线程堆栈

    2021-06-29 03:37:38
  • Java基础篇之反射机制示例详解

    2021-12-08 04:05:25
  • Java编程中的4种代码块详解

    2022-01-04 03:10:20
  • C#文件后缀名的详细介绍

    2022-06-03 15:07:41
  • Java 提取照片的EXIF信息批量重命名

    2023-10-05 14:11:28
  • Spring Boot应用事件监听示例详解

    2022-06-12 22:39:02
  • C# 格式化字符首字母大写的方法

    2022-04-28 00:05:12
  • asp之家 软件编程 m.aspxhome.com