肿瘤康复网,内容丰富有趣,生活中的好帮手!
肿瘤康复网 > android o适配foregroundService和notification

android o适配foregroundService和notification

时间:2023-12-31 06:45:17

相关推荐

public final class NotificationUtil {private static final String CHANNEL_ONE_ID = "myapp_name"; //唯一性private static final int NOTIFY_ID = 0x111;private static final int FOREGROUND_ID = 0x112;/*** 公开使用*/@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)public static void sendNotification(Context context, String channelName, String channelDesc, String contentTitle, String contentText) {NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {manager.notify(NOTIFY_ID, getNotificationO(context, manager, channelName, channelDesc, contentTitle, contentText));} else {manager.notify(NOTIFY_ID, getNotification(context, contentTitle, contentText));}}/*** onStartCommand调用*/@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)public static void startForeground(Service service, String channelName, String channelDesc, String contentTitle, String contentText) {NotificationManager manager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);Notification notification;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {notification = getNotificationO(service, manager, channelName, channelDesc, contentTitle, contentText);} else {notification = getNotification(service, contentTitle, contentText);}notification.flags = Notification.FLAG_ONGOING_EVENT;notification.flags |= Notification.FLAG_NO_CLEAR;notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;service.startForeground(FOREGROUND_ID, notification);}/*** onDestory之前调用*/public static void stopForegound(Service service) {service.stopForeground(true);}@RequiresApi(api = Build.VERSION_CODES.O)private static Notification getNotificationO(Context context, NotificationManager manager, String name, String desc, String contentTitle, String contentText) {Notification.Builder builder;NotificationChannel channel = new NotificationChannel(CHANNEL_ONE_ID, name,NotificationManager.IMPORTANCE_DEFAULT);channel.setDescription(desc);//channel.enableLights(true);//channel.setLightColor(color);//Uri mUri = Settings.System.DEFAULT_NOTIFICATION_URI;//channel.setSound(mUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);// Register the channel with system; you can't change the importance// or other notification behaviors after thismanager.createNotificationChannel(channel);builder = new Notification.Builder(context, CHANNEL_ONE_ID);builder.setCategory(Notification.CATEGORY_RECOMMENDATION).setContentTitle(contentTitle).setContentText(contentText)//.setContentIntent(getPendingIntent(context)).setSmallIcon(android.R.drawable.ic_notification_overlay); //todoreturn builder.build();}@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)private static Notification getNotification(Context context, String contentTitle, String contentText) {Notification.Builder builder = new Notification.Builder(context).setPriority(Notification.PRIORITY_DEFAULT)//.setLights(color, 1000, 0)//.setSound(null, null);;builder.setCategory(Notification.CATEGORY_RECOMMENDATION).setContentTitle(contentTitle).setContentText(contentText)//.setContentIntent(getPendingIntent(context)).setSmallIcon(android.R.drawable.ic_notification_overlay); //todoreturn builder.build();}// private static PendingIntent getPendingIntent(Context context) {// Intent intent = new Intent(context, MainActivity.class);// intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);// PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);// return pi;// }}

在activity中直接调用startService还是能跑的。只有当activity被认定为后台进程了。但是由于不确定性,所以只能使用startForegroundService来处理。

引用自:/p/f2db0f58d47f

startService抛异常不是看调用的APP处于何种状态,而是看Servic所在APP处于何种状态,因为看的是UID的状态,所以这里重要的是APP而不仅仅是进程状态

不要通过Handler延迟太久再startService,否则可能会有问题

应用进入后台,60s之后就会变成idle状态,无法start其中的Service,但是可以通过startForegroundService来启动

Application里面不要startService,否则恢复的时候可能有问题

startForegoundService() 要及时配合startForeground(),否则会有各种异常。

如果觉得《android o适配foregroundService和notification》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。