Need some guidance with multi-app IPC - java

All I’m really looking for here is a little guidance. I’m trying to create my first official android app. I’m new to java/droid but not programming. Over the past month I’ve created quite a number of little experimental activities, services, threads and whatnot and they all function as planned. So now I’m trying to tie is all together but not having much luck.
In a new project I’ve compiled the guts into 'my.main.package' which runs a service that is constantly crunching data that other clients/apps can use… Well that’s my plan. For example, in this service is a custom thread/loop timer that is constantly counting. What would be the best way for any other apps to get a constant feed of this timer and other data as a listener could within its own sandbox and in the least taxing way possible?
I’m assuming one must implement aidl for IPC but I’m not sure if its needed and/or necessary as data from my.main.package is only outgoing, i.e. other apps only need receive/listen. I understand there needs to be some form of message handling or parcelable marshalling going on and possible permissions with aidl but I got to thinking that encoding/decoding a parcel or sending a message every millisecond would be very taxing. Is aidl the only way to go or is there a way to broadcast data as you can intent?
Any help would be greatly appreciated!

In a new project I’ve compiled the guts into 'my.main.package' which runs a service that is constantly crunching data that other clients/apps can use
A service should only be running when it is actively delivering value to the user. Users think that developers who create services that run all of the time are idiots and attack their apps with task killers, force-stops from the Settings app, etc.
Perhaps your description is just depicting your app in a poor light, but "a service that is constantly crunching data" is an anti-pattern. These are phones and tablets, not servers.
What would be the best way for any other apps to get a constant feed of this timer and other data as a listener could within its own sandbox and in the least taxing way possible?
The best way is for them not to be separate apps in the first place.
For example, in this service is a custom thread/loop timer that is constantly counting.
This is not adding value to the user.
I’m assuming one must implement aidl for IPC but I’m not sure if its needed and/or necessary as data from my.main.package is only outgoing, i.e. other apps only need receive/listen.
A remote service using AIDL is one way of doing IPC. It is not the only way. It is not even the most common way. You can also:
send a broadcast Intent
have the client send a Messenger to the service, and the service sends messages to the client via that Messenger
have the service update a ContentProvider, and have clients register a ContentObserver on the ContentProvider
I understand there needs to be some form of message handling or parcelable marshalling going on and possible permissions with aidl but I got to thinking that encoding/decoding a parcel or sending a message every millisecond would be very taxing.
IPC is "very taxing" in general. Hence, IPC should be avoided wherever possible.

In fact, IPC with AIDL is the only & the most effective way to do your requirements, I think. If you need one demo, you can take a look here. It's a simple example I wrote to learn AIDL & Binder on Android. Hope it could give you some brief for starting.

Related

How to create an app to consume real time notifications

I want to create an app to consume real time data from an API. This API give me information about different temperatures. When a certain temperature is exceeded my app need to notify the event to the user.
This app need to run in Android and a web browser. So, my problem is the architecture... My app need to be a websocket or a REST API?
Any help is appreciated!!
You need a notification service, and google has something like that for us...
how does this works??
Take a look at the image below,
you need to register your android app in the google service, and your web interface will need an id, so everytime you want to push something to the android, your web interface instead will push it to the google server with the Id of the app, then google (no matter how) will localize your app, and even if its not running, they will get the notification,
behind the scenes there is a couple of thing that you must do, bu nothing like launching rockets from the NASA.
I will suggest to take a look to some tutorials
in order to start with the registration of your app, get the api key etc etc..
In addition to what #Xoce showed, Amazon has the SNS service which will push notifications to you. Or, if you application is a web based application inside of a native (i.e. Cordova), I've used PubNub for JavaScript based events.
A word of caution though - you'll need to define "real time" for your application. There will be a slight latency between the event and what your application sees no matter what stack you choose. When I think real time I think in terms of microseconds of delay. You may have seconds of delay. If this is a "hey, your house temperature is above a threshold" type of application then that is fine. If this is "hey, your nuclear reactor temperature is above a threshold" then this may not be the way to go.

