startForegroundService
在Android 8.0里 ,应用在后台的时候调用了Context.startService
此时会触发:
java.lang.IllegalStateException: Not allowed to start service Intent
我们需要改成:
Context.startForegroundService()
并且在Context.startForegroundService()
之后必须要调用Service.startForeground
,也就是说在foreground 的启动接口调用后的 5 秒内必须要在service 的onStart()
中调用startForeground()
接口来解除timeout。
startFroeground
public final void startForeground(int id, Notification notification) {
try {
mActivityManager.setServiceForeground(
new ComponentName(this, mClassName), mToken, id,
notification, 0);
} catch (RemoteException ex) {
}
}
要求startFroeground() 中的id 不能为0,而且notification不能为null。
启动service代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, PowerService.class));
} else {
context.startService(new Intent(context, PowerService.class));
}
在service里的onStart()方法:
@Override
public void onCreate() {
Log.i(TAG,"onCreate()");
super.onCreate();
startTo();
}
private void startTo() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel Channel = null;
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Channel = new NotificationChannel(CHANNEL_ID, "Main Service", NotificationManager.IMPORTANCE_LOW);
Channel.enableLights(true);
Channel.setLightColor(Color.RED);
Channel.setShowBadge(true);
Channel.setDescription("PowerStateService");
Channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); //设置锁屏可见 VISIBILITY_PUBLIC=可见
manager.createNotificationChannel(Channel);
notification = new Notification.Builder(this)
.setChannelId(CHANNEL_ID)
.setContentTitle("Main Service")//标题
.setContentText("Running...")//内容
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)//小图标一定需要设置,否则会报错(如果不设置它启动服务前台化不会报错,但是你会发现这个通知不会启动),如果是普通通知,不设置必然报错
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
startForeground(1, notification);//服务前台化只能使用startForeground()方法,不能使用 notificationManager.notify(1,notification); 这个只是启动通知使用的,使用这个方法你只需要等待几秒就会发现报错了
}
}