我正在开发一项正在进行的服务,该服务监听与收音机的 USB 接口连接。当找到这样的连接时,此服务会更新其通知以反映所述连接;当连接丢失时(例如通过拔出事件或关闭无线电),此服务将更新其通知以反映所述断开连接。该服务还会在检测到更改时写入数据库,以便其他应用程序可以利用该信息。
此服务可在未配置用于 OTG 存储的 USB 端口上正常运行。但是,当使用启用了 OTG 存储的 USB 端口时,我会遇到一些问题,即服务通知无法正确更新,即使数据库被正确写入也是如此。我相信这是因为我要连接的收音机也可用作 OTG 存储设备,因此一旦与所述收音机建立连接,就会发生 OTG 存储通知,我可能会丢失我的通知上下文。此外,如果在 OTG 存储能够正确安装之前断开无线电,则服务通知和数据库会正确更新。
例如,如果我当前连接到无线电并且 OTG 存储已安装,如果我断开所述无线电,OTG 存储将卸载但我的服务通知不会反映该更改,即使我可以在数据库中看到发生了更改适当地。
我在 Android 4.0.4 上运行它,所以我认为这个问题仅仅是因为我的代码已被弃用:
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
Notification notification = new Notification(icon, contentTitle, time);
notification.setLatestEventInfo(this, contentTitle, contentText,
contentIntent);
notification.flags = Notification.FLAG_FOREGROUND_SERVICE
| Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(NOTIFICATION_ID, notification);
但是在更新到 Notification.Builder 时我仍然遇到问题:
Notification.Builder notiBuilder =
new Notification.Builder(this);
notiBuilder.setSmallIcon(icon);
notiBuilder.setContentTitle(contentTitle);
notiBuilder.setContentText(contentText);
notiBuilder.setContentIntent(contentIntent);
notiBuilder.setPriority(2);
notificationManager.notify(NOTIFICATION_ID, notiBuilder.getNotification());
所以我对这个问题可能是什么感到有点困惑。
如果您有任何想法或建议,或者我应该包含任何代码以提供更多帮助,请告诉我。