1
在流量監測程序中,如何確定流量是由GPRS還是Wifi生成? 請給我一些想法和建議?非常感謝 。如何確定流量是由GPRS 3G或Wifi生成的?
在流量監測程序中,如何確定流量是由GPRS還是Wifi生成? 請給我一些想法和建議?非常感謝 。如何確定流量是由GPRS 3G或Wifi生成的?
您可以檢查是否無線網絡由以下
ConnectivityManager conman = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean wifi = conman.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
檢查其他網絡連接類型的連接可以通過改變TYPE_WIFI屬性來完成。
哦,我明白了! 通過閱讀/ proc/self/net/dev或/ proc/net/dev來獲取應用程序流量統計信息。
void skipline(FILE *f)
{
int ch;
do {
ch = getc(f);
} while (ch != 'n' && ch != EOF);
}
int main(int argc, char *argv[])
{
FILE *pnd;
char buffer[BUFSIZ];
char *interface;
struct ifinfo {
char name[8];
unsigned int r_bytes, r_pkt, r_err, r_drop, r_fifo, r_frame;
unsigned int r_compr, r_mcast;
unsigned int x_bytes, x_pkt, x_err, x_drop, x_fifo, x_coll;
unsigned int x_carrier, x_compr;
} ifc;
unsigned long long bin, bout, lbin, lbout;
int first;
if (argc != 2) {
fprintf(stderr, "Usage: %s interfacen", argv[0]);
exit(1);
}
interface = argv[1];
first = 1;
lbin = 0; lbout = 0;
while (1) {
pnd = fopen("/proc/net/dev", "r");
if (!pnd) {
fprintf(stderr, "%s: /proc/net/dev: %s", argv[0], strerror(errno));
exit(1);
}
/* Skip header */
skipline(pnd);
skipline(pnd);
/* Get interface info */
do {
if (fscanf(pnd, " %6[^:]:%u %u %u %u %u %u %u %u %u %u %u %u %u %u %u",
&ifc.name,
&ifc.r_bytes, &ifc.r_pkt, &ifc.r_err, &ifc.r_drop,
&ifc.r_fifo, &ifc.r_frame, &ifc.r_compr, &ifc.r_mcast,
&ifc.x_bytes, &ifc.x_pkt, &ifc.x_err, &ifc.x_drop,
&ifc.x_fifo, &ifc.x_coll, &ifc.x_carrier, &ifc.x_compr)
!= 16) {
exit(200);
}
skipline(pnd);
} while (strcmp(ifc.name, interface));
bin = ifc.r_bytes + (lbin & ~0xffffffffULL);
bout = ifc.x_bytes + (lbout & ~0xffffffffULL);
if (bin < lbin)
bin += (1ULL << 32);
if (bout < lbout)
bout += (1ULL << 32);
if (!first) {
printf("%d %Lu %Lun", time(NULL), (bout-lbout)*8, (bin-lbin)*8);
fflush(stdout);
} else {
first = 0;
}
lbin = bin; lbout = bout;
fclose(pnd);
sleep(1);
}
}
謝謝。但3g或gprs? – Carina
嘗試http://stackoverflow.com/questions/5027890/check-gprs-connection – Spencer
謝謝你的回覆,我學到了很多東西。但是我仍然想知道是否只通過連接狀態來確定它是否是?如果我想看看過去產生的流量,怎麼辦? – Carina