Awareness API 全局接收者意图过滤器

Awareness API global receiver intent filter

我正在 android 应用程序中实现 google Awareness API,但是 none 的示例和指南都展示了如何收听 api 应用程序关闭或在后台时的事件。 我根据答案

写了一个全局接收器
    <receiver android:name=".MyFenceReceiver" >
   <intent-filter>
     <action android:name="android.intent.action.FENCE_RECEIVER_ACTION" />
    </intent-filter>
</receiver>

但是,由于它不起作用,我怀疑我不知道拦截 Awarness 事件的正确意图过滤器。 有谁知道正确的 Intent 过滤器,或者如果这不是我的问题,我如何在应用程序关闭或在后台使用全局接收器时拦截 API 事件?

好的,所以最终的答案是您必须在清单中注册此接收器并为其提供您自己的意图过滤器,如下所示:

   Intent intent = new Intent(Constants.Requests.FENCE_RECEIVER_ACTION);
                    mPendingIntent =
                            PendingIntent.getBroadcast(BaseActivity.this, 0, intent, 0);

                    // The broadcast receiver that will receive intents when a fence is triggered.

其中 "FENCE_RECEIVER_ACTION" 是这样的:

// The intent action which will be fired when your fence is triggered.
    public final static String FENCE_RECEIVER_ACTION =
            BuildConfig.APPLICATION_ID + "FENCE_RECEIVER_ACTION";

在清单中,您像这样放入接收器:

 <receiver android:name=".FenceReceiver" >
        <intent-filter>
            <action android:name="'YOUR_PACKGE_NAME_HERE'FENCE_RECEIVER_ACTION" />
        </intent-filter>
    </receiver>

无需在任何地方注销接收器。