confused between syncAdapter,services,loaders,providers and asynctask? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to android and was going through the documentation and some tutorials.
Services
Developer guide says it should be used, when there is required to run a long running task in background, say music player.
Asynctask
Creates a worker thread to perform background task like getting data using api and then notify the UI thread on it's onPostExecute() callback method.
Loaders
Performs operations on separate thread, registers to the listener and notifies on data set changes.
Providers
Share data between different apps by exposing them in the manifest file.
SyncAdapter
For synchronizing data between android device and web server.
Theoretical wise, i understand the above concepts like what they are used for.
I am having difficulty to put them in order like when to use what? what would be the best among them to use? In what scenarios what should be used?
For caching, i used sqlite or library like volley and retrospice
As i said i am android beginner and try to understand these concepts.
Any helps and suggestions would be appreciated.
Thankx in advance.
A couple of things to add to Kaleb's answer:
ContentProvider:
Android has actually many ways to share data. The ContentProvider is a way to share a database between applications. Let's say you have 3 email clients on your phone that cache data from the cloud in case you're offline. It makes sense that you maintain only one offline database of your emails. The same goes if you have an address book, an sms database, or a tweet database. And the same goes if you want to update all that data. You only really want to update that data only once from the cloud, not three times each time, which brings me to the next topic, the SyncAdapter.
SyncAdapter:
A SyncManager is smart enough to do all the synchronization requests in one go, to minimize the time the antenna is powered, and therefore to save battery use. Furthermore, with the help of the AccountManager, the user of the phone can override the synchronization preferences himself/herself, but that's only if the developer chose to use a SyncAdapter in the first place.
Also, a SyncAdapter can only be used with a ContentProvider. So even if you don't want to share your data with other apps, you'll have to use a content provider if you want to use a SyncAdapter.
That being said, ContentProviders are very hard for beginners (even intermediate developers) to implement. I'd suggest you stay away from them for now. You should use a ContentProvider, if there is already one for what you want to do, but I don't recommend you try to create your own.
Loaders:
Loaders are good. Learn to use them if you want to display data from a local database (or from a ContentProvider). They'll save you time. Unlike the SyncAdapter, loaders do not require a ContentProvider for them to work. They can access SQLite directly.
Services:
Learn to use them. There are too many things to say about them. One important point is that you should minimize the time they stay alive by using components like AlarmManager or BroadcastReceivers. Also, you'll need to learn the difference between a Service and an IntentService.
AsyncTask:
AsyncTask is very often necessary in Android to avoid blocking the main UI thread. Don't think that because you're using an AsyncTask, that you can dispense with the use of Services.
Note that many Android tutorials only give you the minimum amount of code to demonstrate a concept, so they'll often skip proper threading. Note that you can start your own thread manually if you want, but AsyncTask does other things for you that make it the ideal choice for many situations where the UI thread gets blocked and you get an "Application Non Responding" error.
Libraries:
There are many good libraries out there. I won't say which ones are the good ones. Learn to use the ones that everyone recommends. Those libraries can do a lot for you (assuming you're good enough to make them work). There is a bit of a learning curve, but it's worth it. And they deal with Android at a much higher level of abstraction, so usually, the threading and many other things are usually taken care for you.
There are many other things that I am glossing over, or that I didn't mention at all, but like I said, I think your question is too broad. And if you really want more details, you should hunker down and read some the developer guides and watch some of the youtube videos provided by Google.
Really quick answer :
AsyncTask : short task that could block the UI thread. You don't mind if they are cancel and you have to re launch them.
Services : use when you have long task that you don't want to be interupted by App changes. Bit harded to implement than AsyncTask.
Loaders : designed for database access
SyncAdapter : here you don't have realtime data. You schedule data sync at a give moment (i.e sync mails, contact data, etc...). Let's say you have fresh data every hour.
Providers : nothing to do with the above. This is used to share data beetween Apps. You don't care how data is retrieve by the sharing App, you just know that you can ask for a given resource.
This infographis helped me understand better the first 3 : https://raw.githubusercontent.com/stephanenicolas/robospice/master/gfx/RoboSpice-InfoGraphics.png

Best way to implement Socket.io in android

