Android--------Create a context for another package from avtivity in one package

Source: Internet
Author: User

Android has a context concept that everyone knows. Context can do a lot of things, open activity, send broadcasts, open folders and databases under this package, get classloader, get resources, and so on. If we get a context object for a package, we can basically do most of the things we can do with this package.

So can we get it? I'm glad to tell you, can!
The context has a Createpackagecontext method that can create another package, which is different from its own instance of the context, but the function is the same.

This method has two parameters:
1. PackageName package name, to get the context of the package name
2. Flags flag, with Context_include_code and context_ignore_security two options. Context_include_code means including code, which means that the code inside the package can be executed. Context_ignore_security means ignoring security warnings, and if you do not add this flag, some features are not available and a security warning appears.


Here's a small example of how to execute a class within another package.
The package name of the other package is Chroya.demo, the class name is main, the method name is print, and the code is as follows:

Java code
    1. Package Chroya.demo;
    2. Import android.app.Activity;
    3. Import Android.os.Bundle;
    4. Import Android.util.Log;
    5. Class Main extends Activity {
    6. @Override
    7. Public void OnCreate (Bundle savedinstancestate) {
    8. Super.oncreate (savedinstancestate);
    9. }
    10. Public void Print (String msg) {
    11. LOG.D ("Main", "msg:" + msg);
    12. }
    13. }

This package calls the code block of Main's print method as follows:

Java code
    1. Context C = createpackagecontext ("Chroya.demo", Context.context_include_code | context.context_ignore_security);
    2. Loaded in this class
    3. Class clazz = C.getclassloader (). LoadClass ("Chroya.demo.Main");
    4. Create a new instance
    5. Object owner = clazz.newinstance ();
    6. Get the Print method, pass in the parameters and execute
    7. Object obj = Clazz.getmethod ("print", String. Class). Invoke (owner,"Hello");

OK, so we call the print method of the main class of the Chroya.demo package, execute the result, and print out hello.

How, this is just a call to other packages of code example, we get to the context, can also do a lot of things, of course, the title of the bad things, or do not do as well.


Other great article articles

Android Learning Note (38) using ProgressDialog to create a skin feed dialog box
Large Web site architecture design-SOLR
Android Learning Note (39) Message tip Use TOAST to hint information lightly
Android Learning Note (32) grid view (GridView) and graphics switcher (Imageswi ...
Android Learning Note (31) Expandable List Component (Expandablelistview)
more about Android development articles


Android--------Create a context for another package from avtivity in one package

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.