Complete Context parsing and Context Parsing

Source: Internet
Author: User

Complete Context parsing and Context Parsing

1. Context type
We know that Android applications useJavaFor programming, you can think about the biggest difference between an Android program and a Java program? What is the division of boundaries? In fact, it is easy to analyze. Unlike Java programs, the Android program creates a class and writes a main () method to run it. Instead, it must have a complete Android engineering environment, in this environment, we have system components such as Activity, Service, BroadcastReceiver, and these components can not be used to create instances in a new way like a common Java object, instead, we need to have their own Context, that is, the Context discussed here. In this case, Context is a core function class that maintains that all components in the Android program can work normally. Let's take a look at the inheritance structure of Context:

 

The inheritance structure of Context is a little complicated. As you can see, there are two directly affiliated sub-classes: ContextWrapper and ContextImpl. From the name, we can see that ContextWrapper is the encapsulation class of context functions, while ContextImpl is the implementation class of context functions. ContextWrapper has three direct subclasses: ContextThemeWrapper, Service, and Application. ContextThemeWrapper is an encapsulation class with themes, and its direct subclass is Activity.



Here we can see at least a few familiar faces, such as Activity, Service, and Application. As a result, we can conclude that there are three types of Context: Application, Activity, and Service. Although these three classes play different roles, they all belong to one type of Context, and their specific Context functions are implemented by the ContextImpl class.



So what functions can Context implement? This is really too much, pop up Toast, start Activity, start Service, send broadcast, Operation DatabaseAnd so on. Because the specific capabilities of Context are implemented by the ContextImpl class, in most scenarios, the three types of Context, Activity, Service, and Application, can be universal. However, there are several special scenarios, such as starting the Activity and displaying the Dialog. For security reasons, Android does not allow the appearance of Activity or Dialog. the startup of an Activity must be based on another Activity, that is, the returned stack. Dialog must be displayed on an Activity (unless it is a System Alert type diert). Therefore, in this scenario, we can only use the Context of the Activity type, otherwise an error will occur.

2. Number of Context

How many Context exists in an application? In fact, we can get the answer based on the Context Type above. Context has three types: Application, Activity, and Service. Therefore, the formula for calculating the number of Context in an Application can be written as follows:

 

Context COUNT = Activity count + Service count + 1

 

The above 1 represents the number of applications, because one Application can have multiple activities and multiple services, but only one Application.

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.