Application objects that are readily available

Source: Internet
Author: User

In the development of Android, application and context objects should be the most contact objects, especially the context object.

When we are in an activity or service, because they are sub-classes of the context themselves, "this" can be equivalent to the context object's use. However, in many cases this context is not so readily available, consider the following:

    • Need to use the context of the SDK, developers in order to use the SDK need to add context of the delivery code;
    • Two-pack add-in code if you need to use the context, you need to modify the original logic, the context passed in;
    • After being injected into the Java environment, we need to use the context to access various IPC services;
    • Wait a minute

So if you can find a way to get to the application object of the current process without touching the original logic, the above mentioned problem can be solved. This is a bit of an abstraction, for example, when we write a custom application class, this usually adds a static method GetContext (or something like that), as follows:

final MyApplication extends Application{    privatestatic Application sInstance;    @Override    publicvoidonCreate(){        this;    }    publicstaticgetContext(){        return sInstance;    }    //...    //...}

This code everyone to understand, mainly is convenient to use the context when used, because the application is global, so it can prevent internal leakage. But how can you do it without this way, even without a custom application, you can get the Application object?

In fact, this time there is not much dry, here I provide a method, this method can be compatible with 1.6 to 5.1 (after the firmware should also be compatible).

The main way is to get the static field of the system class by reflection, the process is as follows:

    • Through the Runtimeinit class, get to Mapplicationobject static field, the type of this field is android.app.activitythread$applicationthread;
    • Through the Applicationthread class, get the This$0 field, note that this is the compiler generated, the type of this field is android.app.ActivityThread;
    • Through the Activitythread class, gets its Minitialapplication field, which is the Application object;

Because of the reflection call, the complete code does not write well, write a simple pseudo-code

Applicatioin app = RuntimeInit.mApplicationObject.this$0.mInitialApplication;

To be sure, this is not the only way that everyone has any good way to welcome to share.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Application objects that are readily available

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.