Home>

The launcher is our home, which can be simply understood as a simplified Linux gui. As a gui, it must first complete its most important functions,That is, it must be able to provide mapping for all applications (category_launcher);but as a gui, in addition to doing its job, it must be a beauty that conforms to the public aesthetics;it must also have good interactivityWithout good interactivity is like you are attentive to a beautiful woman for a long time,She ignored it directly,That turned out to be terrible ~~

The so-called soldiers and horses have not moved,Forage first,Before getting into the details of the launcher,We first need to complete the literacy of some knowledge.Of course, we can find all this knowledge in Uncle SDK Guide,I can tell everyone responsibly,If you've learned all about Uncle sdk guide's triple axe,apk you are basically invincible,Absolute armor + 10000, at least the basic knowledge is enough,The other is creativity:

1. You must have a complete understanding of the four components of the apk, especially the activity. Now you can simply understand that the activity is the window of an application.

2. Must understand the content of ui,This part is more content,I generally think it ’s more depressing,But if i want to design a beauty or handsome man that meets your aesthetic requirements, you must know it.No need to fully understand,But at least something went wrong and you know where to look ~~

3. Resources can be checked as an encyclopedia

4. The content of the intent also needs to be understood in more detail.He is the channel to communicate with the application,You can refer to the document written by Prawn.

5, manifest must understand,look at security

6. The content of the graphic part is provided for gui designs that need higher taste.Although it may be used primarily for games,But I think if i want to make cool enough gui, you definitely need 2d, 3d engine.

7, appwidget can be used as an understanding,Read it when you use it

The prawns passing by must have been directly thundered by so many grains.In fact, what we need to master in detail is 1 and 2. The others can be used as encyclopedias.But it is best if you can see through it carefully.

Okay, it ’s only owed to everything.Let's first look at who started this home.This part of knowledge can be skipped,But understanding is good,You can learn how an apk process was born after October pregnancy.Maybe the words mentioned below are a bit jerky,So it is recommended to look at android anatomy and physiology.pdf first.

After the Linux kernel starts, it will initialize the android runtime java operating environment through the app_main process.And zygote is the first process of android.All android applications and most system services are child processes forkd out through zygote (I only see the native service manager is not forkted out by zygote). Among the several system services started in the system server, the one related to our startup process is the acidity manager.

After systerm server starts all services,The system enters the "system ready" state, at which point the activity manager appears.The activity manager knows that it is a heavyweight service by looking at the lines of code.It mainly manages 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?uri.parse (mtopdata):null);
intent.setcomponent (mtopcomponent);
if (mfactorytest!=systemserver.factory_test_low_level) {
intent.addcategory (intent.category_home);
}

With this category type home intent, the activity manager will pass:

startactivitylocked (null, intent, null, null, 0, ainfo, null, null, 0, 0, 0, false, false);

Started the home process. The process of starting the home process is actually a child process forkd by zygote.Therefore, as long as there is such an intent-filter in the manifest, it can be started as home when it is turned on:

<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.home" />
<category android:name="android.intent.category.default" />
</intent-filter>

The switch between multiple homes will have a choice at the beginning,As for this choice seems to be implemented by the package manager,Haven't studied it carefully.

Alright, understand how Lancher performs,Let's take a look at the internal structure of the entire lancher.Take a look at how a lancher is structured to be a baby who is worthy of the audience:

1. Get all installed applications in the system,And provide a map that can run these programs (the understanding of the image is a small icon of each application). This is the skeleton of the Lancher. What is the so-called Lancher?If it cannot provide application access,No matter how good-looking it is, at most it is a gorgeous vase,Why use rice?

2. Better, we need to provide some well-painted skins and a series of animation effects for this well-designed skeleton.Is our wallpaper and a series of images,animation, graphic and the like. If this part of the work is done,Basically, our home is basically formed.

3. To make our gui more affinity and easier to use,We also provide some additional features,For example, dragging icons implemented by Lancher now,Shortcuts and more.These are the things that the benevolent sees the benign and the wise,It depends on your wild design.

To sum up, a lancher contains three parts:application information collection,Event processing,Animation. Let's talk about the implementation of a launcher:

1.Design

Design your interface from a pure user perspective,What effect do i want to achieve,Be as detailed as possible.Especially how the application information appears,And its operation is generally a highlight of a good design.We now design a simple,We need a wallpaper,Then there is a bar control on this wallpaper to display our application icon.After selecting these icons, a picture will appear in the middle of the screen to indicate the function of this application,Then clicking on the figure will open the application.

2. The overall realization of the design

