What the context is, how it's used

Source: Internet
Author: User

I. What is the context?

Official documentation Overview: An interface for global information about the application environment. This is an abstract class, and its implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as application-level actions such as initiating activities, calling, broadcasting and receiving intentions, and so on. I feel that I can liken the context to an amusement park, you have to play the roller coaster, the pirate ship will play in the amusement park.

Inheritance relationship

Two. Contextwrapper and Conteximpl

Contextwrapper provides a range of methods. This inherits from its application,service,activitykey directly from it to obtain the corresponding method. But its method is empty shell, the concrete function is realized by Contextimpl.

Three. application,service,activity

The object of these three is a context. So when it comes to data manipulation in mainactivity, this is the time to write

Editor editor = getSharedPreferences("Alarm", MODE_PRIVATE).edit();

But in a subclass that inherits from LinearLayout, you need to write this

Editor editor = getContext().getSharedPreferences("Alarm", Context.MODE_PRIVATE).edit();

Therefore, in the mainactivity, sometimes you need to pass in the context of the object, you can see the direct use of mainactivity.this combined with the alarm clock that project, when the alarm is added

Private void Addalarm () {// Add a sentence output log.d ("Context", GetContext () + "");

Will find the context here is mainactivity

Four. Context capabilities are different

The document mentions that the context can be used to do a number of operations, but different context capabilities range, amusement park also has a size difference.
The general operation that Conext can do depends on where the context originates from first. The following table lists what is common in the application that will receive the context object, and for each of the corresponding cases, where it can be used:

    • NO1 says application context can actually start an activity, but it needs to create a new task. This may meet some specific requirements, but in your app you will create a non-standard fallback stack (back stack), which is usually not recommended or good practice.
    • NO2 says this is illegal, but this fill (inflation) can be done, but it is using the default theme (theme) of the system you are running, not the theme that your app defines.
    • NO3 above Android4.2, if receiver is null (this is used to get the current value of a sticky broadcast), this is allowed.

This can be combined with an Android alarm clock project

 public  class  alarmreceiver extends   Broadcastreceiver {@Override   void   OnReceive (context context, Intent Intent) { //  TODO auto-generated m        Ethod stub   ...        LOG.D ( Context, GetContext () + ""  //  start alarm interface  Intent i = new  Intent (context, Playmusic.         Class  );        I.addflags (Intent.flag_activity_new_task);    Context.startactivity (i); }}

You can see that the context here is receiverrestrictedcontext, so you need to create a new task

Read MORE:
    1. http://blog.csdn.net/cswhale/article/details/38958595
    2. http://blog.csdn.net/race604/article/details/9331807

What the context is, how it's used

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.