简体   繁体   English

Android:OTG存储通知与广播c冲突

[英]Android: OTG Storage notification conflicts with radio c

I am working on an ongoing service which listens for a USB interface connection to a radio. 我正在开发一项正在进行的服务,该服务侦听与无线电的USB接口连接。 When such a connection is found, this service updates its notification to reflect said connectivity; 找到这样的连接后,此服务将更新其通知以反映所述连接; when a connection is lost (such as through an unplug event, or the radio being turned off), this service will update its notification to reflect said dis-connectivity. 当连接断开时(例如通过拔出事件或无线电被关闭),该服务将更新其通知以反映所述断开连接。 This service also writes to a database when it detects a change, so other applications can utilize that information. 此服务在检测到更改时也会写入数据库,因此其他应用程序可以利用该信息。

This service works properly on USB ports which aren't configured for OTG storage. 此服务可在未配置OTG存储的USB端口上正常运行。 However, when a USB port with OTG storage enabled is used, I run into some issues where the service notification doesn't update properly, even though the database is being correctly written to. 但是,使用启用了OTG存储的USB端口时,即使数据库已正确写入,也会遇到一些服务通知无法正确更新的问题。 I believe this is because the radio I am connecting to also functions as a OTG storage device, and so once a connection with said radio is made, the OTG storage notification occurs, and I may be losing my notification context. 我相信这是因为我要连接的无线电也充当OTG存储设备,因此一旦与所述无线电建立连接,就会发生OTG存储通知,并且我可能会丢失通知上下文。 Further, if disconnect from the radio before the OTG storage is able to properly mount, the service notification and database correctly update. 此外,如果在OTG存储器能够正确装入之前与无线电断开连接,则服务通知和数据库将正确更新。

For example, if I am currently connected to a radio and OTG storage has mounted, if I disconnect said radio, the OTG storage will unmount but my service notification will not reflect that change, even though I can see that in the database the change occured properly. 例如,如果我当前已连接到无线电并且已安装OTG存储,如果我断开了所述无线电,则OTG存储将卸载,但我的服务通知将不会反映该更改,即使我可以看到数据库中发生了更改正常。

I am running this on Android 4.0.4, and so I thought this issue was merely because my code was deprecated: 我在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);

But I still have issues when updating to Notification.Builder: 但是更新到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());

So I'm a little confused about what the issue might be. 所以我对可能是什么问题感到困惑。

Please let me know if you have any ideas or suggestions, or if there's any code I should have included to be more helpful. 如果您有任何想法或建议,或者我应该包含任何代码以帮助您,请告诉我。

Update: Rather than being an issue with the actual notification, the issue turned out to be a race condition between the OTG storage and my service listening for the same unplug event. 更新:问题不是在实际的通知中出现,而是在OTG存储和我的服务在侦听同一拔出事件之间出现竞争状态。 In order to workaround this issue, I made my service listen for the change in the OTG storage state. 为了解决此问题,我让我的服务侦听了OTG存储状态的更改。 This seems to have resolved the issue, although is admittedly more of a workaround than anything. 尽管公认的解决方法比什么都重要,但这似乎已经解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM