How to get access to values from every Activity using Service - java

I trying to make the app similar to Nissan Leaf Spy. This app receives data from bluetooth interface ELM 327. My goal is to collect data like:
Speed
Temperature
Power
Battery capacity
And some more data
And display them on real time chart using GraphView.
For one parameter is one chart in Activity. So there are at least as many Activities as parameters I need to display. My guess is to use
Android Services
to do work in background to co collect and save every data in different array via bluetooth. Of course when I change Activity to see another Activity the one that works will stop working and there will be no more real time.
The question is: is there any kind of 'superclass' that is always working or do I need to save this data using SQL? Or should I just use intent.putExtraString(key,value) and getIntent().getStringExtra(key). I will be grateful for your help!

About having different activities for different parameters, you need to have just one activity. You can have a graph and different ArrayLists with adapters for parameters and then use one of them to feed the graph according to the parameter selected say, from a Spinner.
To feed those ArrayLists is just as easy. You can have a Service running, for general data collection, with an AsyncTask inside it, which will keep the feed live for a selected parameter when the app is active and not in the background. The Service, by itself, can collect data in some sort of a buffer large enough to feed those graphs.
Remember, AsyncTasks are good for updating UI components without blocking the main thread.
EDIT: Look, if you have an activity (let's consider some other activity than main) where you're going to show the data or graph, you can have AsyncTask running as soon as you enter the activity(you can define a default parameter for a graph to be shown) or when you select from a drop down, giving you real-time data while you're on the activity.
The reason I am using AsyncTask for the live feed is that you can have different UI views and seamlessly integrate without any future problems and that it'd modularize Service into functionalities for serving Activity and would end when you close the app. The Service running in the background would primarily provide to a temp log file or be an InputStream source for AsyncTask when it runs after app launch or activity launch.

Related

How to fetch data from SQLite in the background with callback without Room?

I want to know how to fetch data from the local SQLite database in the background with a callback in the main thread. Say, the callback should be in a Fragment. And I want to avoid using the Room library. Currently I fetch the data in the UI thread using SQLiteHelper.
I have considered various options and understood that there is no silver bullet. For example:
I could have used AsyncTask, but it should be subclassed and in case the activity is destroyed before the task is finished the data is passed to the destroyed referenced activity. As a result - a memory leak. Although, I restrict the screen rotation that lowers the chances of the Activity destruction, should I keep neglecting using AsynkTask?
Another option is IntentService. In this way I can fetch data from SQLite in the background and use EventBus for a callback. However, seems like it is an overkill for such a task. The plus - easy implementation.
CursorLoader with ContentProvider is the third option. I believe this is what I need. However, I believe the code could be verbose. I havent used these classes and after reading miles of texts, still dont know how to implement them.
What option should I choose? Please, provide me with a guided code in case you have one.
Please correct me, if I am wrong in some statements.

Android/Firebase - store firebase data in background for use on UI instead of loading new data when an activity is started

I would like to know, what is the best way to store data from a firebase data in the background of my android app so that it can be used on the UI at any time.
Currently I am accessing the database and then applying the data to the UI within public void onActivityCreated in my fragment but it takes a few seconds to load the data meaning that every time the user accesses this fragment, they are sat waiting for the data to load.I feel like it would be better for the user if the data was stored somewhere in the background so it could be used instantly at any time in order to reduce loading times but I'm not really sure how to go about it.Any help appreciated, thanks.
I discovered what I was looking for was the singleton pattern.
Singleton in Android
https://code.tutsplus.com/tutorials/android-design-patterns-the-singleton-pattern--cms-29153

Best practice to persist android "home"/"main" activity?

I have an android app with one "home" or "main" activity that relies on fragments to accomplish several tasks. This data relies on information retrieved from a server (mine, and presumably a substantial amount by google maps).
I would like to structure my code such that several other activities (ie. preferences) can temporarily take focus before returning to the main activity.
Currently android destroys and recreates the main activity, which means bandwidth is wasted every time.
There are several notable intent flags which 'solve' this problem (Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and Intent.FLAG_ACTIVITY_CLEAR_TOP), however they only appear to be useful when transitioning back to the main activity, which means I have to #override the system behaviour for both onKeyDown() and onBackPressed(). I would really prefer not to do this in case it causes other issues or eventually becomes deprecated.
Is this safe? Or is the better solution to force my application to create a serialization (savedInstanceState) of the main activity and all fragments anytime another activity temporarily takes the foreground?
Using saved instance state is the proper approach here. That will let you persist the information retrieved from the server when the activity gets re-created.
You only need to implement code to save / restore the instance state in the fragment or activity that holds the data. If you want to share that data across all your fragments, you can place it in the activity and add code to save the instance state of the activity. Then you can access the data that's stored in the activity from your fragments with ((MainActivity)getActivity()).getData().
For code to save and restore instance state, take a look at:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
http://developer.android.com/training/basics/activity-lifecycle/recreating.html#RestoreState
If the data you need to persist is really large, you can use a retained fragment instead, as explained here:
http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject
Another trick for large data objects is to use a singleton class to store the data while the orientation change takes place.

Best practices to get data and save it

I have an android application and in one of my activities I am making a call to get say "Customers", this call is made to an external API, when I get the response I get it as a JSON object. The problem i am having is that I have a ListView in the activity and when you click on of its items it shows you the details but then when you hit the back button I have to make the call again to populate it. In Samsung Galaxy 4S it seems to keep the data of the list view but in the HTC android incredible it's blank. So what I did is, make it rebind OnResume(), this fixed the issues for both BUT the consequence is making another call to that server. When its 10 or 100 customers it doesnt matter but I know that there are some accounts that have up to 5000 and I am sure it will crash.
What are my options to improve performance on this issue with Android?, I tried a static variable but at some point that object got cleared too.
How do Android applications usually handle this cases where the data is retrieved from API's and they need to be stored through out the application and there is no need to make another call for the same information?, I was thinking on static object but i want to make sure I do this the right way.
You have a couple of options.
1) You can cache the data in memory. For example you can make a static cache or cache the data within the Activity or the App object. If you are doing this in only one view and if it is not a lot of data, this might be an ok solution. However, if you have to do this for many activities and there is a lot of data that has to be cached, you might want to go for option 2. Also storing data in memory in android, does not mean it won't be garbage collected (in some cases, even if you have a reference to it.)
2) You can cache the data in the internal storage and refresh it from time to time.
You can find more info about the internal storage and how to use it here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Basically, you store the response within the internal storage under a specific identifier. The next time you open the activity, you check if the storage has data for that identifier and if yes, you read it and display it. If no, you make the API call.
Keep in mind, that you will have to refresh the cache from time to time.
I had the problem with ListViews on my application too. What I did is that I wrote a custom adapter and that solved the issue..
However the thing you can do is to make a global variable and save the returned results to it. When your application wants to call the server, check the variable, if it's null make the call, if it's not then just draw the ListView with the already fetched data..
Keep in my mind, to implement a refresh button, you need to skip the check.

First steps in Android architecture - please help me avoid a big mistake

I'm a relatively experienced .NET/iOS developer taking first steps in Android development - to help me avoid going down the wrong path I'd be grateful for some guidance:
Essentially, I have an app that displays locations on a map/list. As the user scrolls around the map, the locations are fetched from a JSON web service. A location can be tapped, at which point another JSON web service is called to retrieve live information for that location. The live info is then displayed.
So, having read the various 'getting started' Android docs, I would imagine I need:
An Activity to display the main map view of the locations
A second Activity to display the list view? These seems odd since I get the impression that each Activity has to be an entire screen of the app. I'd like to persist the other UI elements. (e.g. button to switch views, button for settings etc) Is this possible?
A Service (or IntentService?) to retrieve the locations from the web. How should it let the Activity and ContentProvider know when new locations have been retrieved - via Broadcasts or should they bind to it?
A ContentProvider, to cache and persist my locations. Perhaps the content provider should broadcast to the activities when new data is available to display?
Your help would be very much appreciated, since I feel a little lost!
Carlos
PS: I'll be developing with Mono for Android, unless enough people advise against
An Activity to display the main map view of the locations
Yes
A second Activity to display the list view? These seems odd since I
get the impression that each Activity has to be an entire screen of
the app. I'd like to persist the other UI elements. (e.g. button to
switch views, button for settings etc) Is this possible?
Not necessarily so. Take a look at the Fragments API. It allows you to switch only parts of your UI. It was introduced in Android 3.0, but there exists an official backport of it, so that you can also use it in previous Android versions.
With it, you can put your buttons into the activity, the map in one fragment and the list in another, and then just switch the map with list while retaining the buttons.
A Service (or IntentService?) to retrieve the locations from the web.
How should it let the Activity and ContentProvider know when new
locations have been retrieved - via Broadcasts or should they bind to
it?
I would strongly advice against this. You should use a service if you have long-lasting downloads in the background, like downloading a file or something. Short term JSON requests can and should be handled in the UI process. Use AsyncTask or an Executor for that. There has been advice by Google to put almost all of your requests into a service, but believe me, it's bull.
A ContentProvider, to cache and persist my locations. Perhaps the
content provider should broadcast to the activities when new data is
available to display?
Not required. You only really need a ContentProvider if you plan to make your content accessible to other Applications or the System. For HTTP caching, you can directly access the database/filesystem, or better yet, use the Apache HTTP Client Cache. Works well if you use the already embedded Apache HTTP Client, which you should.
Points 1 and 2 : You could use Fragments to update part of the screen, Activity will act as a container for multiple fragments ( use compatibility library for back porting fragments to API level 10 or less
You should use AsyncTask instead of a service to get the locations from a remote web service
AsyncTask has a callback onPostExecute(..) which will be called on completion of remote fetch, this can be used to update List, Maps or Fragments
1 . You can use MapActivity for map view;
2 . use Activity and place listView to include button in a single view instead of ListActiviy
3 .please follow the link for location updator tutorial
http://www.vogella.de/articles/AndroidLocationAPI/article.html
4 . use map overlay technique for your message display
please make comments if u want any suggestions further after u go through it

Categories