Android Application Module: application, task, process, and thread

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) that 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.

 

Make sure that you have read the launchMode attribute and Intent flags and follow the details of these options.

 

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.

You can run multiple. APK packages in the same process to reduce system overhead.

Helps the system manage resources and put 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.

 

Please refer to the security documentation for more information.

Related Article

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.