它扼杀了后台服务,要解决您的问题,应该使用前台服务。
我的后台粘性服务是在奥利奥和加高设备上杀死任何在后台服务中获取位置的解决方案,当活动处于备份状态时。
发布于 2020-01-20 14:47:00
这是因为Android Oreo的行为改变了后台执行。建议的替代方案如下
1)前台服务.
如果在尝试使用location检索位置时可以向用户显示通知,请使用此方法。这将是可靠的,并在文件中提出。
文档中的示例应用程序:LocationUpdatesForegroundService项目在GitHub上 一个应用程序的例子,它允许应用程序继续用户发起的操作,而不请求所有时间访问背景位置。
References
https://developer.android.com/training/location/receive-location-updates2)工作经理
这种方法不太可靠,因为您无法控制何时会调用此方法,但如果您根本不想向用户显示通知,则可以使用该方法。
发布于 2020-01-20 13:58:06
您将无法运行后台服务在奥利奥长期运行,因为有行为改变,现在奥利奥优化系统内存,电池等,它杀死后台服务,以解决您的问题,您应该使用前台服务。
查看后台执行限制https://developer.android.com/about/versions/oreo/android-8.0-changes
我的一个建议是,如果你可以使用FCM,那就去做吧,因为像WeChat、Facebook这样的应用程序使用它来传递通知,它们不面临任何问题。
替代解决方案,我选择了没有FCM,因为客户要求运行服务,更新位置在后台。以下是以下步骤:
希望你能做更多的研发工作。我分享了我的经验和在后台运行服务的过程。
发布于 2020-01-20 13:14:49
必须显示ForegroundService的通知
public class ForegroundService extends Service {
public static final String CHANNEL_ID = "ForegroundServiceChannel";
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String input = intent.getStringExtra("inputExtra");
createNotificationChannel();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Foreground Service")
.setContentText(input)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
//do heavy work on a background thread
//stopSelf();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}并添加许可
<uses-permissionandroid:name=”android.permission.FOREGROUND_SERVICE” />https://stackoverflow.com/questions/59823932
复制相似问题