How can I share an Object between a running Service and Activity - android

I have an API Object that I made, and I would like to share it between a running Service and various Activities in my app, almost like if I was to make the class static. How could I go about sharing the created object between the two?

You may find the following helpful.
Binding a Service to an android.app.Activity vs Binding it to an android.app.Application
Android Service interacting with multiple activities
Alternatives for Pushing data from an Android Service to an Activity

I figured out the best way to do this is to have a class that holds all the information that you want, and to use that class through each activity and service.

Related

Json Android: different activities or one class

I have a small doubt, I am developing an application with various activities that call a database via json, did right is to make calls in the same activity importing all packages in each activity or create an activity / service / class its functionality is the treatment of calls to the server?
Create a class that exposes your required service methods, then use that class in each Activity to communicate w/your server.

How to get class name of the Activity which is bound to a Service?

I would like to get the class name (*.class) of an Activity which is bound to my local Service. Is there any native method which i could use to achieve this?
I tried to get that information out of the Intent I use to bind to the Service. But it seems there is no function in Intents for this.
If I wasn't blind, additionally I would like to know why there's no information about the calling Activity in an Intent?
Is there any native method which i could use to achieve this?
No, in large part because the class in question may not exist in your app, and may not even be a class.
You are welcome to package whatever identifying information you would like in the Intent, such as via an extra.
Also, if you are allowing third parties to bind to this service, you can use methods like getCallingUid() on your Binder to find out information regarding the process that bound to you.
additionally I would like to know why there's no information about the calling Activity in an Intent?
Because there is not necessarily a calling Activity. The binding engine works with C; C does not have classes, let alone activities.

Android - Communication between two services in the same application

In an application that I am developing I have a main Activity that starts one Service (it is a floating window, I am using StandOut library). The same application contains a class that extends InputMethodService, I'd like to make them communicate, since I want to handle some Events in InputMethod calling methods contained in instances of classes created inside my StandOutWindow. I tought to use SharedPreferences, is this a good way or are there better ways to make them communicate?
I think you must use bindService to communicate with your service
http://developer.android.com/guide/components/bound-services.html
To communicate with your InputMethodService you can follow this tutorial:
http://android-developers.blogspot.com/2009/04/creating-input-method.html
And to comunicate between services:
Android communication between two services

Service or not for persistent data

I have an application that keeps some complex data in memory between activities.
As for now I use a Singleton class that use SharedPreferences in getters and setters.
What I want: As long as my application is live and showing in the recent apps, I want a class to never get released or find a way to achieve this another way without consequences.
So I was wondering, is a better way available to me?
Would a Service be better?
If so, should I start and/or bind it?
If you go with a service, you wouldn't bind it as unbinding could cause it to stop.
You could create a static object and create it in a custom Application class. So for as long as your application is alive the object is held by a strong reference.
Or a combination, use a singleton class but let the application class store the reference to prevent garbagecollection(GC)
after chat:
a service running in its own process should be the most persistent thing you could build. However you need to communicate with the service via AIDL, a cross-process bridge, which draws performance if the communication is high-speed.

Binding and connecting an android service outside an activity

I would like to bind and connect a service but not within an android activity. Is there a class witch could be extended to have a context necessary for binding?
What i am trying to do is to provid a simple java library using an android service. My library does not use a UI. I only need to bind and connect my service inside a class witch necessary have application context necessary to the binding
Thanks
You can get the context from your application class. Derive your own class from Application, and give it a static getApplication method. You can use that for creating services.
Note that without an Activity, binding to a service may be a little hard - if, for example, you're in a BroadcastReceiver, it's not going to be alive long enough for you to receive the callback after the service has been bound.
Simply create an application without default activity. Then extend base Service class. And do not forget to describe it in the manifest file.
Service has its own context.

Categories

Resources