last day (16 days later) » 

16:39
5
Q: How to handle the necessity of interprocess communication in my Android application?

RyanMy application requires me to periodically do the following: Send periodic heartbeat information to a server, once per 15 seconds Take periodic screen captures and send them to a server, once per 60 seconds Send message to and poll the server for messages, once every 5 seconds I've written th...

First and foremost, the Android has no way of ensuring that any background apps keep running; it was never meant for daemons. Second, periodic server polling will kill the battery and the data plan. Third, it sounds outright spooky - what's with quiet screen captures?
Devices will be mounted in public, hard-wired to a power source and on a wifi network so battery life/consumption isn't a concern. The goal of the screen caps is for monitoring purposes to ensure the application is running correctly and to help with debugging.
Then Android is a very poor choice for an OS. Consider one of the slimmer flavors of Linux or Windows Embedded.
What I need cannot be done with Android then? I'm constrained by the desires of my employer so I don't have the luxury of better choices in this case.
I don't know the entirety of what you need, so I can't tell for sure. But Android is known for killing background apps with no forewarning. It's not a server OS, never was.
16:39
Not sure if this makes much of a difference or not but the app I'm trying to write is a launcher and will be set as the default launcher for the device and if the app is binding to the services in question wouldn't that keep things running somewhat how I intend?
Why don't you implement broadcast receivers in these services to communicate with each other?
I wasn't just looking a way, I was looking for the best way to handle the communication paths I laid out. Do you think the broadcast receiver route meets those needs and is the best way to handle things in this case?
That's how I would do it. And also return the START_STICKY flag (developer.android.com/reference/android/app/…) on the services to ensure they always get recreated if Android decides to destroy them.
You mention mounted and wired. What is it - a kiosk system? Some kind of CCTV?
Why Android in the first place? Android was designed for mobile phones, later retooled for consumer tablets. Your setup sounds like neither. Why Android?
Yes, it's basically going to be at a charging kiosk in an airport. The device I've been given to work with is the Nexus 10. I didn't have a say. I pushed for a windows tablet to no avail.
16:55
Sounds like the project has some money behind it, not just a hobby endeavor. Is that really the case?
It's for my employer so I'm sure there is money behind it
Basically, we have an application that is written in python used to monitor remote windows based systems in the field... they are trying to take that into the mobile world with an Android tablet... I'm the guy they tapped to make it happen.
Well then, go and hire an experienced Android developer. The fact that a lead developer (I'm assuming you're one) is out on StackOverflow asking for, frankly, rather basic stuff doesn't sound too promising for the project as a whole.
"Kiosk" isn't "mobile" by any stretch :) Just saying.
I'm a web developer they're trying to turn into a software engineer...
Sorry for sounding too harsh, but asking around on StackOverflow is not how you should learn a new platform. The SO community will not design your app for you.
If you're willing to learn, nothing is impossible. It's just code after all. But your first instinct should be to read up, not to ask on SO.
The service lifetime is pretty well documented.
But you could give him some pointers, Seva. How would you approach the problem? I'm curious...
17:01
No problem, I understand. I couldn't quite possibly explain the entire breadth of the project in detail for a question. I've got a lot of the functionality at a point I consider working however I'm just using worker threads and not services to do the work.
AlarmManager and broadcast receivers
But it's not about pointers and specific techniques. I've seen enough failed software projects in my lifetime to immediately notice red flags all over this one.
So you wouldn't use any service?
There would probably be a service to perform the HTTP request and wait for a response. But the periodic poll - you wanna do that from a framework-based timer. AlarmManager is one.
A service can't revive itself if killed.
It is revived by Android if you return START_STICKY
Right, but the timing will be lost.
As far as I see, the OP is not after having the server run all the time; he's after periodic polling with maybe 1-2 seconds of activity (enough to start and finish an HTTP request)
17:08
My employer will just have to accept the fact that if the process is killed that the tablet may stop reporting in for a while but it should eventually start back up. We will also have VNC access to these tablets to verify proper functionality.
If it's a kiosk, is it fair to say that only one app is running all the time?
There is going to be the main app I'm writing as the launcher... it will let the user select their outbound flight and remind them of when the flight is boarding/leaving... in the mean time we give them access to a web browser and a few select 3rd party apps so no, I don't think it will be limited to one at a time however I don't think there will be very many at a time.
The device will be locked down with another 3rd party app (they'd like me to write myself at some point I'm sure) which requires the entry of a pin code to launch apps we choose
I.E. we're locking down the settings app, etc.
Ah, okay. That kinda changes the picture. In a single-app scenario, the chances that the service will be killed are minimal. But this is a truly background scenario.
For the record: an app with running working threads but no running activities/services/content providers is the prime candidate for killing.
Good to know, thanks
I've overridden the main application class in my app and create the threads and bind to services from the application and not individual activities... is that bad? Will that keep my app running any longer?
You're trying to subvert Android's memory saving mechanisms. You understand that tricking the system can only so far, right?
17:18
Yeah, I understand
That a much better approach would be finding a system that explicitly allows long-running processes?
I didn't do that out of strategy, I did that because in my limited knowledge on Android that seemed like the best place to put it given the needs of the project.
By the way, I appreciate you both taking time to chat here. Thank you.
From where I sit, it sounds like "How can I inflate a bicycle tire with a potato? What do you mean it wasn't meant for tires? My manager gave me a potato, I now just have to squeeze it just right."
Ha ha, I'm sure Seva.
What would you do if your employer gave you this task and no matter how hard you pushed back with alternatives they said no, it has to be an android OS?
I'd do my best to manage the expectations. Make sure to cover my ass when the whole thing goes down in flames. Which is probably what you're doing.
17:23
Covering my ass or going down in flames? '=_
;-)
Managing expectations :) It's not down in flames yet.
By the way - the question mentions screen capture; are you after captures of your own app's screen or the system screen no matter what is running? That latter might be trickier; I'm not even sure it's possible at all.
I already told my employer we can only do caps of our app
I hope they remember....
They also want caps of video playing... I told them that isn't likely to happen either
17:27
Again, not sure if it's possible...
Like I said, I have a working prototype (under the assumption Android doesn't kill my app) and the whole point in asking my question was to take the 3 threads I wrote which are serving as members of the application class that seem to be working and transition them into services
You still want threads because Android's HTTP class is synchronous. If you want to be able to run several HTTP requests at a time, you want to run them on threads.
Besides, you can't do HTTP in the main thread since Android 3 anyway.
So you'd need both services (to keep the process alive) and threads, for reasons stated above
A service (which is to say, onStartCommand()) runs on the app's main thread.
Does a bound service work the same?
Is a service bound to my app essentially started as START_STICKY?
Pretty much. The difference is pretty much that between datagram and stream communications.
A bound service is request/response, a command-driven service is one-way.
ok, that makes sense
17:32
I don't recall how does sticky work; but I don't think the logic of "restart the service as soon as you can" is there.
That's why I keep mentioning AlarmManager. It's known to revive killed apps on timer, which is what you seem to be after.
I'll look into that Class
Now, feel free to cache service connections and such in the receiver class; it's not like Android will be killing your app all the time; most of the time.
Most of the time it'll probably keep running as expected.
My point is - the main entry point of the polling is the receiver. The receiver is ready to start the service, if necessary. The service would process commands from the receiver - possibly by spinning up worker threads.
If power is cheap, it's maybe easier (for the coder) to spin up a new thread for every HTTP request or closely related group of requests, rather than keeping dormant threads ready to execute the next command that comes down the pipe.
I'm looking for an example in my stuff
Cool, that'd be awesome if you found one.
There are many parts to it. First, you need to write a receiver and register it in the manifest.
That's just class that derives from BroadcastReceiver and overrides
public void onReceive(Context Ctxt, Intent in)
In the app manifest, it goes:
The only one's I've done were programatically
And they were just tinkering more than for usability
17:42
<receiver android:name=".MyReceiver"/>
Now, there's the whole scheduling business. In my app, it goes like this:
long Now = System.currentTimeMillis();
AlarmManager am = (AlarmManager)Ctxt.getSystemService(Context.ALARM_SERVICE);
Intent in = new Intent(Ctxt, GooglePollReceiver.class)
.putExtra("OrderID", OrderID)
.putExtra("StartTime", Now);

PendingIntent pi = PendingIntent.getBroadcast(Ctxt, 0, in, 0);
am.cancel(pi); //Just in case there's one already waiting
am.set(AlarmManager.RTC, Now + MS_PER_MIN * Proto.GOOPOLL_FIRST_INTERVAL, pi);
Ignore the putExtra calls; that's just passing parameters so that the receiver gets them once invoked.
yeah, got it
Now, the AlarmManager doesn't do periodic. You schedule an intent for a specific time, when the broadcast comes, the receiver schedules itself again.
The GooglePollReceiver is your custom receiver?
Yeah.
Sorry, didn't take the time to make the code generic - copypasting straight from the project :)
No problem, I'm following you
17:45
Anyways, the receiver class goes:
public class GooglePollReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context Ctxt, Intent in)
{
Log.d("inapp", "Got a polling broadcast");
String OrderID;
if((OrderID = in.getStringExtra("OrderID")) != null)
{
Ctxt.startService(
new Intent(Ctxt, PickupService.class)
.putExtra("Key", OrderID)
.putExtra("GoogleOrderID", true));
}

if(System.currentTimeMillis() - in.getLongExtra("StartTime", 0) < 1000*3600*Proto.GOOPOLL_DURATION)
{
AlarmManager am = (AlarmManager)Ctxt.getSystemService(Context.ALARM_SERVICE);
Yikes, the indentation all want away :(
Note the repeat am.set() call in the end, reusing the intent.
That's me scheduling the next try.
Ok, so basically you just keep scheduling the next one to fire
Makes perfect sense
Note: in my case, rather than do the work itself, the receiver fires a command to a service. Just like in yours.
Would it make sense to have a single custom receiver for an app or multiple?
17:49
Well, if you want to run several unconnected schedules, then probably it does.
Oh, one word of caution: AlarmManager doesn't guarantee exact timing.
It's more of a best-possible-effort scheduling
Because the phone may go into sleep, or something
Yeah, I'm sure native android uses that class and I've noticed that if I set an wake up alarm and watch the clock on the device it doesn't necessarily go off as soon as the alarm is supposed to
There's also a setExact method, but it requires the brand new Android 4.4
Cool, well thanks again for your time Seva and Ricardo. I appreciate the help.
Hope this helped :)
It did, I'll probably spend time writing that custom receiver next.
17:56
See how it flies when the tablet is under load. With browser and stuff.
The whole approach is to make the background polling work even when the system is somewhat starved for memory.

  last day (16 days later) »