应用未运行时的Google Awareness Api事件 [英] Google Awareness Api events while app isn't running

查看:125
本文介绍了应用未运行时的Google Awareness Api事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在Google Awareness API围栏"启动时启动BroadcastReceiver或IntentService(取决于我的最终处理需要多长时间).例如,也许我想知道一天中我激活了一组信标围栏的次数(假设我随身携带手机).我发现的所有示例都显示了在代码中注册广播接收器,但是我的理解是,如果我的应用未运行,我将需要在清单中注册广播接收器,以便OS可以向其发送广播.此外,意图ID似乎是自定义的ID,所以我想我至少必须通过代码在操作系统中注册一次.

I would like either a BroadcastReceiver or IntentService (depending on how long my eventual processing takes) to start when a Google Awareness API "fence" fires. For example, perhaps I want to know how many times I activate a set of beacon fences over the course of the day (assuming I keep my phone with me). All the examples I've found show registering broadcast receivers in code, but my understanding is that I would need to register a broadcast receiver in the manifest in order for the OS to send the broadcast to it if my app isn't running. What's more, the intent ID appears to be a custom one, so I would guess I'd have to register it with the OS at least once via code?

我猜想我将不得不创建一个或多个测试应用程序以通过反复试验来解决这个问题,但一定会收到尝试过此方法并希望与您分享结果的任何人的来信!

I'm guessing I'm going to have to create one or more test apps to figure this out by trial and error, but would sure appreciate hearing from anyone who has tried this and would like to share your results!

推荐答案

只要在清单文件中指定BroadCastReceiver,就足够了.

It is just enough if you specify BroadCastReceiver in your Manifest file.

即使声明了清单<receiver>条目,也不必必须在代码中注册它.只要考虑一下平台如何处理活动,就可以只在清单文件中注册它(如果没有,我们就获得ActivityNotFoundException),就像广播也可以只在清单文件中注册一样.

Its not a must that you need to register it in the code even after declaring the Manifest <receiver> entry. Just think about how the platform is able to handle Activities you register it only in the Manifest file(if not we get ActivityNotFoundException) the same way Broadcasts can also be register only in the Manifest file.

您需要像这样声明接收方:

You need to declare the receiver like:

<receiver android:name=".MyFenceReceiver" >
   <intent-filter>
     <action android:name="android.intent.action.FENCE_RECEIVER_ACTION" />
    </intent-filter>
</receiver>

扩展BroadcastReceiver类.

Extend the BroadcastReceiver class.

public class MyFenceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    FenceState fenceState = FenceState.extract(intent);

    if (TextUtils.equals(fenceState.getFenceKey(), "geofence")) {
        switch(fenceState.getCurrentState()) {
            case FenceState.TRUE:

                break;
            case FenceState.FALSE:

                break;
            case FenceState.UNKNOWN:

                break;
        }
    }
}
}

https://developer.android.com/guide中的更多信息/topics/manifest/receiver-element.html

这篇关于应用未运行时的Google Awareness Api事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