I am planning to implement Socket.io in android by this library for a chat based application. As far as I understood the library seems to be pretty good. I want to know how to maintain a single socket connection throughout the app all the time? Here I have listed out ways to achieve, in which I need the best and stable way.
Three ways
MainApplication Class extends Application
By this we have a good scope that the socket connection is maintained in the main thread( or application's life cycle) and whenever the socket instance is needed from the activity we can get it easily. But it's main thread which also the problem. It might block the main thread.
BoundService
By this way we can bind the service with the activities and we can simply use it. Doing in separate thread is the way to achieve IO/Network calls. But cross processing transfer is more expensive than directly accessing in same process.
Singleton
Maintaining connection in Singleton also makes sense. But we don't know when the instance is killed by process, because it doesn't work in activity life cycle.
If I makes sense please help me out. If not comment it out.
Edit
I have given the answer which is more suitable for me.
First of all, the Application's onCreate() is irrelevant in your use case because you can't have a thread running in the background when it was first initiated in a non service code.
Also, I would recommend using Google Cloud Messaging instead of creating your own mechanism. It would be best for the device's battery life and much less code for you to handle.
If you do want to implement that chat completely on your own, Service is your only choice. You can also combine it with a singleton, but I wouldn't recommend that approach. You can use broadcasts and BroadcastReceiver for communicating between your Service and Activity, I think it is easier than a Bound Service since bounding to the service is asynchronous and it creates a lot of mess compared to simple broadcasting.
Service for Maintaining socket connection
As per Ofek Ron mentioned Service with BroadcaseReceiver is a better idea than BoundService. Because its a tedious process to maintain communication. And I also recommend pub/sub way of broadcasting, like Otto or EventBus (I myself suggest Otto by Square, which is clean and brilliant api).
Pros of OTTO
1. Cleaner Api
2. You can subscribe and publish in/to any Activity, Fragment, Service class.
3. Decoupling. (You have to couple as less as possible in your code).
And one more point is use START_STICKY in the onStartCommand() to start service after it is getting destroyed. See this reference.
MainApplication to start the service
It's best practice to start the service in the MainApplication that extends Application. Because the application will be killed when there is a memory constraint or user forcefully closes the app from the stack. So onStartCommand() will not be called frequently like if we have implemented in Activity.
Implementing online status
You can implement online status simply by implementing Application.LifeCycleCallbacks in the MainApplication class, which has most of the life cycle callbacks of activity and will be notified in the callback. By that you can implement Online status simply without any boiler plate codes. (If anyone need help here let me know).
Uploading or downloading images or files.
Best practice is to implement by IntentService because it's running in the a separate thread. I promise which will give the best performance because it is handled by android itself, not like threads created by us.
You can combine first way and third way like:
Create Sockets in your Application and declare them as static. So you can maintain and access them every where in you application. Don't forget to create separate Threads to perform the network operations, you can use ThreadPool to manage those Thread. If you want to update your UI from those Thread you can use Handler and Message to communication with UI Thread
Before you create your own implementation of a socket.io client, you should give this library a chance: https://github.com/socketio/socket.io-client-java
I use it in one of my projects to communicate with a node.js server which is working pretty fine. Actually, all of your suggestions are correct and mostly depend on what you want to achieve. However, one context is always available: the application context. Therefore you should keep a singleton instance in the Application class and get it by getApplicationContext().
Online status of the user: here you should create a background service which is listening constantly for the users state. As there are not many information, this should be okay and should not drain the battery too much. Additionally you can send a flag if there are new information available and only if there is something new, you start another thread, which is receiving the new data. This keeps the data low.
Chat: the chat data is transfered only when the app is active. So, this should be done in an activity.
Both the service and the activity can access the singleton instance of the socket.io client from the application context. As long as you do not process any complex data on the main thread everything is fine. So, wrap your calls into separate thread and only start them when they are actually needed.

Android Test Platform Generator using java

So I have received a task to develop a prototype software for testing the software/hardware component of android. Example: By using the software I can send a command from my PC to my android to change the color of screen display without interacting with the devices(touch,shake or other possible gestures). I would like to know how do I actually start. The whole idea is actually messed up in my mind. Because after a few days of research I haven't found any topic related to what I am going to do. I am still fresh with java so please go easy on me :) Any help is much appreciated.
hi this is very much feasible. First you need is a web service that connects your software to your phone. When you give command on your software, the command should go trigger the web service which in turn should send a push notification to the phone to execute your command. You will be needed to create a some sort of listener and push notification system on your phone(Hint GCM may work). Also you will need to add permissions to your app that is listening to the notifications. Does that make any sense to you?

Android - library/app communication

I've worked with Android in the past, but haven't done anything super-advanced or what I'm about to describe so need some guidelines as to what the best approach/method is to do this before I proceed.
I'm not entirely sure how to google this, so it's best to explain.
I want to build an Android library project preferably with the source undisclosed. I read this can be done as follows: Create another jar that the Android library project references. However, not sure if all of the source code can be private. If anyone can point me somewhere, that would be great.
Asides from that, the library needs to expose an API for any Android app to use, and some sort of event mechanism to broadcast an event when certain events happen (e.g when the app is in foreground etc).
A scenario would be:
1) User loads the app which has the library embedded
2) The embedded library detects that the app has loaded and 'sends an event' to the app
3) The app captures the event and does some stuff specific to the app + an API call to the library
I guess what I'm interested mostly is figuring out what the best ways are to capture the callbacks by the app, once the library has sent some event to the app and to reduce the burden on the developer having to spend too much time implementing what needs to be done when certain events are captured.
Hope this makes sense.

Categories