Design the overall implementation of this lancher for your own ideas,If there is something that can't be achieved, modify the design in time.Or another design solution.We use a framelayout here as a container for our lancher. Then layered,The bottom layer is used to place the shortcuts that may be needed and our wallpaper, and then put a component we define on the wallpaper layer to display our application information.Personally, I think the framelayout is more suitable as a layout for the Lancher. It is similar to the control of the layer in Photoshop.The upper layer covers the lower layer.

3. The specific implementation of specific functions

Specific to the code here is to design various Java functional classes.For dragging and moving wallpapers and icons here,More can see the implementation of Android Lancher. Wallpaper is generally a broadcastreceiver registered to handle all requests to change the background picture in the system,The drag and drop of the icon involves the draglayer class.

Let's focus on how to get information about android installed applications.This involves our other important service, which is the package manager, which is responsible for managing the installed packages.There are some permissions involved here,I copied its permissions directly according to the implementation of android lancher:

<uses-permission android:name="android.permission.call_phone" />
<uses-permission android:name="android.permission.expand_status_bar" />
<uses-permission android:name="android.permission.get_tasks" />
<uses-permission android:name="android.permission.read_contacts" />
<uses-permission android:name="android.permission.set_wallpaper" />
<uses-permission android:name="android.permission.set_wallpaper_hints" />
<uses-permission android:name="android.permission.vibrate" />
<uses-permission android:name="android.permission.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 the gallery to display the obtained application information,Use imageswitcher to display the introduction of the application,Click imageswitcher to open the corresponding application.

public class mylancherswitcher extends linearlayout implements viewswitcher.viewfactory, adapterview.onitemselectedlistener, adapterview.onitemclicklistener {
…………
mimageswitcher=new imageswitcher (context);
mgallery=new gallery (context);
this.addview (mimageswitcher, new linearlayout.layoutparams (layoutparams.wrap_content, 400));
this.addview (mgallery, new linearlayout.layoutparams (layoutparams.fill_parent, 80));
…………
}

The architecture is selected.Here's how to provide information about installed applications for these two controls,First we get the package manager:

packagemanager manager=this.getcontext (). getpackagemanager ();

The package manager then provides the corresponding application information through the intent information:

intent mainintent=new intent (intent.action_main, null);
mainintent.addcategory (intent.category_launcher);
final list<resolveinfo>apps=manager.queryintentactivities (mainintent, 0);
collections.sort (apps, new resolveinfo.displaynamecomparator (manager));

Then we define our own class myappinfo to store this obtained information:

for (int i=0;i<count;i ++) {
myappinfo application=new myappinfo ();
resolveinfo info=apps.get (i);
application.title=info.loadlabel (manager);
application.setactivity (new componentname (
info.activityinfo.applicationinfo.packagename,info.activityinfo.name),intent.flag_activity_new_task
| intent.flag_activity_reset_task_if_needed);
application.icon=info.activityinfo.loadicon (manager);
mapplications.add (application);
}
final void setactivity (componentname classname, int launchflags) {
intent=new intent (intent.action_main);
intent.addcategory (intent.category_launcher);
intent.setcomponent (classname);
intent.setflags (launchflags);
}

We use an array to store this myappinfo information and provide it to the gallery:

private static arraylist<myappinfo>mapplications;
mgallery.setadapter (new applicationsadapter (this.getcontext (), mapplications));

Finally, overload the arrayadapter<myappinfo>getview () function to make some cuts on the drawing, and the gallery can display the picture information of our application.Finally, we pass the application information of the selected picture in the gallery to the imageswitcher, and register a key event for the imageswithcher,You can start the application:

private onclicklistener mimageswitcherlistener=new onclicklistener () {
public void onclick (view v) {
if (mappinfo == null) {}
else
v.getcontext (). startactivity (mappinfo.intent);
}
};

In this way, the skeleton of our Lancher is basically done.But there is one more,That is when we newly install or remove an application,Our home must capture this intent and adjust the application information in the home in time.So I want to register a package's 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);
filter.addaction (intent.action_package_removed);
filter.addaction (intent.action_package_changed);
filter.adddatascheme ("package");
registerreceiver (mapplicationsreceiver, filter);
}

ok so our lancher is basically completed,The rest is to add the animation effect you need for each event,Not to say here.I have not experienced java programming before, but I personally think that the programming of android java application is still relatively simple.It just seems a bit complicated because there are so many things,But basically it is very convenient to use,Basically, you can overload or implement interfaces after inheritance.And android provides a more convenient way for ui programming is to use xml, using xml can make your design more intuitiveIt also facilitates your future modification and porting

  • Previous Android implementation method to add headset status icon
  • Next php survey results statistics