In the project, you need to start a service to play music. It happens to be a suspended. A few seconds of play disappeared for a few seconds. Android 8.0 itself limited it, and it seemed to go to Google. After Google, Google has limited the background of Android 8.0. It is mainly considered in performance. There will be problems with the suspension box and service startup. It is just that the project needs to be adapted. Specific restrictionsGOOGLE background limit
From the API of Android, at 26, Type_toast was replaced by Type_application_overlay, so you need to make the following adaptation in Android 8.0.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mAudioParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
mAudioParams.type = WindowManager.LayoutParams.TYPE_TOAST;
}
When starting the service, you need to use StartForegroundService. Google does not allow the service to start service in the background, unless you let the user know.
if (Build.VERSION.SDK_INT >= 26) {
mContext.startForegroundService(audioIntent);
} else {
mContext.startService(audioIntent);
}
After the system creation service, it applies for five seconds to call the service’s StartForeground () method to show the notification of users who show the new service. Called in the OnStartCommand in the Service class.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification.Builder builder = new Notification.Builder(this, "555").setContentTitle(getString(R.string.app_name)).setContentText("SmartTracker Running").setAutoCancel(true);
Notification notification = builder.build();
startForeground(1, notification);
}
above code can solve the above two problems, and one point is that the authority of the suspension box is remembered, otherwise the effect will not be visible.
Invite you to join, we grow up free of charge, just one second to change