Logs in ListView? Memory issues? Alternative methods - android

I want to have an activity that shows the logs for the application. When it checks the internet for updates, how many updates it makes, etc etc.
So I'm thinking that there should be a new log entry every minute or so. What's the best way to display a long log like this? ListView? I plan to truncate the logs after...1000 entries? but still I don't want my app wasting tons of memory storing 1000 logs.

Depending on the complexity of your logs (one cell per entry might actually be a bit overkill)
you could try and store logs in a sqlite database, then use a simplecursoradapter and bind it to a listview. You wouldn't have memory problems as from the database perspective it's just text, and the cursor + adapter combination would make sure you only load and keep in memory what you're currently displaying on screen.

Related

Should I store messages in my database or query the system SMS database everytime?

I am developing an SMS app. I am getting messages from the Android Database every time user opens the App.
On a device with around 100 messages, it works fine. But with a device with 10,000 messages the app takes around 20 seconds to display the list after getting all the information.
To solve this, I am thinking about three possible options here:
Should I consider saving all the data to my own database? Will it make it faster?
Should I get some messages first, and then the rest in the background?
Any other option you might suggest.
Thanks in advance.
Below are the answers of your question
1. Should I consider saving all the data to my own database? Will it
make it faster?
No, don't do that. This will not make it faster. Same response you will get.
2. Should I get some messages first, and then the rest in the
background?
You can do this, but there is no need to do this to get all messages in background.Because User can see limited number of messages and chances are he will not see all messages down the bottom. So it will be useless to get those in background (untill or unless there is a business requirement)
Any other option you might suggest.
Yes, you need to implement pagination and need to keep track how many messages are needed to be loaded first time and while user scrolls then get more messages. In this case you need to keep track your own about how many messages you have already loaded and how many more you want to load. TO achive this, you will be required to implement PULL TO REFERESH mechanism of android. And a lot of tutorials can be found over the web.
Hope that answers your question.

android marshmallow app crash large arrayList

Have an application that generates an arrayList of data collected from a rest call. The arrayList is used in an adapter supporting a listView. Had a case where the arrayList reach a size of 999 entries.
The application passes the arrayList via a putExtra on an Intent. The arrayList is used to populate a listView. When startActivity is called, the application crashes without generating a stack trace recorded in stacktrace.
Is there a limit to the size an arrayList can be? Have set a debug break point in the activity started and the crash is occurring right after calling the super onCreate method.
Ideas on how to debug this? Have been able to pass a 390 entry arrayList without crashing.
AS #CommonsWare said, there's a limit of < 1MB but in real practice is way less than that, as soon as you need to pass more data than a couple hundred KBs you need to use other procedures to pass the data around.
The most common one would be to save it into a Local database or other type of storage and then on the other activity query for the data (on the intent you only pass the data needed to query for the list later on) and instead of loading all of them at once, you use a CursorLoader so that you only load the data as you need.
Other quick and dirty way is by adding an static field to pass the data to the other activity and then on load read the value and remove it, the problem that you will face is that this is not Thread-safe nor it will solve the issue of what to do with the data on the other activity when onSavedInstanceState gets exectued, because you are going to run into the same 1MB issue. [WARNING: This is the worst solution you could think of, so, please try anything else before you try this one, that's why i gave you first a very good approach and leaved this one at the end]
[UPDATE:]
BTW the first approach is how we fixed this problem on the Microsoft Band app, because we had so much data coming as part of a very long run (GPS points, and other tracked data). Just make sure that the data is not big enough that you will generate a bottleneck somewhere else, we used to only cache it in memory and never send it to disk to avoid the serialization process or the time it took to save and retrieve from storage.

what is a good way to handle very very large amount of data in Android?

I'm currently working on an Android application that requires reading from call history and text history. And for further analysis, I've extracted these the huge amount of entries from the corresponding content provider, created and inserted all of them to a SQLite database.
But the problem I've encountered is when this is running on a phone that has been used for years (meaning there's an enormous amount of data generated from calls and texts), the retrieval process and database building process takes too much time and may even cause the app to crash. Even if i tried to put these process in a AsyncTask, the problem still exists. So my question is:
Am i doing it in a good way to just put any time consuming operations away from Main UI, OR What's a better way, if any, to handle very very large amount of data in Android?
Use pagination logic. Fetch only the most recent and relevant data and load older data if the user requests it.
Call history on most android phones is limited to 500 entries CallLog.Calls, while SMS provider has not such limits, you can query the count of the tables and limit your queries to 50 at a time and pass it to a separate thread for processing. Also make sure you run this in a background service with low priority so as to not disturb any other operations ongoing in the device.

Save gps-location to sqlite continuously

I'm trying to make my own gps-tracker, mainly for bike-rides. I managed to make a usable app, at least for personal use, but I wanted to improve it, and have added ContentProviders, Fragments, CursorAdapters and a Service to receive the onLocationChanges from GPS.
However, I have really no idea how to handle the stream of Locations I'm receiving. My old app just saved them to an ArrayList, so right now my Service is sending a Broadcast to my Activity, that saves them to the ArrayList.
Problem is, that when the ride is over, it takes from 5-15 seconds to save the locations to sqlite (yes, I'm using compiledstatement and transaction), and I would like to avoid that, by saving the locations when received. When I tried to do that, my app became unresponsive (as expected), as I was doing the insert in the UI thread, and I do receive location updates often.
This is of course the most important aspect of the app, so what is the solution?
I could do the insert in a thread, but since inserting a single record is expensive, I'm not sure it could keep up.
I could write 25 records (or more) at a time in a transaction, but there will be some logic to keep track of what is saved and what is not.
Is there other options for a better user-experience ?
Use an IntentService to delegate the saving to another thread, then use applyBatch to do inserts.
Personaly I think the gps track would be better in a file than embeded in the database tracks are lickly to be just big. It's the same senario as pictures where embeding in the database is not the recomended solution. The databases would just hold some summary information and a reference to the file. You wouild write out the file as you go so no big pauses at the end. You keep the local ArrayList as well while you are recording if you are using it for displaing a path on a map or a graphical plot etc. That's the way I am doing things in my biking app IpBike.

How to save the user's selection for every time they run the app on Android?

The first time the app is run, I want to have a checkbox list appear where the user selects the list items that they are interested in, and run the program based on that. Every subsequent time the app is run, I want those selected settings to be remembered, or changed with an options menu, in which case the new settings will be remembered. But all I know how to do is make an app go from the beginning every time it is run...
Similarly, my app reads sequentially through all the data in a large, read-only, unchangable database. As of right now, it creates and opens and fetches all the data every time, which takes a few seconds at the start of the program to open up and do anything. Is there a way to get around this, or, is it even a good idea to try to get around this?
To remember the users selection, have a look at SharedPreferences. You can store the selected items there.
To the database: That really depends on your app. If you need all that data at the same time in memory, I guess theres no way around loading it at the start. If you only need parts, it would be a good idea to load a part of the data when required. But without exact knowledge of your app/use case, this is hard to tell.
When you have some sort of "lag" when loading the database: Do you probably load the database in the UI-thread (= without creating a seperate thread for loading)? Thats bad practice since it blocks all UI operations. Have a look at AsyncTasks. Thats a handy class that wraps around a thread and lets you do things in the background without blocking all the UI. After it's done with its task (loading in this case) it provides you a onPostExecute() callback where you can update your UI with the loaded data.

Categories

Resources