在Android 9中,Service.startForeground()方法的行为发生了变化。在Android 9及以上的版本中,如果应用程序在后台运行,并且没有运行可见的Activity时,调用startForeground()方法将会引发一个异常。
为了解决这个问题,可以在调用startForeground()方法之前,先判断应用程序的运行状态,如果应用程序在后台运行,并且没有可见的Activity时,可以先创建一个Notification并使用startForeground()方法将Service放入前台。
下面是一个示例代码:
public class MyService extends Service {
private static final int NOTIFICATION_ID = 1;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 创建NotificationChannel,Android 8及以上版本需要创建通知渠道
NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
// 创建Notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle("Foreground Service")
.setContentText("Service is running...")
.setSmallIcon(R.mipmap.ic_launcher);
// 将Service放入前台
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && !isAppRunningInForeground()) {
// 在Android 10及以上版本,判断应用程序是否在前台运行
startForeground(NOTIFICATION_ID, builder.build());
} else {
startService(new Intent(this, InnerService.class));
startForeground(NOTIFICATION_ID, builder.build());
}
// TODO: 执行后台任务
return START_STICKY;
}
// 判断应用程序是否在前台运行
private boolean isAppRunningInForeground() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List processInfos = activityManager.getRunningAppProcesses();
if (processInfos != null) {
for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.processName.equals(getPackageName()) && processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}
}
return false;
}
public static class InnerService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground(NOTIFICATION_ID, new Notification());
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在这个示例代码中,我们首先判断了应用程序的运行状态,如果应用程序在后台运行并且没有可见的Activity时,我们创建了一个Notification并将Service放入前台。同时,为了避免Android 9中的限制,我们创建了一个InnerService,并在StartCommand()方法中调用了startForeground()方法将其放入前台,并立即停止了InnerService。这样做的目的是为了绕过Android 9中对startForeground()方法的限制。