问题描述
我使用pubnub作为一个Android应用程序和服务器之间的发布/订阅通道。目前,我在考虑我将如何实现这一点。
I'm using pubnub as a publish/subscribe channel between an android app and a server.Currently I'm thinking of how I will implement this.
我使用的为Android提供的库(的https:// github上。 COM / pubnub / pubnub-API /树/主/机器人),但我认为会有一些问题,如果我使用它像它现在是应用程序生命周期。 (纠正我,如果我错了)
I'm using the provided library for android (https://github.com/pubnub/pubnub-api/tree/master/android) but I think there will be some problems with the application lifecycle if I use it like it is now. (Correct me if i'm wrong)
我想实现它作为一个服务
I was thinking of implementing it as a service
我要什么
-
该服务已继续运行一个小时(可转让)最后一个应用程序的使用之后。这是因为我们希望有通知发来的邮件,但是该应用程序是在当前使用的应用程序。
The service has to keep on running until an hour (negotiable) after the last app usage. That's because we want to have notifications when a message comes in, but the app is not the currently used app.
如何停止的应用程序的非活动一小时后的服务?也许Android将杀死它,但我想一些控制。
How do i stop the service after one hour of non-activity of the app? Probably Android will kill it, but I want some control.
该服务必须能够触发应用程序来改变它的接口时,特定的消息进来(我在想从服务发送意图,当我们收到pubnub消息?),pubnub将数据发送到服务,所以我需要一种方法来该数据传递给应用程序(可能保存在一个包中的意图是什么?)
The Service must be able to trigger the app to change it's interface when specific messages come in (I was thinking of sending intents from the service when we receive a pubnub message?), pubnub will send data to the service, so I need a way to pass this data to the application (probably save it in a Bundle in the intent?)
我要听多pubnub通道(最多2个在同一时间),我想我将不得不为此在此服务的多个实例?
I need to listen to multiple pubnub channels (max 2 at the same time), I think I will have to do this in multiple instances of this service?
我想我会做这样的:
-
创建年代开始,一个服务时,应用程序启动
Create a service that's started when the app starts
让该服务收听pubnub通道
Let the service listen to a pubnub channel
发来的邮件中,发送意图和使用意图过滤器
When a message comes in, send an intent and use the intent filters
实施broadcasthandlers听这些内部意图
implement broadcasthandlers to listen to these internal intents
这是正确的方式做到这一点?任何提示?
Is this the right way to do this? any hints?
推荐答案
您有一个很好的一组问题的详细点,我将谈谈这个答案。您正在使用Android和你有兴趣的约定和最佳实践PubNub发布/订阅方案。
You have an excellent set of questions an detailed points that I will talk about in this answer. You are using Android and you are interested in the conventions and best practices for PubNub Publish/Subscribe scenarios.
您的使用情况是很常见的,并构建应用程序的最佳方法总是变化取决于应用需求。然而,你绝对有正确的想法,并要求所有正确的问题。你只需要一些样品code和方向,以获得在实施您的应用需求的细节开始。要定义列表中的您的需求:
Your use case is very common and the best ways to build apps always vary dependent on application needs. However you definitely have the right idea and have asked all the right questions. You just needed some sample code and a direction to get started on implementing the specifics of your application needs. To define your needs in a list:
- 连接/断开能力。
- 在不间断的后台服务,可以发送/接收数据,并通过Android的意图通知其他应用程序。
- 连接到多PubNub频道在同一时间。
所以上手,我会为你提供直接链接到一些例子和方法:
So to get started I will provide you direct links to some examples and methods:
- 创建已启动的服务时,当机器人靴:https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/BootReceiver.java
- 取消/断开例子$ C $,当你想停止监听一个PubNub通道C:https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/MainActivity.java - 听多渠道很容易通过将阻止pubnub.Subscribe()方法的线程内 。
- Create a Service that is Started when when Android Boots: https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/BootReceiver.java
- UnSubscribe/Disconnect Example Code when you want to stop listening on a PubNub Channel: https://github.com/pubnub/pubnub-api/blob/0dfd8028b803332f5641adc909b1a26f87bd7ff1/android/PubnubAndroid/src/com/aimx/androidpubnub/MainActivity.java - Listening to multiple channels is easy by placing the blocking pubnub.Subscribe() method inside a Thread.
关于你的想法 - 这的是的正确的方式做到这一点:
Regarding your thoughts - This IS the right way to do it:
- 创建年代开始,一个服务时,应用程序启动
- 让服务听PubNub通道。
- 在发来的邮件中,发送意图和使用意图过滤器。
- 实施BroadcastHandlers听这些内部意图。
这篇关于对于pubnub在Android上的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!