代码之家  ›  专栏  ›  技术社区  ›  Pentium10

为什么pendingent不发回我的定制附加设置?

  •  32
  • Pentium10  · 技术社区  · 14 年前

    这个问题在某种程度上与我想问的问题有关 get the extras back in startActivityForResult

    我已经订阅了receiveproximityalerts,并且我已经明确地构建了包含一些额外内容的意图。但当我得到服务的时候,临时演员不在了。

    答案之后是工作代码:

    Intent intent = new Intent(this, PlacesProximityHandlerService.class);
    intent.setAction("PlacesProximityHandlerService");
    intent.putExtra("lat", objPlace.getLat());
    intent.putExtra("lon", objPlace.getLon());
    intent.putExtra("error_m", objPlace.getError()+ALERT_RANGE_IN_METERS);
    PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);
    LocationUtils.addProximity(this, objPlace.getLat(), objPlace.getLon(),objPlace.getError()+ALERT_RANGE_IN_METERS, -1, sender);
    

    文件上说param PendingIntent to be sent for each location update

    5 回复  |  直到 7 年前
        1
  •  43
  •   ognian    14 年前

    由于某些未指明的原因,只有在设置了某些操作(例如setAction(“foo”)时,才会传递额外的内容。Commonware所指的仅适用于获取挂起的实例时,如果您没有设置FLAG\u ONE\u SHOT。这可以通过pendingent.get中的requestCode参数来修复。。。工厂方法。尽管文档中说它目前还没有被使用,但在区分悬而未决的内容时,它实际上是考虑到了这一点的。

        2
  •  25
  •   CommonsWare    14 年前

    如果你有多个 PendingIntents Intents 不同的不仅仅是他们额外的。否则,Android将继续重用第一个 PendingIntent 你为你的第一个 Intent ,先用这个 总是有临时演员。

    例如,您可以通过 setAction() 意图 路由(因为您指定的是组件),但它会使 与众不同。

        3
  •  8
  •   Jonathan Cameron    11 年前

    我遇到了这个问题,我找到的解决方法非常简单,尽管我无法解释为什么它会起作用。

    最初我的意图是这样的:

         notificationIntent = new Intent(ctx, FragmentTabsPager.class);
         notificationIntent.setData(Uri.parse("content://com.sbs.mobile.workorder.WorkOrder/notes/"));
         notificationIntent.putExtra("NOTIFICATION", true);   
         notificationIntent.putExtra(WorkOrder.WorkOrderColumns.WORKORDERID, submessage);
    

    当创建这样的意图时,单击通知时不会传递额外的内容,临时内容映射在接收活动中为空。我对初始化notificationIntent的行做了以下更改:

         notificationIntent = new Intent().setClass(ctx, FragmentTabsPager.class);
    

    现在临时任务被填充到接收活动中。再说一次,我无法解释为什么这样做,但它解决了我的问题。

        4
  •  2
  •   Vahid Amiri    7 年前

    没有一个答案对我有用。将action设置为特定的字符串第一次起作用,但是如果以后将同一个通知与不同的附加项一起使用,则不会起作用。我把绳子换了 setAction

    intent.setAction(new Random().nextInt(50) + "_action");
    
    • 如果您认为您可能会经常使用通知(比如下载不同的文件),那么将较大的数字传递给 nextInt()
        5
  •  1
  •   inor    13 年前

    关键是把额外的和独特的行动融入到意图中 打电话

    PendingIntent sender=PendingIntent.getService(this, 0, intent, 0);
    

    如果你把额外的任务和行动安排在意图中 打上面的电话,是行不通的。 这将

    Intent intent;  
    PendingIntent sender=PendingIntent.getService(this, 0,   
       intent=new Intent(this, PlacesProximityHandlerService.class), 0);  
    
    intent.setAction("PlacesProximityHandlerService");  
    intent.putExtra("lat", objPlace.getLat());  
    
    推荐文章