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

创建多个PendingContent

  •  1
  • bibek  · 技术社区  · 8 年前

    myRange的值始终为1、2或3。

    如果我没有在If语句中检查我的范围值,它不会给出错误,但不会创建pendingIntent2和pendingIntent3。

      private PendingIntent createGeofencePendingIntent(int myRange) {
        Log.d(TAG, "createGeofencePendingIntent");
        Toast.makeText(getContext(),"creating intent function" + myRange ,Toast.LENGTH_SHORT).show();
    
        if ( geoFencePendingIntent1 != null && myRange == 1)
            return geoFencePendingIntent1;
        if ( geoFencePendingIntent2 != null  && myRange == 2 )
            return geoFencePendingIntent2;
        if ( geoFencePendingIntent3 != null  && myRange == 3)
            return geoFencePendingIntent3;
    
        if(myRange == 1)
          {
             Toast.makeText(getContext(),"creating intent 1",Toast.LENGTH_SHORT).show();
              Intent intent1 = new Intent( getContext(), GeofenceTransitionService.class);
              intent1.putExtra("region",inString[0]);
               geoFencePendingIntent1 = PendingIntent.getService(
                      getContext(), GEOFENCE_REQ_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT );
                return geoFencePendingIntent1;
          }
        else if (myRange ==2)
            {
                Toast.makeText(getContext(),"creating intent 2",Toast.LENGTH_SHORT).show();
                Intent intent2 = new Intent( getContext(), GeofenceTransitionService.class);
                intent2.putExtra("region",inString[1]);
                geoFencePendingIntent2 =  PendingIntent.getService(
                        getContext(), 5, intent2, PendingIntent.FLAG_NO_CREATE );
                return geoFencePendingIntent2;
            }
        else if (myRange == 3)
            {
                Intent intent3 = new Intent( getContext(), GeofenceTransitionService.class);
                return PendingIntent.getService(
                        getContext(), GEOFENCE_REQ_CODE, intent3, PendingIntent.FLAG_UPDATE_CURRENT );
    
            }
    
    
    
    
        geoRange++;
       // Toast.makeText(getContext(), "leaving my geofence", Toast.LENGTH_SHORT).show();
        return null;
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   David Wasser    8 年前

    你这里有几个问题。第一个是:

           geoFencePendingIntent2 =  PendingIntent.getService(
                    getContext(), 5, intent2, PendingIntent.FLAG_NO_CREATE );
    

    这可能总是会回来 null FLAG_NO_CREATE 。这将只返回非- 如果匹配,则返回结果 PendingIntent 已经存在(可能不存在)。使用 FLAG_UPDATE_CURRENT

    第二个问题是,你需要确保你的3个不同 处理意图 s是唯一的。为此,您需要提供一个 requestCode PendingIntent.getService() 或者您需要在 Intent 你传给的 PendingEvent.getService() ,你会得到同样的结果

    关于Stackoverflow,有大约一百万个问题,其中大多数都详细解释了如何解决这个问题 处理意图