清除发送/接收的数据包

时间:2012-11-12 08:16:34

标签: blackberry

是否可以清除发送/接收的数据包并再次从0开始?

获取发送或接收数据包的代码:

long  no_of_packet_Sent = RadioInfo.getNumberOfPacketsSent();

long  no_of_packet_Received = RadioInfo.getNumberOfPacketsReceived();

1 个答案:

答案 0 :(得分:1)

我从来没有找到答案,但另一个选择是将数据写入文本文件,然后从“获取数据包数”中减去文本文件中的数据。

private static String fileFormatString(String filename) {
    return filename.replace(" ".charAt(0),"_".charAt(0));
}
public static String readTextFile(String fName) {
    fName = fileFormatString(fName);
    String result = null;
    FileConnection fconn = null;
    DataInputStream is = null;
    try {
        fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
        is = fconn.openDataInputStream();
        byte[] data = IOUtilities.streamToBytes(is);
        result = new String(data);
    } catch (IOException e) {
        System.out.println("Error on read: "+fName+" - " + e.getMessage());
    } finally {
        try {
            if (null != is) is.close();
            if (null != fconn) fconn.close();
        } catch (IOException e) {
            System.out.println("Error on read IO: "+fName+" - " + e.getMessage());
        }
    }
    return result;
}
public static void writeTextFile(String fName, String text) {
    fName = fileFormatString(fName);
    DataOutputStream os = null;
    FileConnection fconn = null;
    try {
        fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
        if (fconn.exists());
        if (!fconn.exists()) fconn.create();
        os = fconn.openDataOutputStream();
        os.write(text.getBytes());
    } catch (IOException e) {
        System.out.println("Error on write: "+fName+" - " + e.getMessage());
    } finally {
        try {
            if (null != os) os.close();
            if (null != fconn) fconn.close();
        } catch (IOException e) {
            System.out.println("Error on write IO: "+fName+" - " + e.getMessage());
        }
    }
}
long no_of_packet = RadioInfo.getNumberOfPacketsSent()+RadioInfo.getNumberOfPacketsReceived();
DTHelper.writeTextFile(text_file_name,""+no_of_packet );

String readnumberofkbytes=readTextFile(text_file_name);
long Longreadnumberofbytes = Long.parseLong(readnumberofbytes);

long CurrentNumberofDataUsed= no_of_packet -Longreadnumberofbytes;