MvvmCross DataService in an Android Broadcast listener - service

I am currently venturing into the MvvmCross realm and making some good headway, but ran into something that I have been unable to figure out on my own. I currently have an android service that is going to be running all the time. That service is going to be started either on a system boot or when the application first fires up.
That service/broadcast receiver will need access to the DataService that is created in a PCL project with MvvmCross. I have not been able to figure out how to get the instantiated data service into that service/broadcast receiver on creation of the service since there are not any view models that are associated with the service.
I know that it's probably relatively simple, but I haven't been figure it out on my own.

The easiest way to do this is probably to just request that the full Setup is completed during the first part of OnCreate for your service:
var setupSingleton = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
setupSingleton.EnsureInitialized();

Related

OSGi: What happens when an imported service is stopped while the service is still running

Say I am using a service A which is imported in another service B. While B is running normally(ofcourse A is Active), what will happen is service A is uninstalled while service B is still running?
Service A -> Service B
What will be the different scenarios in case I am using ServiceReference, ServiceTracker & DS?
When a service is unpublished in OSGi, an event is sent to all bundles currently using that service to tell them that they should stop using it.
If you are using DS, your unbind method will be called. When it is called, you should make best efforts to stop using the service as soon as possible. But ultimately OSGi is a cooperative system, it cannot force you to release the service. However if you don't then you can cause problems, for example the service publisher will not be fully garbage-collected. You end up sabotaging the dynamics of the OSGi platform, possibly creating memory leaks and so on.
If you are using ServiceTracker then the removedService method will be called, and you need to respond in the same way. But didn't I tell you in the other question not to use ServiceTracker?? ;-)
If you are using ServiceReference then you need to explicitly register a ServiceListener in order to receive these events. This is why you really really shouldn't use this low-level API until you have gained a lot more experience (and once you do have that experience, you won't want to use it anyway!).
First of all: one of the advantages of OSGi is that the behaviour of the framework and standard services are clearly specified. Those specifications can be downloaded from the OSGi Alliance web site, or, if you don't like reading PDFs, ordered for print. The question you are asking is perfectly answered in those specifications.
That said, in summary: when a service is unregistered:
The ServiceReference object remains as it is. However, a call to ServiceReference.getService() will return null. Note that when using ServiceReferences you should release any references to the actual service object as retrieved via getService(), this normally requires some kind of tracking of the service.
For ServiceTracker ServiceTracker.remove is called. This normally results in a call to removedService() on the ServiceTracker or the defined ServiceTrackerCustomizer.
For DS, the defined unbind method for the referenced service is called (if specified). Furthermore, if the cardinality for the used service indicates that the service is mandatory, the using service may also be unregistered, even possibly deactivated or a new instance activated depending on the availability of alternative services and the policy defined for the service.

Play 2.0 - Push current state of execution to page

So I currently have an application independent of Play which may take a long time in its execution.
I want to put a UI on top of it using Play where the application may be invoked and to display some of the details of the execution inside of the application to the user. I would like the page to be updated automatically as the execution proceeds e.g. if a variable in the application increments this would be reflected on the page.
I'm not sure where to begin with this - do I need to split the application up into models + controllers ? Or do I just need to have code in a controller to instantiate the classes I have already coded and call the methods I need ?
What about constantly showing the execution state on the page?
Any resources I should know about/read ? Code examples?
Thanks
You may have already done so, but a good starting point is to create a skeleton Play application using the play new command, while referring the creating a new application section. You will have "views" (HTML template pages) and one controller (in Application.scala). You could add more controllers, but as you will have just a single page that should suffice.
You can add jars from your app (if it's a JVM app) to the lib directory of your Play application. From this: "Or do I just need to have code in a controller to instantiate the classes I have already coded and call the methods I need?" it sounds like you would be happy to have your app run in the process of the Jetty + Play server. Check out the Global object for starting your app at process startup.
Check out the section on comet sockets for sending updates from the Play app to the browser. You'll need a bit of Javascript in the web page.
Do you want to have this application running outside of play, perhaps on another server? Can you modify the application, or is this 3rd party software?
If so, you have to have some way to send data back and forth between your play front end and your application. You can either have your application expose a websocket, and then your play front end and your application can push data back and forth to each other. You can then have your client page have a websocket open to you play front end, and then play can push the updates to the client. If your application can't support a websocket, you could also expose some URLs on your front end for the application to POST to. You can then use some sort of message bus or database mechanism (RabbitMQ, redis, Mongo capped collection, or even just a shared Queue object) so that the front end websocket can get those updates and send them to the client.

Do I really have to use an Activity class to launch my Service

Creating an Activity with a Service and setting up the bindService.
I thought i could make all this run "hidden" like a background Service - silentely this Service (client) would connect to my PC server waiting for incoming. I read the Service class api dock and cant see any description how to do that. Do I really have to use an Activity class to launch my Service. I know i can launch activity classes from the service if I need.
Can somebody enplane this before i go nuts?
What is the BroadcastReceiver "actually" doing?
cant wrap my head around the code implementation here.
after reading many questions here and elsewhere about this
getting fragment of clarification I think the method is:
- an apk file which contains a Service an activity and a
BroadcastReceiver Class.
- action.BOOT_COMPLETED will start only my Service, not the Activity.
The user can from Main Launcher start Activity for "in app settings" and I Bind
to Service and Unbind when done
Yea will try this tomorrow after work.

Activity not responding error on emulator when using webservices in json parsing?

Am new to android, am developing application with websevices using json parsing with httpget method,cant use http post method in android actually.
It working fine normally, but many time it shows the error on emulator like activity not responding force close activity.when i put that url in browser it shows the result .but i don't know why this activity not responding error came.
I think the httprequest took more time to retrieve the data from server,but am not sure. any one help me to how to avoid this error or how to minimize this .
I want know what are the possibilities to get this activity not responding error.
Thanks,
Lakshmanan
You need to perform blocking operations such as I/O in a separate thread - see the below linked resource:
http://developer.android.com/guide/practices/design/responsiveness.html:
In Android, the system guards against applications that are insufficiently responsive for a period of time by displaying a dialog to the user, called the Application Not Responding (ANR) dialog, shown at right in Figure 1. The user can choose to let the application continue, but the user won't appreciate having to act on this dialog every time he or she uses your application. It's critical to design responsiveness into your application, so that the system never has cause to display an ANR dialog to the user.
To avoid ANR (Application Not Responding) dialog,
Your business logic code is inside doBackground() of AsyncTask and You may also need to override onPostExecute(),etc. After that it is better to invoke the async task in a Service (bound or normal service).
Service:
(bound service or normal service based on your requirement)
From, android office documentation:
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.

message queue for iOS / iPad - something like MSMQ?

I have an iPad app that works both on and offline but when I am offline there are web service calls that will need to be made once online availability is an option again.
Example:
A new client is added to the app, this needs to be sent to the web service but since we are offline we dont want to slow the user down so we let them add locally and keep going but we need to remember that that call needs to be made to the web service when we can. Same thing for placing orders and such.
Is there some sort of queue that can be setup that will fire once we have connectivity?
I don't think the overhead of a heavyweight tool like MSMQ is needed for a simple action. You can use Core Data, persist managed objects with the data needed to call the web service, and only delete each managed object after a successful post. There might or might not be a way to capture an event when connectivity starts, but you can certainly create a repeating NSTimer when the first message is queued and stop it when there are no messages in the queue.
This library handles offline persistent message queueing for situations like you describe. It says alpha from a year ago, but I have confirmed it is used in production apps:
https://github.com/gcamp/IPOfflineQueue