Android Foreground Service

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

示例代码:

需要在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);
                //是否在久按桌面图标时显示此渠道的通知
                //channel.setShowBadge(true);
                mNotificationManager.createNotificationChannel(channel);
            }
        }
    }
}

以上

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值