相关文章推荐
烦恼的核桃  ·  Android图片加载框架最全解析(一),G ...·  1 月前    · 
光明磊落的登山鞋  ·  使用 Intune 在 iOS 和 ...·  2 周前    · 
刀枪不入的茶壶  ·  提取栅格—Portal for ...·  2 月前    · 
自信的热水瓶  ·  FPGA基础设计:Verilog常数赋值、字 ...·  3 月前    · 
阳光的酱肘子  ·  莊敬傑出校友王大陸華人之冠!演藝之光!中國票 ...·  4 月前    · 
眼睛小的香瓜  ·  3dmax - AutoRemove - ...·  4 月前    · 
失眠的开心果  ·  智驾龙头地平线机器人,拟募资超46亿港元_中证网·  4 月前    · 
Code  ›  Android Foreground Service开发者社区
android开发
https://cloud.tencent.com/developer/article/2195328?areaId=106001
霸气的弓箭
1 年前
103style
0 篇文章

Android Foreground Service

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
103style
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > Android开发经验分享 > Android Foreground Service

Android Foreground Service

作者头像
103style
发布 于 2022-12-19 13:29:10
326 0
发布 于 2022-12-19 13:29:10
举报

为了防止后台服务被系统干掉,我们需要将服务提升为前台服务。

示例代码:

需要在 AndroidManifest 添加 前台服务的权限 : <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

FOREGROUND_SERVICE Added in API level 28 Android 9.0 public static final String FOREGROUND_SERVICE Allows a regular application to use Service.startForeground . Protection level: normal Constant Value: android.permission.FOREGROUND_SERVICE

public class SampleService extends Service {
    public static final String CHANNEL_ID = "com.github.103style.SampleService";
    public static final String CHANNEL_NAME = "com.github.103style";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    @Override
    public void onCreate() {
        super.onCreate();
        registerNotificationChannel();
        int notifyId = (int) System.currentTimeMillis();
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
        mBuilder
                //必须要有
                .setSmallIcon(R.mipmap.ic_launcher)
                //.setSound(null)
                //.setVibrate(null)
                //...
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            mBuilder.setContentTitle(getResources().getString(R.string.app_name));
        startForeground(notifyId, mBuilder.build());
     * 注册通知通道
    private void registerNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel notificationChannel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
            if (notificationChannel == null) {
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                //是否在桌面icon右上角展示小红点
                channel.enableLights(true);
                //小红点颜色
                channel.setLightColor(Color.RED);
                //通知显示
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                //是否在久按桌面图标时显示此渠道的通知
 
推荐文章
烦恼的核桃  ·  Android图片加载框架最全解析(一),Glide的基本用法开发者社区
1 月前
光明磊落的登山鞋  ·  使用 Intune 在 iOS 和 Android 上管理 Microsoft Edge | Microsoft Learn
2 周前
刀枪不入的茶壶  ·  提取栅格—Portal for ArcGIS | ArcGIS Enterprise 文档
2 月前
自信的热水瓶  ·  FPGA基础设计:Verilog常数赋值、字符串、标识符 | FPGA 开发圈
3 月前
阳光的酱肘子  ·  莊敬傑出校友王大陸華人之冠!演藝之光!中國票房一哥,好萊塢硬漢也擋不住大陸神奇 - 電影電視科 - 教學單位 - 莊敬高職
4 月前
眼睛小的香瓜  ·  3dmax - AutoRemove - 官网 | 专业Autodesk卸载软件 | 轻松卸载工具
4 月前
失眠的开心果  ·  智驾龙头地平线机器人,拟募资超46亿港元_中证网
4 月前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号