Android to implement a custom desktop method _android

Source: Internet
Author: User

The example in this article describes how Android implements a custom desktop. Share to everyone for your reference. Specifically 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 important function, that is, it must be able to provide a mapping of all applications (Category_launcher), but as a GUI, it must be in addition to doing its part to be in line with the popular aesthetic beauty (wallpaper) Also must have the good interaction, does not have the good interactivity to be like you to a beautiful woman diligently for a long time, she but directly to ignores, that result is comparatively bad ~ ~

We need to complete the literacy of some knowledge before we know the details of launcher. Of course, we can find this knowledge in the SDK Guide uncle, I can be very responsible to tell you, if you put the SDK Guide Uncle Sanbang have learned, apk you are basically invincible state, absolute armor +10000, the minimum basic knowledge is enough, other than the creative:

1, you must have a more complete understanding of the 4 parts of apk, especially the activity, now you can simply understand that activity is an application window.

2, must understand that part of the UI content, this part of the content is more, Chinese general I see is more depressed, but if you want to design a beauty or handsome in accordance with their own aesthetic requirements must have to understand, do not need to understand completely, but at least out of the question you know which part of the check ~ ~

3, resources that part of the content can be an encyclopedia to check

4, intent that part of the content is also need to understand the more detailed, he is the channel of communication with the application, you can refer to the small prawn to write the document.

5, manifest must understand, security can see

6, graphic part of the content is to require a higher taste of the GUI design provided, although it may be mainly used in the game, but I think if you want to make a cool GUI certainly need to 2d,3d engine.

7, Appwidget can be used as an understanding, use the time to flip

You pass the prawns must be so much fodder to direct thunder down, in fact, we need to master the details of the 1 and 2, the other can be used as an encyclopedia, but if you can carefully see that is the best.

Well, it's all about the East wind, so let's start by looking at when the home is started by WHO. This part of the knowledge can be skipped, but understanding is good, you can understand how a apk process was born after October. Perhaps the words below are a bit jerky, so it is advisable to look at the Android anatomy and Physiology.pdf first.

Linux kernel starts with the App_main process to initialize the Android Runtime Java Runtime Environment, and Zygote is the first process of Android. All the Android apps and most of the system services are zygote fork (the only native service manager I see now is not fork by zygote). The acitivity Manager is involved in several system services that are started in the systems server that are related to our startup process.

When Systerm server starts all the services, the system enters the "system ready" state, at which point the activity manager comes in. The activity manager sees the line of code as a heavyweight service that mainly manages the jumps between the activity and the lifecycle of the process. When Activity manager discovers that the system has been started, it emits a 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);
}

Through this category type of Home Intent,activity Manager will pass:

Copy Code code as follows:
Startactivitylocked (NULL, intent, NULL, NULL, 0, ainfo, NULL, NULL, 0, 0, 0, false, false);

Start the home process. And this process of starting the home process is actually going to go through the zygote to fork a child process. So as long as you have such a intent-filter in manifest, you can start as a home when you boot:

<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>

Multiple home switch will have a choice at the beginning, as if this option was implemented by Package Manager, and has not been carefully studied.

Well, knowing how lancher is done, let's take a look at the entire lancher internal structure. Look at a lancher how to construct is a long to be worthy of the audience's baby:

1. Get all the installed applications in the system, and provide the mapping to run these programs (image understanding is a small icon of an application). This is the skeleton of the lancher, what is the so-called lancher is it ~ ~ if it can not provide access to the application, and then look at most is a gorgeous vase, what with rice.

2, a little better we need for this well-designed skeleton to provide some skin and a series of animation effects, is our wallpaper and a series of images, Animation,graphic and so on. If we do this part of the work, basically our home will be basically shaped.

3, to make our GUI more affinity more convenient to use, we also provide some additional features, such as now lancher implementation of the icon drag, shortcuts and so on. These are the things of the beholder, depending on your unrestrained design.

Summed up a lancher contains 3 parts: Application information collection, event handling, animation. Let's talk about an implementation of our own launcher:

1. Design

Design your interface from a pure user perspective, what kind of effect you want to achieve, and try to write in detail. In particular, how the application information appears in the way, and its operation is generally a good design highlights. We now design a simple, we need a wallpaper, and then on this wallpaper there is a bar control to display our application icon. When you select these icons, a diagram appears in the middle of the screen to indicate the functionality of the application, and then click the diagram to open the application.

2, the overall implementation of the design

For their own ideas to design the overall implementation of this lancher, if there is no content to be implemented in time to modify the design, or change a design scheme. We use a framelayout here as a container for our lancher. Then layering, the bottom layer is used to place the shortcuts that might be needed and our wallpaper, and then we put a component on the wallpaper layer to display our application information. Personally feel that framelayout is more suitable as a lancher layout, it is similar to Photoshop layer control, the above layer will cover the following layer.

3, specific functions of the specific implementation

Here specifically to the code is to design a variety of Java functional classes. For wallpaper and icons drag-and-drop move here simply to mention, more to see the Android Lancher implementation. Wallpaper typically registers a broadcastreceiver to handle all requests for changes to the background picture in the system, and the drag-and-drop movement of the icon involves Draglayer this class.

Let's focus on how to get the application information that Android has installed. Here comes our other important service, which is Package Manager, which is responsible for managing the packages that are installed. Here are some permissions that I copied directly from the Android Lancher implementation:

<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 implementation, we create our own controls, use LinearLayout to load the Imageswitcher and gallery two controls, and gallery to display the information that is available to the application. Using Imageswitcher to display an introduction to the application, click Imageswitcher to open the appropriate 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));
.....
}

Schema selected, here's how to provide information about the installed applications for this two control, first we get Package Manager:

Copy Code code as follows:
Packagemanager manager = This.getcontext (). Getpackagemanager ();

Package Manager then provides the appropriate application information through 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 the information we get:

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 the Myappinfo information and provide it to gallery:

private static arraylist<myappinfo> mapplications;
Mgallery.setadapter (New Applicationsadapter (This.getcontext (), mapplications));

Finally, the GetView () function of the overloaded arrayadapter<myappinfo> is OK, and gallery can display the picture information of our application. Finally, we can launch the application by passing the application information of the selected picture in gallery to Imageswitcher and registering a key event for Imageswithcher:

Private Onclicklistener Mimageswitcherlistener = new Onclicklistener () {public
void OnClick (View v) {
if ( Mappinfo = = null) {}
Else
v.getcontext (). StartActivity (mappinfo.intent);
}
};

So basically our lancher skeleton is done, but there is another one, which is that when we install or delete an application, our home must capture the intent and adjust the application information in the home in time. So I'm going to register a package broadcast receiver for our controls:

Private class Applicationsintentreceiver extends Broadcastreceiver {
@Override public
void OnReceive 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 finished, and the rest is to add the animation effect you need for each event, here is not said. Have not experienced Java programming before, but personally feel that the Android Java application programming is relatively simple, just because a lot of things so it seems a bit complicated, but basically it is very convenient to use, basically is inherited after the overload or implementation interface, and Android provides a more convenient way for UI programming to use XML, which can be more intuitive for your design, and facilitates your future modifications and porting.

I hope this article will help you with your Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.