Android:两种通过反射机制获取方法的使用实例

该文章主要应对Android framework层的hide方法在AS独立开发的应用中使用说明。

SystemProperties的调用

在系统中的调用

import android.os.SystemProperties;
public static String getSystemProperties() {
         String value = SystemProperties.get("vendor.text");
         return value;
}

在AS中的第三方应用的使用

import java.lang.reflect.Method;

    private String getSystemProperty(String key, String defaultValue) {
        String value = null;
        try {
            Method getMethod = Class.forName("android.os.SystemProperties").getMethod("get", new Class[]{String.class, String.class});
            value = (String) getMethod.invoke(null, new Object[]{key, defaultValue});
        } catch (Exception e) {
            Log.e(TAG,e.toString());
        }
        if (value == null) return defaultValue;
        else return value;
    }

PersistentDataBlockManager的调用

在系统中的使用

 import android.service.persistentdata.PersistentDataBlockManager;
 
    public void setMDMReducedRecoveryEnabled(boolean enabled) {
        PersistentDataBlockManager manager =
                (PersistentDataBlockManager)mContext.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
        manager.setMdmReducedRecoveryEnabled(enabled);
    }

在AS中的第三方应用的使用

package com.sndemo.refection;

import android.annotation.SuppressLint;
import android.os.RemoteException;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RefectionUtils {

    private static final String TAG = RefectionUtils.class.getSimpleName();

    @SuppressLint("WrongConstant")
    public void setMdmBootloaderEnabledLocked() {
        Log.d(TAG, "setMdmBootloaderEnabledLocked");
        int mode = 0;
        try{
            Class <?> ServiceManager = Class.forName("android.os.ServiceManager");
            Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
            Object oRemoteService = getService.invoke(null, "persistent_data_block");
            if (oRemoteService == null){
                Log.d(TAG, "oRemoteService == null ");
                return ;
            }

            Class <?> cStub = Class.forName("android.service.persistentdata.IPersistentDataBlockService$Stub");
            Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
            Object mPersistentDataBlockService = asInterface.invoke(null, oRemoteService);

            if (mPersistentDataBlockService == null){
                Log.d(TAG, "mPersistentDataBlockService == null ");
            } else {
                Method setMdmBootloaderEnabled = mPersistentDataBlockService.getClass().getMethod("setMdmBootloaderEnabled",Boolean.class);
                setMdmBootloaderEnabled.invoke(mPersistentDataBlockService,true);
            }

        } catch (ClassNotFoundException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - ClassNotFoundException!");
        } catch (InvocationTargetException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - InvocationTargetException!");
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - NoSuchMethodException!");
            throw new RemoteException();
        } catch (IllegalAccessException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - IllegalAccessException!");
        }  finally {
            return;
        }
    };

    public int getMdmBootloaderEnabledLocked() {
        Log.d(TAG, "getMdmBootloaderEnabledLocked");
        int mode = 0;
        try{
            Class <?> ServiceManager = Class.forName("android.os.ServiceManager");
            Method getService = ServiceManager.getMethod("getService", java.lang.String.class);
            Object oRemoteService = getService.invoke(null, "persistent_data_block");

            Class <?> cStub = Class.forName("android.service.persistentdata.IPersistentDataBlockService$Stub");
            Method asInterface = cStub.getMethod("asInterface", android.os.IBinder.class);
            Object mPersistentDataBlockService = asInterface.invoke(null, oRemoteService);
            Method getMdmBootloaderEnabledLocked = mPersistentDataBlockService.getClass().getMethod("getMdmBootloaderEnabledLocked");
            mode = (int) getMdmBootloaderEnabledLocked.invoke(mPersistentDataBlockService);
        } catch (ClassNotFoundException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - ClassNotFoundException!");
        } catch (InvocationTargetException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - InvocationTargetException!");
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - NoSuchMethodException!");
            throw new RemoteException();
        } catch (IllegalAccessException e) {
            Log.e(TAG, "getMdmBootloaderEnabledLocked - IllegalAccessException!");
        }  finally {
            Log.d(TAG, "getMdmBootloaderEnabled: mode = " + mode);
            return mode;
        }
    };
}

以PersistentDataBlockManager为例,是它连声明都是hide的

      /** @hide */
      public PersistentDataBlockManager(IPersistentDataBlockService service) {
          sService = service;
      }

     /**
       * Use with {@link #getSystemService(String)} to retrieve a {@link
       * android.service.persistentdata.PersistentDataBlockManager} instance
       * for interacting with a storage device that lives across factory resets.
       *
       * @see #getSystemService(String)
       * @see android.service.persistentdata.PersistentDataBlockManager
       * @hide
       */
      @SystemApi
      public static final String PERSISTENT_DATA_BLOCK_SERVICE = "persistent_data_block";

所以最终调用的是IPersistentDataBlockService类的对象,获取该接口的方法,从而实现get/set的。

IPersistentDataBlockService pdb = IPersistentDataBlockService.Stub.asInterface(                   ServiceManager.getService(Context.PERSISTENT_DATA_BLOCK_SERVICE));

第二种方法花了点时间,也发现了PersistentDataBlockManager和常用的UsbManager的差别,就稍微记录了一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值