android前台服务

关于作者:CSDN内容合伙人、技术专家, 从零开始做日活千万级APP。

专注于分享各领域原创系列文章 ,擅长java后端、移动开发、商业变现、人工智能等,希望大家多多支持。

未经允许不得转载

目录

  • 一、导读
  • 二、使用
    • [2.1 添加权限](#2.1 添加权限)
    • [2.2 新建一个服务](#2.2 新建一个服务)
    • [2.3 构建通知消息](#2.3 构建通知消息)
    • [2.4 启动与停止服务](#2.4 启动与停止服务)
  • [三、 推荐阅读](#三、 推荐阅读)

一、导读

我们继续总结学习基础知识,温故知新。

今天记录下android前台服务(Foreground Service),我们知道服务可以分成好几类,

前台服务(Foreground Service)是一种在 Android 应用程序中执行长时间运行任务的服务类型。

与普通的后台服务不同,前台服务在系统通知栏中显示一个可见的通知。

二、使用

前台服务通常用于执行用户可感知的任务,例如播放音乐、下载文件、进行定位更新等。

通过将服务置于前台状态并显示通知,前台服务可以避免被系统误认为是低优先级的后台任务,从而更好地确保服务的稳定运行。

但是,需要注意的是,使用前台服务可能会增加应用程序的电池消耗,因为持续运行任务需要持续的系统资源。

我们就一起来看看如何使用一个前台服务:

2.1 添加权限

xml 复制代码
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

不添加的话就会报错啦

java.lang.SecurityException: Permission Denial:

startForeground from pid=2, uid=1 requires android.permission.FOREGROUND_SERVICE.

2.2 新建一个服务

java 复制代码
public class DataService extends Service {

    ServiceBinder binder;


    public DataService() {
    }

    public void onCreate() {
    }

    @Override
    public IBinder onBind(Intent intent) {

        if(binder == null) {
            binder = new ServiceBinder(this);
        }

        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler = new InnerHandler(this);
        initPos();
        return super.onStartCommand(intent, flags, startId);
    }
    
}

2.3 构建通知消息

构建通知消息,一般在服务启动时就构建一个通知栏出来,在onCreate 、onStartCommand都行

java 复制代码
    public int onStartCommand(Intent intent, int flags, int startId) {
      Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
      Intent nfIntent = new Intent(this, MainActivity.class);
      
      builder.setContentIntent(PendingIntent.
        getActivity(this, 0, nfIntent, 0)) // 设置PendingIntent
        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
          R.mipmap.ic_large)) // 设置下拉列表中的图标(大图标)
        .setContentTitle("下拉列表中的Title") // 设置下拉列表里的标题
        .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
        .setContentText("要显示的内容") // 设置上下文内容
        .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间
      
      Notification notification = builder.build(); // 获取构建好的Notification
      notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
            startForeground(NOTIFICATION_ID, notification);
     
      return super.onStartCommand(intent, flags, startId);
    }

Notification创建完后,就调用 startForeground(NOTIFICATION_ID, notification);让Android服务运行在前台。

2.4 启动与停止服务

startForeground(NOTIFICATION_ID, notification);让Android服务运行在前台。

stopForeground(true);// 停止前台服务--参数:表示是否移除之前的通知

三、 推荐阅读

Java 专栏

SQL 专栏

数据结构与算法

Android学习专栏

未经允许不得转载

相关推荐
我是阿亮啊4 分钟前
Android 自定义 View 完全指南
android·自定义·自定义view·viewgroup
2601_949833392 小时前
flutter_for_openharmony口腔护理app实战+意见反馈实现
android·javascript·flutter
峥嵘life2 小时前
Android 16 EDLA测试STS模块
android·大数据·linux·学习
TheNextByte12 小时前
如何打印Android手机联系人?
android·智能手机
泡泡以安3 小时前
Android 逆向实战:从零突破某电商 App 登录接口全参数加密
android·爬虫·安卓逆向
2501_944525544 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 预算详情页面
android·开发语言·前端·javascript·flutter·ecmascript
清蒸鳜鱼5 小时前
【Mobile Agent——Droidrun】MacOS+Android配置、使用指南
android·macos·mobileagent
2501_915918415 小时前
HTTPS 代理失效,启用双向认证(mTLS)的 iOS 应用网络怎么抓包调试
android·网络·ios·小程序·https·uni-app·iphone
峥嵘life6 小时前
Android EDLA CTS、GTS等各项测试命令汇总
android·学习·elasticsearch
Cobboo6 小时前
i单词上架鸿蒙应用市场之路:一次从 Android 到 HarmonyOS 的完整实战
android·华为·harmonyos