Launcher development

What is Launcher?
Launcher may be a bit unfamiliar to junior developers. It is commonly known as "HomeScreen", which is the first App we see after turning on the device. The launcher is actually an app, which is used to display and manage other apps on the phone. At present, there are many third-party launcher applications on the market, such as "Xiaomi Launcher", "91 Launcher" and so on. Today I will talk about how to develop your own launcher application.

Create a new Launcher project
First, we create a new project in Android studio (I won't repeat it here). To use our application as a Launcher, we need to add in AndroidManifest.xml:

Set the Activity attribute
At this point our AndroidManifest.xml should look like this:

AndroidManifest.xml
may feel the same as usual when we run the program, but when we click the Home button, we will see the prompt to select the desktop:

Select the desktop
and you can see that the "Mylauncher" we developed appears together with the Google Now Launcher that comes with Android. Click on our own App and select always, so that our application becomes the Home of the system. Maybe some readers will complain, this is not Home at all, where are all our Apps?

Where did the Apps go
? Don't worry, let's learn what PackageManager is before discussing how to display your Apps.

The official definition given to us is:
Class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager().

This class is used to retrieve various related information of the application on the device, you can get it through getPackageManager().

In layman's terms, PackageManager is used to obtain application information in the Android system, query Application-related information (application, activity, receiver, service, provider and corresponding attributes, etc.), query installed applications, add or delete permissions, and clear user data , cache, snippets, etc. Since we need to display all our Apps, it is natural that its help is indispensable.

We can call the queryIntentActivities() method of PackageManager to obtain a collection of App information (ResolveInfo). This ResolveInfo object contains information such as the program name, package name, and entry class name of the application. The code is shown in the figure:
Get the ResolveInfo collection
when we get the ResolveInfo collection, we can do whatever we want (fog). Here I use a GridView to display all the App information. The sample code is as follows:
Display the icon and name of the App
Display the icon and name of the App.
Can it be displayed or not? Not to mention that we still need to be able to click to jump (´・◡・`), and add a click event to the item of the GridView:
click to jump
after these few steps, our launcher can display all apps and click to jump. But readers may complain again, are you ashamed to say that you are a desktop without a wallpaper? It doesn't matter, our background will come soon~

Setting the desktop background
First, we need to display the background first, and add the following code under the res/valuses/styles.xml file:
set style
Then use this Theme in AndroidManifest.xml:
when we run the program again, we can display our The desktop is finished, and a simple Launcher application is developed.
operation result

Guess you like

Origin blog.csdn.net/hongranzuoxiang/article/details/110184938