安卓:OTG存储通知与无线电c冲突



我正在开发一项正在进行的服务,该服务监听与收音机的USB接口连接。当发现这样的连接时,该服务更新其通知以反映所述连接;当连接丢失时(例如通过拔下事件或关闭收音机),此服务将更新其通知以反映所述断开连接。该服务在检测到更改时还会写入数据库,因此其他应用程序可以利用该信息。

此服务在未配置为OTG存储的USB端口上正常工作。然而,当使用启用OTG存储的USB端口时,我会遇到一些问题,即使数据库被正确写入,服务通知也没有正确更新。我相信这是因为我连接的无线电也可以作为OTG存储设备,因此一旦与所述无线电连接,OTG存储通知就会发生,我可能会丢失通知上下文。此外,如果在OTG存储器能够正确装载之前断开与无线电的连接,则服务通知和数据库将正确更新。

例如,如果我当前连接到一个收音机,并且OTG存储已安装,如果我断开所述收音机的连接,OTG存储将卸载,但我的服务通知不会反映该更改,即使我可以在数据库中看到更改正确发生。

我在安卓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时仍然有问题。生成器:

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());

所以我有点困惑可能是什么问题。

如果你有任何想法或建议,或者如果有任何代码我应该包括在内,以提供更多帮助,请告诉我。

更新:该问题不是实际通知的问题,而是OTG存储和侦听同一拔出事件的服务之间的竞争条件。为了解决这个问题,我让我的服务侦听OTG存储状态的更改。这似乎解决了这个问题,尽管无可否认,这更像是一种变通方法。

最新更新