This article describes the method of implementing a customized desktop on Android. Share it for your reference. The details are as follows:
Launcher is our Home, which can be simply understood as a simplified Linux GUI. As a GUI, it must first complete its most responsible function, that is, it must be able to provide mappings to all applications (CATEGORY_LAUNCHER); however, as a GUI, in addition to doing its duty well, it must also be a wallpaper that conforms to the public's aesthetics; it must also have good interactivity, without good interactivity, just like if you are attentive to a beauty for a long time, but she ignores it directly, and the result is relatively bad~~
As the saying goes, before the troops move, food and grass take the lead. Before understanding the details of the launcher, we first need to complete the literacy of some knowledge. Of course, we can find these knowledge in the SDK guide. I can tell you very responsibly that if you learn all the three tricks of the SDK guide, you will be invincible APK, with absolute armor + 10,000, at least the basic knowledge is enough, and the others are creative:
1. You must have a relatively complete understanding of the 4 components of APK, especially Activity. Now you can simply understand that Activity is an application window.
2. You must understand the content of the UI. This part is quite rich. I think English is generally depressed, but if you want to design a beautiful woman or handsome guy that meets your aesthetic requirements, you must understand it. You don’t need to fully understand it at once, but at least if there is a problem, you know where to check it~~
3. The content of Resources can be used as an encyclopedia.
4. The content of the intent also needs to be understood in detail. It is a channel for communication with the application. You can refer to the documents written by Xiao Si Daxi.
5. You must understand the manifest, and you can check it out
6. The content of the Graphic part is provided for GUI designs that require higher taste. Although it may be mainly used in games, I think if you want to make a cool GUI, it will definitely require a 2d and 3d engine.
7. AppWidget can be used as an understanding, and then read it when used.
The prawns passing by must have been directly poured by so much food. In fact, what we need to know in detail are 1 and 2. The others can be regarded as encyclopedias, but it would be best if we can see through them carefully.
Okay, everything is ready, but the east wind is missing. Let’s first look at when and who started this Home. This part of the knowledge can be skipped, but it is good to understand it. You can understand how an APK process was born after ten months of pregnancy. The vocabulary mentioned below may be a bit awkward, so it is recommended to take a look at Android Anatomy and first.
After the Linux kernel is started, the Android Runtime Java running environment will be initialized through the App_main process, and zygote is the first process of Android. All android applications and most system services are child processes produced through zygote fork (I see only the native service manager that is not produced by zygote fork). Among the several system services launched in the system server, the related to our startup process is the Activity Manager.
After the system server starts all services, the system enters the "system ready" state, and Activity Manager will appear. Activity Manager just looks at the lines of code and knows that it is a heavyweight service. It mainly manages the jumps between activities and the life cycle of the process. When the Activity Manager finds that the system has been started, it will issue an intent:
Intent intent = new Intent( mTopAction, mTopData != null ? (mTopData) : null); (mTopComponent); if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) { (Intent.CATEGORY_HOME); }
Through this intent with type home, the Activity Manager will pass:
The Home process has been started. The process of starting the Home process is actually a child process produced through zygote fork. Therefore, as long as you have such an intent-filter in the manifest, you can start as Home when you boot:
<intent-filter> <action android:name="" /> <category android:name=""/> <category android:name="" /> </intent-filter>
The switch between multiple homes will have a choice at the beginning. As for this choice, it seems that the package manager is implemented, and I have not studied it carefully.
Okay, after understanding how Lancher executes, let’s take a look at the entire internal structure of the lancher. Let’s see how a lancher is constructed to be a child who looks worthy of the audience:
1. Obtain all installed applications in the system and provide mappings that can run these programs (image understanding is a small icon for each application). This is the skeleton of Lancher. As the saying goes, what is lancher? If it cannot provide application access, no matter how beautiful it looks, it will be just a gorgeous vase, and what is the use of rice.
2. To be better, we need to provide some painted skins and a series of animation effects for this well-designed skeleton, namely our wallpaper and a series of images, animations, graphics, etc. If this part of the work is completed, our Home will basically be in shape.
3. To make our GUI more affinity and more convenient to use, we also provide some additional functions, such as the dragging of icons implemented by lancher, shortcuts, etc. These are all matters that are different from one's own opinion, and it depends on your wild design.
To sum up, a lancher contains 3 parts: application information collection, event processing, and animation. Let's talk about the implementation process of your own launcher:
1. Design
Design your interface from a purely user perspective, and try to write in detail what effect you want to achieve. In particular, how application information appears and how it operates is generally a highlight of a good design. We now design a simple one, we need a wallpaper and then have a bar control on top of this wallpaper to display our application icon. After selecting these icons, a picture will appear in the middle of the screen to show the functions of the application, and then clicking this picture will open the application.
2. Overall implementation of design
Design the overall implementation of this lancher based on your own ideas. If there is something that cannot be realized, you must modify the design in time or change the design plan. We use a FrameLayout here as our Lancher container. Then layer it, and the bottom layer is used to place the shortcuts you may need and our wallpaper, and then put a component we defined on the wallpaper layer to display our application information. I personally think FrameLayout is more suitable as a layout for lancher. It is similar to the control of the photoshop layer, and the above layer will overwrite the lower layer.
3. Specific implementation of specific functions
Here, the specific code is to design various Java function classes. For wallpaper and icon drag and drop movement, here is a brief mention. For more information, you can check the implementation of Android Lancher. The wallpaper generally registers a broadcastreceiver to handle all requests to change the background image in the system, while the icon removal movement involves the Draglayer class.
Let's focus on how to obtain Android installed application information. This involves another important service, which is the package manager, which is responsible for managing the installed packages. Here are some permissions, and I copied its permissions directly according to the implementation of android lancher:
<uses-permission android:name=".CALL_PHONE" /> <uses-permission android:name=".EXPAND_STATUS_BAR" /> <uses-permission android:name=".GET_TASKS" /> <uses-permission android:name=".READ_CONTACTS"/> <uses-permission android:name=".SET_WALLPAPER" /> <uses-permission android:name=".SET_WALLPAPER_HINTS" /> <uses-permission android:name="" /> <uses-permission android:name=".WRITE_SETTINGS" />
Let’s take a look at the specific implementation. We create our own control, use LinearLayout to load the ImageSwitcher and Gallery controls, use Gallery to display the obtained application information, use ImageSwitcher to display the application introduction, and click ImageSwitcher to open the corresponding application.
public class MyLancherSwitcher extends LinearLayout implements , ,{ ………… mImageSwitcher = new ImageSwitcher(context) ; mGallery = new Gallery(context) ; (mImageSwitcher, new (LayoutParams.WRAP_CONTENT,400)) ; (mGallery, new (LayoutParams.FILL_PARENT, 80)) ; ………… }
After selecting the architecture, the following is how to provide the installed application information for these two controls. First, we obtain the package manager:
Then the package manager provides the corresponding application information through intent information:
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); (Intent.CATEGORY_LAUNCHER); final List<ResolveInfo> apps = (mainIntent, 0); (apps, new (manager));
Then we define our own class MyAppInfo to store the obtained information:
for (int i = 0; i < count; i++) { MyAppInfo application = new MyAppInfo(); ResolveInfo info = (i); = (manager); (new ComponentName( , ), Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); = (manager); (application); } final void setActivity(ComponentName className, int launchFlags) { intent = new Intent(Intent.ACTION_MAIN); (Intent.CATEGORY_LAUNCHER); (className); (launchFlags); }
We use an array to store this MyAppInfo information and provide it to Gallery:
private static ArrayList<MyAppInfo> mApplications; (new ApplicationsAdapter((), mApplications)) ;
Finally, overload the getView() function of ArrayAdapter<MyAppInfo> and it will be OK to cut some drawings, and Gallery can display the picture information of our application. Finally, we pass the application information of the selected image in the Gallery to ImageSwitcher, and register a key event for ImageSwithcher to start the application:
private OnClickListener mImageSwitcherListener = new OnClickListener(){ public void onClick(View v){ if(mAppInfo == null){} else ().startActivity(); } };
In this way, our lancher skeleton is basically done, but there is another one, that is, when we install or delete an application, our Home must capture this intent and adjust the application information in the home in time. Therefore, I want to register a package broadcast receiver for our control:
private class ApplicationsIntentReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { loadApplications(false); } } private void registerIntentReceivers() { filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); (Intent.ACTION_PACKAGE_REMOVED); (Intent.ACTION_PACKAGE_CHANGED); ("package"); registerReceiver(mApplicationsReceiver, filter); }
Ok so that our lancher will be basically completed, and the rest is to add the animation effects you need for each event, so I won’t talk about it here. I haven't experienced Java programming before, but I personally think that programming of Android Java applications is relatively simple, but it seems a bit complicated because there are a lot of things, but it is basically very convenient to use. Basically, it is inherited and overloaded or implemented interfaces. Android provides a more convenient way for programming Ui, which is to use XML. Using XML can make your design more intuitively, and it also facilitates your future modification and porting.
I hope this article will be helpful to everyone's Android programming design.