启动服务时,我会显示一条通知。 在Android 9以下是不错的选择。 但是在Android 9中,当我单击“主页”按钮时,该应用程序关闭后,通知将被取消。 这是我的代码
@Override
public int onStartCommand(Intent intent, int flags, int startId) {//____________________________ Start onStartCommand
SaveLog("onStart Service : " + getStringCurrentDate());
if (alarmNotify == null) {
Integer id = getApplicationContext().getResources().getInteger(R.integer.NotificationRun);
NotificationManagerClass managerClass =
new NotificationManagerClass(
getApplicationContext(),
getApplicationContext().getResources().getString(R.string.ServiceRun)
, true
, 3
);
alarmNotify = managerClass.getNotification();
startForeground(id, alarmNotify);
}
Calendar now = Calendar.getInstance();
Intent intent1 = new Intent(getApplicationContext(), ReceiverJobInBackground.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), 60 * 1000, pendingIntent);
return Service.START_STICKY;
}//_____________________________________________________________________________________________ End onStartCommand
alarmNotify是android.app.Notification
我设置了权限:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
这是我的频道:
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, notifManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(context.getResources().getColor(R.color.colorPrimary));
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(notificationChannel);
请帮助我。 坦克