android oreo上的永久服务
Android 8的电池消耗改进对用户来说很好,但是如果我的服务能按预期工作,我有点害怕。
首先:感谢您提出任何建议,但我不能只安排我的服务。 我希望一直在后台运行一个类似OK Google
的关键词监听器。 它将基于开源的pocketsphinx-android库。 我知道这会消耗很多电池电量,我会告诉用户这个。
我们可以在android 8+上创建永久后台服务吗? 我需要在gradle中定位android 8,因为我期待一些旧目标的bug。 我也不想使用前台服务来惹恼用户,该服务会在状态栏中永久显示通知。
[ https://developer.android.com/about/versions/oreo/background.html] – 是否真的无法为我的用例(但最好是所有用例)提供永久性后台服务?
不幸的是,无法使用后台服务,也不能在Android 8.0及更高版本上显示前台通知。
唯一可行的方法是将应用程序粘贴到Voice API等Google API上 。
据我所知,没有一个好的工作,大多数应用程序,如WhatsApp仍然针对Android API 24。
值得一提的是,我分享了我的经验:
部分可以使用后台服务,而不是在Android 8.0上显示前景通知,只是进行了一些实验,结果是:
只需使用IMPORTANCE_NONE创建通知通道, 通知仍然存在,但不会显示在状态栏中 。
代码摘录:
NotificationChannel channelService = new NotificationChannel( YOUR_SERVICE_CHANNEL_ID, YOUR_CHANNEL_HUMAN_READABLE_NAME, NotificationManager.IMPORTANCE_NONE); channelService.setSound(null, null); // let's be quiet : no sound channelService.setLockscreenVisibility(Notification.VISIBILITY_SECRET); // not on lock screen // Register the channel with the system; you can't change the importance // or other notification behaviors after this // changes needs to uninstall app or clean app data, or rename channel id! notificationManager.createNotificationChannel(channelService));
这样,当您的应用程序在前台运行时,通知将不会显示。
不完美的解决方案:
当您的应用程序进入后台时(即,您打开另一个应用程序),“Android系统”应用程序会显示有关“(您的) 应用程序在后台运行 ”的通知。 所以,这不是很好,但是,从我的观点来看,它比以前好一些。