该文章主要应对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的差别,就稍微记录了一下。