Contact management in Android - android

I am able to fetch contacts using this code https://stackoverflow.com/a/1780818/992774 , it works perfectly fine and returns all the emails and phone numbers.But my problem is that when I have 10,000 of contacts it takes about 3-4 minutes and for that time it blocks my UI and looks like my app got halted. I have seen many apps like groupme which shows contacts without taking any time.Can anyone suggest how to reduce the time while fetching contact thousand of contacts or how can I create custom cursor adapter?

Can you do the loading in a separate thread, either using a Handler or an AsyncTask, for an example please refer to this tutorial http://www.vogella.de/articles/AndroidPerformance/article.html
This will leave your UI thread to be free to do UI stuff and you should be able to see your contacts get populated one by one on your screen.

use CursorLoader to get your cursor. It runs asynchronously, so you won't hang your UI thread while you're retrieving data.
It still may take some time. Is it worth stepping back for a second and asking if you need to look at 10,000 contacts? Seems to me that it's nearly impossible to search through them on the screen.

Related

How to read all phone contacts in android faster?

I am working on an application (API > 15) which reads all phone contacts and then suggests all people who are on our application.
Data required :
1. Phone numbers
2. Email ids
The flow is :
1. Read all phone contacts
2. Send them to the server
3. Match with existing contacts
4. Send the matched contacts back to client
5. Suggest the user those contacts.
Now issue is I have close to 2000 contacts in my phone. For that it is taking 46 sec. This is too much. I can't make the user wait so much.
Possible Solution :
Read contacts in several batches of 200 contacts each using different threads. Run them parallely and consolidate the results.
Issue
I tried it but still taking too much time. I think content resolver is thread safe. So it is still not able to service different thread requests parallely and taking same time. Infact a little more than before now.
I think there should be some other solution which all these messaging apps like whatsapp uses. Anyone have any idea?
I can post the code as well but it is the common default one only which everyone uses. I have done all type of optimisations there including not making any extra String variables which might interest android to run garbage collector again and again and increase processing time.
Put everything in a service when the user launcher your app. do everything in background. once you finish popup something to the user.
This is what am doing right now and I hope I help.

Why is the UI thread blocked when work is done on a separate thread?

In my app, I have a ViewPager with +/- 10 pages. When the app is first opened, all the pages are instantiated and immediately begin to load data to display. Each page (which are fragments) creates an AsyncTask to query a database and populate itself with the appropriate data. Here's the problem: even though the work is being done on separate threads, the UI stops updating during the database queries (which are done sequentially, and take 1-3 seconds total). This happens both on my Nexus 5 and a crappy old Samsung phone, so I know the problem is not that the hardware just can't keep up.
So ultimately, I'm wondering why the UI thread is blocked by work done on a background thread. My understanding of threading was that doing work on one would not block the other for an extended period of time. If my understanding is wrong, please explain how. Thanks in advance.
I don't think code is required here, but if it is, let me know and I will post the relevant portions.
It stops animating immediately after the first database query begins and starts animating again immediately after the last database query completes
It is possible, then, you are not doing the work on a background thread that you think you are. You may be doing the work on the main application thread.
Traceview can help you identify what you are doing on the various threads, and StrictMode can help you with obvious problems (disk I/O and network I/O on the main application thread).
In this case, you may be getting caught by how you are doing your work:
Each page (which are fragments) creates an AsyncTask to query a database and populate itself with the appropriate data.
If you are doing your query in doInBackground() but are not touching the resulting Cursor also in doInBackground(), the query actually wasn't done yet. The Cursor is a SQLiteCursor, and it lazy-executes the query when the data is first used. This is another one of those "really cool ideas that just plain suck in how we do things nowadays". A workaround is to call getCount() on the Cursor while you are in doInBackground(), to ensure that the query actually is executed on the background thread.

Android : Loading huge data in CursorAdapter through a single query hangs the UI

I have a listView associated with a CursorAdapter than can have more than 5k of rows. I am using CursorLoader for the same and it hangs the UI for a long time. I have gone through similar posts that are already present on SO here and here.
I do understand that fetching such a big data and loading it on the UI is very huge to process and load into memory. Saying this, I still need to get it in my adapter.
I have considered using Scrolling Cursor but the other problem that I would go through is while implementing Alphabetical Indexing similar to what Android Contacts app have and which stops me from updating the cursor part-wise
Also, my query makes use of a left-outer join along with an Order by clause
I am hoping there might be some way of doing this. It has already been done in the Android basic apps such as Contacts, Music and so on.

Starting a thread when user stops interacting with activity

I am working on a contacts app. I have completed its code. Its running fine and does all the things you can ask it to do without any headache. However, there is still chance for improvement. To remove any kind of lag, I load all the contacts image and primary phone numbers before providing any UI to user with a help of a progress dialog. I want to implement a mechanism, so that application starts retrieving a contact image when user is done with scrolling and is searching for/ reading contact names to get the desired one, so the thread should start as soon as user starts reading/or stops scrolling, and it by any chance, he starts scrolling, thread should pause and let user scroll again. Any ideas how it can be achieved as apart from implementing other lag reduction techniques, I believe it would be best. So, any ideas ? I have no clue how to start this mechanism.
I suggest using Executors for this instead of Threads. Here's an example - http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html

why rawQuery in AsyncTask is much slower..?

Here is the case:
In my app, I have service which is used for communication with other special device. In the middle of this communictation I need to query database in my app to get data neccesary to proceed communicating with 2nd device. Here I am using rawQuery with 2 parameters. Query is a not too complicated cause I am actually quering view in database. As a result I should get back around 50 rows and 25 columns.
The thing is that if I just call this query from service (same thread) it blocks my UI for like 6 seconds. There is animation signaling long operation which got stuck.
On the other side, if I call exactly same query in doInBackground of AsyncTask (to move it from UI thread) my animation flows without troubles but for same amount of data query now need 25 seconds til 1 minute depending on which phone I am testing.. (almost 1min take on new Motorola with Intel cpu)
I measure times and where is so much time lost (cause queries and databases are exactly the same on both phones) and all this time is wasted on first access to cursor. In my case c.moveToFirst() ..
My question is more Android oriented not SQL, cause in my app I integrate already complete, filled with data, database
Any idea, anybody?
Edit: I try different solutions for issue I have and here is interesting thing I came to: When I use another thread with Runnable which has query, time is reduced back to few seconds (exactly like everything is executed in UI thread)
Thread t = new Thread(RunnableWhichHoldQuery); //then I just start thread t.
Still not sure why AsyncTask is behaving like it does.. Now I am just curious for explanation cause I would like to understand better what is going on.. thanks to everybody with ideas..

Categories

Resources