前台服务应该使用startForeground方法来提高优先级并使其更难被系统杀死。
示例代码:
//创建Notification对象 Notification notification = new Notification.Builder(this) .setContentTitle("Foreground Service") .setContentText("Running") .setSmallIcon(R.drawable.icon) .build();
//将服务设置为前台服务 startForeground(1, notification); //1为通知ID