typedef struct
{
int uid;
} NVRAM_AMOI_TEST_STRUCT;
demo code:
//将int型转化成byte数组
private static byte[] getBytes(int data){
byte[] bytes = new byte[4];
bytes[0] = (byte)(data&0xff);
bytes[1] = (byte)((data&0xff00)>>8);
bytes[2] = (byte)((data&0xff0000)>>16);
bytes[3] = (byte)((data&0xff000000)>>24);
return bytes;
}
//将byte数组转换成整型
public static int byte2int(byte[] res) {
int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) | ((res[2] << 24) >>> 8) | (res[3] << 24);
return targets;
}
//向nvram中写数据
public void writeData(int file_lid) {
IBinder binder = ServiceManager.getService("NvRAMAgent");
NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);
int n = 10;
byte[] buff = new byte[]{0,0,0,0};
byte[] by = getBytes(n);
for(int i=0;i<4;i++
write_buff[i] = by[i];
try {
int flag = agent.writeFile(file_lid, write_buff);
if (flag > 0) {
System.out.println("write success");
} else {
System.out.println("write failed");
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
//从nvram中读取数据
public void readData(int file_lid) {
IBinder binder = ServiceManager.getService("NvRAMAgent");
NvRAMAgent agent = NvRAMAgent.Stub.asInterface(binder);
byte[] buff = null;
try {
buff = agent .readFile(file_lid);// read buffer from nvram
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(i=0;i<buff.length;i++){
System.out.println("i:"+buff[i]);
}
int b = byte2int(temp);
System.out.println("b:"+b);
}