Best practices for pubnub on android - 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.
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
What I want
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.
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.
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?)
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?
I think I will do it like this:
Create a service that's started when the app starts
Let the service listen to a pubnub channel
When a message comes in, send an intent and use the intent filters
implement broadcasthandlers to listen to these internal intents
Is this the right way to do this? any hints?

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.
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:
Connect/Disconnect Ability.
Always-on Background Service that can Send/Receive data and notify other apps via Android Intents.
Connecting to Multiple PubNub Channels at the Same Time.
So to get started I will provide you direct links to some examples and methods:
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:
Create a service that's started when the app starts
Let the Service listen to a PubNub Channel.
When a message comes in, send an intent and use the intent filters.
Implement BroadcastHandlers to listen to these internal intents.

Related

Best practice for Android app: Multiple services or just one

I'm working on an Android app which controls some BLE enabled hardware. It also accesses a Server API and use location services.
The current app organises this as three different services: Location (system service), BLE service and Server service.
That means a lot of broadcasts coming to the GUI part of the app and a lot of logic in the Activity.
I would like to move more functionality into a background service for two reasons:
- Enable the background service to perform interactions with both BLE, server and receive locations while being in the background.
- Simplify the interface between the Activity and the services (also makes it easier to test functionality).
I would like some "best practice" inputs on what solution would be best:
1: Create an additional service which is the entry point for the Activity and which then starts and communicates with BLE, Server and location services.
2: Merge my server and BLE code into a single service and add locations to that?
I'm kind of leaning towards 2 to avoid too many broadcast being thrown around making things more complex even with the more functionality getting stuffed into one service.
Any thoughts? Any comments on several services communicating?
Thanks

To get the XMPP message in the background without using persistent Service

I am trying to build an IM using Smack Library. I did it correctly and its working fine in foreground and I could start a STICKY service which can look for the message in the background. My issue is that I don't want a persistent service in the background, because it will eat up the battery of the android device, instead I want some broadcast to be fired up when the XMPP message comes with some events.
Is there any way I could achieve this? I have tried looking for example with the search term and I found nothing so I did not achieve any sort in this particular context so does not have any relevant code.
I think that all depends on what you want to archive.
If you need fast direct message arrival when the 2 clients are online, I think that the persistent service is the only solution with smack xmpp . With solid code development it should not eat up much battery...
If you don't really care if the message arrives after 2 minutes for example, you should use an intentservice (connect/get messages/disconnect) & a timer (e.g. every 2 mins) as long as the app is running.
What you describe (some broadcast to be fired up when the message comes) is more similar to PUSH mechanisms like Google Cloud Messaging...

android application design

I have to develop an application where i have to continuously collect the GPS, accelerometer data [esp. when user is in motion i.e traveling, else it can be relaxed). Also I have to communicate with web server and handle the response from the sever. So for this(handling response ) part application should continuously poll.
Based on response from server I have show UI to the user.
I am not very clear about the design.
Do I have to create activity from where I start service. Should service be ran as a separate thread(this always runs in background).
I am planning to create two services. One service to continuously collect GPS data.
Other service for communicating with the web server[start timer/alarm manager] for polling.
ALso can these two services communicate with each other.
Also after processing the response frm server the service should be able to start user interface to show some form. I am not clear how to handle this.
Also is acquiring wakelock required if I have to collect GPS data continuously.
Please clarify.
Thanks
You can use an AlarmManager for polling. You just need to set the interval. You will notice that most components Application, Service, Activity all are Contexts. So they all can get Application Context. The way to think about communication is that the android message/event is essentially the Intent. So you define your custom Intents. You just need to give them a custom name for the action. Then you use Intent filters. You can do point to point messaging by doing startService, startActivity or you can broadcast the intent. I would recommend that you communicate via broadcasted intents. Message routing is handled using Intent filters. Starting form etc would just be a startActivity(Intent) and the Intent itself could contain using intent.getExtras(). Just so long as you can put it in the Bundle you can pass it and display. So to answer your question, just use Intents for everything. Use broadcasting and filtering for communication and Intents again to start your activity for display.
The May 10th Google I/O had an afternoon session called Android Pro Tips. The first section covered several different approaches to making your app more continuously location aware.

Android - New to Android, is this use of Application Components is correct?

so I'm extremely new to Android development. The only thing that I've actually coded is Hello World. Then I used Google App Inventor but realized that that was not what I needed. I've also read through the developer's guide. Anyway, I'm working on a project where we use various sensors (accelerometers by means of a wiimote and strain gauges converted to a digital signal through an Arduino microcontroller) and process the data on an Android tablet which will display the data for the user. Then the processed data will be uploaded to a website. Before I do anything else I need to plan out how to build this using Application Components and I wanted to make sure my general ideas are correct before I start trying to code and run into complications. So below is a list of how I think this application should user each of the application components.
Activities
The user display
Services
Timer
Broadcast Receivers
Listen for new accelerometer data
Listen for new strain gauge data
Content Providers
Send the data to a website.
Sorry for asking such a basic question, but I find it's best to understand a tool well before I use it and I've been told that this place is noob-friendly. Thanks for your time.
Broadcast Receivers Listen for new
accelerometer data Listen for new
strain gauge data
Not necessarily. Broadcast receivers listen for new intents. New data won't be given in an intent unless you're the one who sends it.
Content Providers Send the data to a
website.
Not really. They're for inter or cross-application sharing of data, usually from a database.
It seems you don't have a very clear understanding of any of the components of Android. Instead of just guessing, you should take a look at one of the many example android projects all over the net.

What is the best way to implement a "stub" on Android?

I'm creating an Android application that will register an Observer and listen for events, I will probably use one of the suggestions by Mark in this previous question.
However, my question is, how can I create a "stub" on Android that I can use to fire events at my applications observer? For the purpose of this example, we'll assume that my app is listening for a wifi signal strength, I need to create something that will run on the emulator (or device) that will mock up data to "pretend" it has a strong/weak signal, and to relay that to my app.
Any suggestions?
I am no Android expert but here is my catch.
If you are implementing an observable, I believe you need to create a Service by inheriting from ServiceBase.
Also create a content provider and allow other applications to insert data. The whole notification is built into the framework so if you have a cursor, you will get notifications of change in the data.
So here are the steps:
You run the service and register for notifications
Application is getting an instance of your service and registers to get back a token
They use your content provider to insert event along with the token they got
They call the notification
You will be notified whenever anything changes.
I know that there are notification services built into the framework but I have never had a chance to look into it.

Categories

Resources