Service设置为前台服务

通常Service都是运行在后台,后台运行的Service系统优先级较低,在系统内存不足的时候,后台运行的Service可能会被回收。当需求Service一直保持运行且在内存不足的情况下不会被回收,可以将Service设置为前台服务。

创建一个前台服务
public class MyService extends Service{
 private static final String TAG = MyService.class.getSimpleName();
 @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");
        setForeground();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        super.onBind(intent);
        return null;
    }
    /**
     * 将Service设置为前台
     */
    private void setForeground() {
        Log.i(TAG, "setForeground: ");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("Foreground_Service",
                    "Foreground_Service", NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager == null) {
                return;
            }
            manager.createNotificationChannel(channel);
            Notification notification =
                    new NotificationCompat.Builder(this, "Foreground_Service")
                            .setAutoCancel(true)
                            .setCategory(Notification.CATEGORY_SERVICE)
                            .setOngoing(true)
                            .setPriority(NotificationManager.IMPORTANCE_LOW)
                            .build();
            startForeground(101, notification);
        }
    }
}

Android 9以后上述的方法过了几秒后,Service会自动destory,试了试下面的方法

 private String notificationId = "serviceid";
 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 //创建NotificationChannel
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      String notificationName = "servicename";
      NotificationChannel channel = new NotificationChannel(notificationId, notificationName,                     NotificationManager.IMPORTANCE_MIN);
      notificationManager.createNotificationChannel(channel);
    }
 startForeground(1, getNotification());
 private Notification getNotification() {
        Log.i(TAG, "getNotification: ");
        Notification.Builder builder = new Notification.Builder(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(notificationId);
        }
        return builder.build();
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值