Json Android: different activities or one class - android

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.

Related

Receiving an array of MyObject from another app android

I want to design an IntentSerive class in my Android app that receives an array that has objects of MyObject class from another app. The class MyObject is defined in both apps.
I just want to know the steps to make it happen...for example, I know that my service needs to define an intent-filter to communicate with the other app.
Is it even possible? does MyObject class have to be Parcelable?(I don't even know what it is, I read something about it...)
Thanks
Yes, It's a common task.
The main purpose of Intents is the communication between independent applications. You are not limited to intents for interaction between your own activity and the service within single application. Android has a special "binding" technique for direct communication between an activity and a background service.
There is an excellent brief tutorial on this topic.
I think you can serialize your object to String (by JSON) then pass it as an extra of Intent. Then your app read the String extra and deserialize it.

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

Reuse an activity relying on a subclass of Application class in another android application

I have an android application in which I subclassed Application class (defining MyApplication), and some Activities having a MyApplication instance as a member and relying on it for some global configuration variables and functions.
Now, what if I want to reuse one or more of the activities in another android application? I guess I can put the activities to be shared in both applications in an android library project, but what happens if I subclass Application class in my new application as well?
Will the two different instances of the two different Application subclasses be able to exist at the same time, will they create any trouble? Should I think about a totally different approach?
Yes this will not work.
solution (used to create a very robust framework)
Create your library with activities.
Create an Interface which your Application class should implement. Say, you named it: MyInterface
Now each time you get the Application, cast it to MyInterface, and call the functions that you need.
In other words, each app that uses this library should create the Application class that implements MyInterface

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.

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

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.

Categories

Resources