android - 应用程序终止后,谷歌附近的 API 后台扫描不起作用

标签 android google-play-services beacon google-nearby

我有 kontakt.io 信标,我尝试使用附近的 API 编写后台扫描应用程序。

我用这个方法订阅消息:

SubscribeOptions options = new SubscribeOptions.Builder()
            // Finds messages attached to BLE beacons. See
            // https://developers.google.com/beacons/
            .setStrategy(Strategy.BLE_ONLY)
            .build();

    Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "subscribed successfully");
                        mSubState = SubState.SUBSCRIBING;
                        // Start background service for handling the notification.
                        getActivity().startService(getBackgroundSubscribeServiceIntent());
                    } else {
                        Log.i(TAG, "could not subscribe");
                        handleUnsuccessfulNearbyResult(status);
                    }
                }
            });

我的代码是根据这个示例: https://github.com/googlesamples/android-nearby/tree/master/messages/NearbyBackgroundBeacons

我正确地接收消息,但是当我终止应用程序时,不再有消息出现。

有没有办法在杀死应用程序后从附近获取消息?

最佳答案

还没有真正尝试过使用一个,但你是否考虑/检查过使用 Service ?根据描述:

A service can essentially take two forms:

Started

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

Bound

A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

您可以创建一个持续运行扫描的服务,直到您选择停止/销毁它为止。文档本身已经包含关于创建启动服务 的步骤。请仔细阅读。

关于android - 应用程序终止后,谷歌附近的 API 后台扫描不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36481333/

相关文章:

android - 未解析的引用,无法访问类

android - 在 Android Studio 上添加 Google Play 服务依赖项的最佳方式是什么?

android - 使用 Google Play 服务获取最后已知位置

java - 使用 android-beacon-library 检测蓝牙设备

android - 无法运行我的信标应用程序,因为 kontakt sdk 错误

使用 CLLocationManager 的 Swift 3 信标监控

android - 同步Gradle失败

javascript - 框架7 : How to refresh a page after changing template data?

android - 有没有一种方法可以捕获所有错误而又不会在try语句中乱扔我的代码?

java - [Android map API] : Google-play-services prompting update on phone but not in emulator.