【问题标题】:Duplicate data when using PendingIntent使用 PendingIntent 时重复数据
【发布时间】:2019-11-07 12:17:05
【问题描述】:

我正在使用pending intent 收听DetectedActivityFusedLocationGoogleFit 等更新。我过去了

PendingIntent.FLAG_UPDATE_CURRENT when calling: pendingIntent = PendingIntent.getService(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT); 

但是,我仍然得到重复的数据。

我尝试在每次收听某些更新时使用不同的请求 ID(用于收听 FusedLocation 更新的不同请求 ID,用于收听 GoogleFit 更新的不同请求 ID 等),但它仍然给我重复的数据。我还尝试将不同的 actionIds 添加到 intentService

在我的服务中,我有以下代码:

intentService = new Intent(this, SensorsIntentService.class); //in onCreate
pendingIntent = PendingIntent.getService(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT);

我希望每次只有一组数据并且没有重复。 任何帮助,将不胜感激。

【问题讨论】:

    标签: android android-service android-pendingintent google-fit android-fusedlocation


    【解决方案1】:

    所以,回答我自己的问题,显然我的问题不是关于挂起的意图标志,而是关于onStartCommand 在我的service 里面。显然它被调用了两次(第二次是因为我通过调用将service 移动到foreground

    startForeground(id, notification)
    

    (如果我对原因有误,请纠正我)所以听众再次注册,所以数据被发送了两次。我现在不是在onStartCommand 中启动它,而是在onBind 中启动foregroundService

    我唯一没有弄清楚的是:我必须创建一个 BroadcastReceiver 而不是我的 IntentService 来处理 pendingIntent 和我现在使用的 Service 实现

    pendingIntent = PendingIntent.getBroadcast(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT); 
    

    而不是

    pendingIntent = PendingIntent.getService(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT); 
    

    否则pendingIntent 永远不会被捕获。 有人知道为什么吗?

    【讨论】:

      猜你喜欢
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      相关资源
      最近更新 更多