android Foreground Service 前台服务/notification全局通知

前言

我在设计一款自用的用于自我管理的app,为了增强自我反馈,我想要这个软件能够持续的显示我执行某一个事件所用的时间。并且为了使得软件不会被系统自动关闭,百般斟酌后我选了前台服务方案。

要素简介

前台服务(Foreground Service)

前台服务(Foreground Service)相比起后台服务(Service),拥有一个优势那就是在系统内存不足的时候不允许系统杀死的服务,并且在运行时需要能被用户所知,需要在状态栏创建一个通知来管理。

全局通知(notification)

全局通知(notification),常用于告知用户某些事件在某个事件发生,在发动时会在状态栏和通知栏都显示信息,用户可以点击信息进行某些功能的管理

功能实现

声明服务与获取权限

声明权限

 <!-- 通知权限 -->
<!--    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>-->
    <!-- 前台权限权限 -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

声明service

<service android:name=".AppService"/>

在最新版本中notification已被启用,改为使用NotificationCompat
案例如下:

Notification 初始化

		registerNotificationChannel();
 		notifyId = 0x975;//(int) System.currentTimeMillis();

        Intent nfIntent = new Intent(this, HostActivity.class);
        mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
        mBuilder.setContentIntent(PendingIntent.
                getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
                        R.mipmap.ic_launcher)) // 设置下拉列表中的图标(大图标)
                .setContentTitle("下拉列表中的Title") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setContentText("要显示的内容") // 设置上下文内容
                .setWhen(System.currentTimeMillis()) // 设置该通知发生的时间
                //.setPriority(Notification.PRIORITY_MIN) //设定为最低优先级
                .setPriority(Notification.PRIORITY_HIGH) //设定为最高优先级
                .setOngoing(true)//设定为点击后不消失
        ;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
   
            mBuilder.setContentTitle(getResources().getString(R.string.app_name));
        }
        //notificationManager.notify(notifyId, mBuilder.build());
        startForeground(notifyId, mBuilder.build());
  1. 其中notifyId是通知的唯一标识当通知的notify一致时,再发布通知则会覆盖原有的通知内容。这个方法也常用于实时更新通知内容
  2. 前台服务发布通知的方法为startForeground,使用方法和notificationManager.notify类似,不需要另外再注册Manager。

NotificationChannel 通知渠道

而在高版本的安卓中,还需要注册通知渠道:

 	/**
     * 注册通知通道
     */
    private void registerNotificationChannel() {
   
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
   
            notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = notificationManager.getNotificationChannel(CHANNEL_ID);
            if (notificationChannel == null) {
   
                NotificationChannel channel = new 
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Androidforeground_service是一种可以在前台运行的服务。相比于普通的服务foreground_service更加重要,更容易获取系统资源,并且可以显示一个状态栏通知,告知用户此服务正在运行。 要创建一个foreground_service,首先需要创建一个继承自Service类的服务类,并在manifest文件中进行注册。在服务类中,需要重写onCreate()方法与onStartCommand()方法,并在onStartCommand()方法中返回START_STICKY或START_REDELIVER_INTENT来确保服务在被系统杀掉后能够重新启动。 然后,在服务类的onStartCommand()方法中,需要使用startForeground()方法启动foreground_service,并传入一个通知的ID和Notification对象。通知对象可以使用NotificationCompat.Builder来构建,可以设置标题、图标、内容等信息。 在创建foreground_service通知时,通常还需要为通知添加一个 PendingIntent,用于处理用户点击通知时的操作,比如打开一个Activity或启动一个Service通知还可以设置为使用默认的铃声与震动等效果。 当foreground_service运行时,通知会显示在状态栏中,用户可以通过下拉状态栏查看通知的具体内容。foreground_service会一直运行,直到调用stopForeground()方法,或者通过stopService()方法停止服务foreground_service可以在后台执行各种任务,比如播放音乐、下载文件等。由于它在前台运行,并且有一个显示的通知,所以用户可以随时知道服务正在运行。这对于需要长时间运行的服务或有前台交互的服务非常有用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值