Use of Android loader and acquisition method of mobile phone address book

The main thread of Android performs long-time operations, resulting in no response on the interface, which will cause ANR. If you need to perform operations for a long time, it will generally be processed in another thread, and then the data will be transferred to the main thread for display. Android itself provides us with some mechanisms to deal with this situation. Let's take a look at loader today. Loader is mainly used to load data asynchronously in activity and fragment. It is also very simple to use.

The initialization of Loader is very simple. Activity provides the interface to get LoaderManager and then invocation of LoaderManager's initLoader. Initloader accepts three parameters:

The unique identifier ID of the loader, which is used to distinguish multiple loaders;

Parameters passed to loader, optional;

Callback for loader.

To destroy the loader, you only need to use the destroryloader of the loadermanager. The parameter only passes the ID of one loader.

The loader callback interface loadercallbacks has three methods:

Oncreateloader is called when the loader is created;

Onloadfinished is called when the loader finishes loading data;

Onloaderreset is called when the loader is reset.

This is the loader interface. We use a practical application case to see how to use loader. Here, we select the acquisition of mobile phone address book. To obtain mobile phone address book information, we need to use the phone's ContentProvider. See the following example in detail.

To access the address book, you need to add permissions to the manifest file:

activity_ Main.xml is very simple, including a listview to display the address book list.

The code of mainactivity class is relatively long. Let's decompose it. Mainactivity needs to implement the loadercallbacks interface

Using kotlin's partner object, the static member variable, loader, is defined_ ID is the ID of the loader, phone_ Project is the set of columns you want to find when querying the address book

OnCreate uses SimpleCursorAdapter as the Adapter of ListView, and then calls initLoader to initialize Loader.

Ondestory destroy loader

Three callback methods:

Oncreateloader creates a cursorloader, which executes the operation of ContentProvider and then returns a cursor;

When onloaderreset, set cursor in the adapter to null;

When onloadfinished, the cursorloader will return a new cursor after querying the data. We use the new cursor to replace the cursor in the previous adapter.

In this way, we can automatically and asynchronously load data when the activity is started. Try it on the mobile phone and you can find that the loading is very smooth. GitHub has uploaded the code.

The above article on the use of Android loader and the acquisition method of mobile phone address book is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>