【NvRAM】apk中中读写nvram

nvram中LID对应的结构体如:

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);

}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值