Using the Java reflection mechanism, use the internal class member functions of the android System

Source: Internet
Author: User
// Access an attribute
            Context mmsCtx = createPackageContext("com.android.lockscreen",          Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);  Class<?> maClass = Class.forName("com.android.lockscreen.LockScreenActivity", true, mmsCtx.getClassLoader());  Object maObject = maClass.newInstance();Field f_mPm = maClass.getDeclaredField("TAG");  f_mPm.setAccessible(true);              Log.i(LOG_TAG, "___________________________________________obj:"+f_mPm.get(maObject));

  

// Access method

// Access try {context mmsctx = createpackagecontext ("com. Android. lockscreen", context. context_include_code | context. context_ignore_security); Class <?> Maclass = Class. forname ("com. android. lockscreen. lockscreenactivity ", true, mmsctx. getclassloader (); object maobject = maclass. newinstance (); field f_mpm = maclass. getdeclaredfield ("tag"); // maclass. getdeclaredmethod ("", parametertypes); f_mpm.setaccessible (true); log. I (log_tag, "_______________________________________________ OBJ:" + f_mpm.get (maobject); Class [] Resource = new class [] {context. class}; object [] resourceobj = new object [] {mmsctx}; Method addsmethod = maclass. getdeclaredmethod ("getlockscreenview", resource); addsmethod. setaccessible (true); view addsresult = (View) addsmethod. invoke (maobject, resourceobj); log. I (log_tag, "_______________________________________________ addsresult:" + addsresult);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} // defines the public class lockscreenactivity extends activity {private string tag = "lockscreenactivity"; // Private lockscreen mscreen = new lockscreen (lockscreenactivity. this); Private lockscreen getlockscreenview (context) {log. I (TAG, "_____________________________________________ context:" + context); return New lockscreen (context );}}

  

// Obtain the setting package's context mmsctx = createpackagecontext ("com. android. settings ", context. context_include_code | context. context_ignore_security); // use setting's classloader to load COM. android. settings. manageapplications class <?> Maclass = Class. forname ("com. android. settings. manageapplications ", true, mmsctx. getclassloader (); // creates an object named maobject = maclass. newinstance ();/** assign values to the private domain MPM. Because MPM is used in invokegetsize of sizeobserver, * Initialization is required because oncreate is not executed and not initialized. */Field f_mpm = maclass. getdeclaredfield ("MPM"); f_mpm.setaccessible (true); f_mpm.set (maobject, mmsctx. getpackagemanager ());

  

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.