zoukankan      html  css  js  c++  java
  • SystemProperties反射调用

    Systemproperties类在android.os下,但这个类是隐藏的,上层程序开发无法直接使用,只能通过java反射来调用

    SystemPropertiesInvoke.java

    package com.as;
    
    import java.lang.reflect.Method;
    
    import android.util.Log;
    
    public class SystemPropertiesInvoke {
        private static final String TAG = "SystemPropertiesInvoke";
        private static Method getLongMethod = null;
        private static Method getBooleanMethod = null;
           
        public static long getLong(final String key, final long def) {
            try {
                if (getLongMethod == null) {
                    getLongMethod = Class.forName("android.os.SystemProperties")
                            .getMethod("getLong", String.class, long.class);
                }
                
                return ((Long) getLongMethod.invoke(null, key, def)).longValue();
            } catch (Exception e) {
                Log.e(TAG, "Platform error: " + e.toString());
                return def;
            }
        }
        
        public static boolean getBoolean(final String key, final boolean def) {
            try {
                if (getBooleanMethod == null) {
                    getBooleanMethod = Class.forName("android.os.SystemProperties")
                            .getMethod("getBoolean", String.class,boolean.class);
                }
              
               //Log.i(TAG,"getBoolean:"+"key:"+key+" def:"+def); 
               //Log.i(TAG,"getBoolean:"+getBooleanMethod.invoke(null, key, def)); 
                
                return (Boolean)getBooleanMethod.invoke(null, key, def);
            } catch (Exception e) {
                Log.e(TAG, "Platform error: " + e.toString());
                return def;
            }
        }
    }
  • 相关阅读:
    time fly
    小论文初稿终于完成
    leetcode之Length of Last Word
    static关键字
    参数传递
    this关键字
    面向对象有三大特征
    空指针异常
    变量按数据类型分为
    构造方法
  • 原文地址:https://www.cnblogs.com/lijunamneg/p/4498087.html
Copyright © 2011-2022 走看看