问题
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?
回答1:
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.
回答2:
There are two ways to kill an app. If you find the app in Settings > Apps and press "Force Stop", or equivalently if you run adb shell am force-stop <package>
, that kills the Nearby subscription too. But if you kill the app by swiping it away in the recents view, or by using adb shell kill <process-id>
, the Nearby subscription should stay alive and wake your app with a PendingIntent.
来源:https://stackoverflow.com/questions/36481333/google-nearby-api-background-scan-doesn%c2%b4t-work-after-application-kill