Android Application Module details

Source: Internet
Author: User

In most operating systems, an independent one-to-one executable file (such as an EXE file in Windows) exists, which can generate processes, it can also interact with interface icons and applications. But in Android, This is not fixed. It is very important to understand how to combine these scattered parts.

Because Android is flexible and flexible, you need to understand some basic technologies when implementing different parts of an application:

An android package (. APK) contains the application code and resources. This is an application release where users can download and install files on their devices.

A task can usually be started as an "application". Generally, there is an icon on the desktop to start the task. This is an upper-layer application, you can switch your task to the foreground.

A process is a core process at the underlying code running level. All codes in the common .apk package run in one process, and each process is opposite to a .apk package. However, process labels are often used to change the code running location. They can be a full. APK package or independent activities, receivers, services, or provisioner components.

Task

Remember the key point: when a user sees an "application", no matter how it is actually handled, it is a task. If you create a .apk package through a few activities, one of them must be the upper-level portal (a separate task is created through the intent-filter feature package of the action. No matter which activity you start, it will be part of the task.

A task is an application from the user's point of view. for developers, a task runs through one or more views of an active task or an activity stack. When the intent. flag_activity_new_task flag is set to start an activity intent, the task is created. This intent is used as the root purpose of the task and defines which task to distinguish. If this flag is not enabled when the activity starts, it will be run in the same task (unless your activity is started in special mode, which will be discussed later ). If you use flag_activity_new_task to mark and the intent task has been started, the task will be switched to the foreground rather than reloaded.

Flag_activity_new_task must be used with caution: in the user's opinion, a new application is started from this. If this is not what you expect, you want to create a new task. In addition, if you need to exit from the desktop to the original place and start a new task with the same intent, you need to use the new task tag. Otherwise, if you press the Home Key in the task you just started instead of the back key, your task and task activity will be placed behind the desktop program, there is no way to switch over.

Affinities)

In some cases, Android needs to know which task activity is attached to a special task, even if the task has not been started. This is accomplished through task affinity, which provides a unique static name for one or more activities that may be running in the task. The name of the activity .apk package is the name of the task affinity. All the activities in the .apk package are part of a single application.

When a new activity is started without the intent. flag_activity_new_task flag, the task affinity does not affect the newly started activity: it will always run in the task it starts. However, if new_task is used, affinity checks whether existing tasks have the same affinity. If yes, the task will be switched to the foreground, and new activities will be started at the top of the task.

You can set your own task affinity for all the activities in the .apk package in the app tag in your presentation file. Of course, you can also set tags for individual activities. Here are some examples to demonstrate how to use:

If your .apk package contains upper-layer applications that can be started by multiple users, you may want to assign different affinity for each activity. There is an incorrect association here. You can add fake numbers to the strings behind the. APK package name. For example, the affinity name for "com. Android. Contacts" can be "com. Android. Contacts: dialer" and "com. Android. Contacts: contactslist ".

If you want to replace a notification, shortcut key, or other internal activities of an application that can be started from outside, you need to explicitly set the task affinity (taskaffinity) in the activity you want to replace ). For example, if you want to replace the contact details browsing interface (you can call the interface directly or by using a shortcut), you need to set the task affinity (taskaffinity) to "com. Android. Contacts ".

Start mode and start mark

The most important way to control the communication between activities and tasks is to set the Startup Mode attributes and mark the intention accordingly. These two parameters can be combined to jointly control the startup result of the activity, which is described in the relevant document. Here we only describe some common usage and several different combinations.

The most common mode you use is singletop (except the standard mode by default ). This does not affect the task; it only prevents the same activity from being started multiple times at the top of the stack.

The singletask mode has some impact on the task: it can make the activity always open in the new task (or switch the opened task to the foreground ). To use this mode, you need to be more careful about how the process interacts with other parts of the system, which may affect all activities. This mode is best used in the tag of the app entry activity. (Supports the main activity and launcher category ).

Singleinstance startup mode is more special. This mode can only be used when the entire application has only one activity.

In one case, you will often encounter other entities (such as search manager searchmanager or notification manager icationicationmanager) that will start your activity. In this case, you need to use the intent. flag_activity_new_task tag because the activity is started out of the task (the application/task has not been started. As described earlier, in this case, the standard feature is that tasks that match the affinity of the current task and the new activity will switch to the foreground, and then start a new activity at the top. Of course, you can also implement other types of features.

One common practice is to use intent. flag_activity_clear_top and new_task together. In this way, if your task is already running, the task will be switched to the foreground. All the activities in the stack except the root activity will be cleared, the onnewintent (intent) method of the root activity is called after the intent parameter is passed in. When this method is used, singletop or singletask startup mode is often used, so that the current instance will be placed with a new intent, instead of destroying the original task and then starting a new instance.

In addition, you can set the Activity Task affinity to a null string (indicating no affinity), and then set the finishonbackground attribute. If you want the user to provide you with a separate notification of the activity description, it is better to return to the application task. This is more useful. To specify this attribute, the activity ends no matter whether the user uses back or home. If this attribute is not specified, pressing the Home key will cause the activity and task to remain in the system, and there is no way to return to the task.

Process

In Android, a process is the complete implementation of an application, rather than what users generally know. Their main purposes are simple:
◆ Improve stability and security, and move untrusted or unstable code to other processes.
Worker can run multiple. APK packages in the same process to reduce system overhead.
◆ Helps the system manage resources and place important code in a separate process, so that other parts of the application can be destroyed separately.

As described above, process attributes are used to control processes that run with special application components. Note that this attribute cannot violate System Security: If two .apk packages cannot share the same user ID but attempt to run in a process, this situation is not allowed, in fact, the system will create two different processes.

Thread

Each process contains one or more threads. In most cases, Android does not create redundant threads in the process. Unless it creates its own thread, we should maintain the single thread of the application. An important conclusion is that all call instances, broadcast receivers, and service instances are created by the main thread running in this process.

Note that new threads are not created for active, broadcast receivers, services, or content providers instances: components of these applications are instantiated in the process (unless otherwise stated, are processed by the same process), which is actually the main thread of the process. This means that these components (including services) do not require long-distance processes or block operations (like network calls or computing loops) when the system calls, because this will block all other components in the process. You can use standard thread classes or android handlerthread classes to perform remote operations on other threads.

Here are some exceptions for creating thread rules:

Call the interface implemented by ibinder or ibinder. If the call comes from another process, you can call it through the ibinder sent by the thread or the thread pool in the local process, the call from the main thread of the process is not allowed. In special cases, the ibinder that calls a service can handle this. (Although the call method in the service has been completed in the main thread .) This means that the implementation of the ibinder interface must have a thread-safe method so that any thread can access it at the same time.

Call the main method of the content provider sent by the calling thread or main thread and ibinder. The specified method is recorded in the class of the content provider. This means that there must be a thread-safe mode to implement these methods, so that any other thread can access it at the same time.

Call view and subclass composed of running threads in the view. Normally, this will be used as the main thread of the process. If you create a thread and display a window, the inherited window view will be started from that thread.

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.