应用程序终止后,Google附近的API后台扫描不起作用 [英] Google nearby API background scan doesn´t work after application kill

查看:222
本文介绍了应用程序终止后,Google附近的API后台扫描不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我使用此方法订阅邮件:

  SubscribeOptions options = new SubscribeOptions.Builder()
//查找附加到BLE信标的消息。请参阅
// 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状态状态){
if(status.isSuccess()){
Log.i(TAG,订阅成功);
mSubState = SubState.SUBSCRIBING;
//开始处理通知的后台服务
getActivity()。startService(getBackgroundSubscribeServiceIntent());
} else {
Log.i(TAG,无法订阅);
handleUnsuccessfulNearbyResult(status);
}
}
});

我的代码依据以下示例:
https://github.com/googlesamples/android-nearby/tree/master/messages/NearbyBackgroundBeacons



我正确接收邮件,但是当我杀了应用程序时,没有更多邮件来。

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

解决方案

还没有真正尝试过使用其中一个,但是您是否考虑/使用服务?根据描述:


一项服务基本上可以采用两种形式:

当一个应用程序组件(如一个活动)通过调用startService()来启动它时,服务就会启动。开始

启动后,服务可以无限期地在后台运行,即使启动该服务的组件已被销毁。通常,启动的服务执行单个操作,但不会将结果返回给调用方。例如,它可能通过网络下载或上传文件。当操作完成后,服务应该自行停止。



绑定

当一个应用程序组件通过调用bindService()绑定到一个服务时,服务就会绑定。绑定服务提供了一个客户端 - 服务器接口,允许组件与服务进行交互,发送请求,获取结果,甚至跨进程使用进程间通信(IPC)进行交互。绑定服务只在另一个应用程序组件绑定到它时才运行。多个组件可以一次绑定到服务,但是当它们全部解除绑定时,服务将被销毁。


您可以创建一个 Service ,它会持续运行扫描,直到您选择停止/销毁它为止。文档本身已经有了创建启动服务的功能。只要仔细阅读。



希望这有助于某种方式。祝你好运。


I have kontakt.io beacon and I try to write application for background scanning with nearby API.

I use this method to subscribe messages:

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);
                    }
                }
            });

My code is according to this sample: https://github.com/googlesamples/android-nearby/tree/master/messages/NearbyBackgroundBeacons

I´m receiving messages correctly, but when I kill application no more message come.

Is there any way to get messages from nearby after killing application?

解决方案

Haven't really tried using one yet, but have you considered/checked on using a Service? As per the description:

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.

You could create a Service that runs the scan continuously until such time that you choose to stop/destroy it. The docs itself already has step on Creating a Started Service. Just read it thoroughly.

Hope this helps somehow. Good luck.

这篇关于应用程序终止后,Google附近的API后台扫描不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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