使用Android.net.TrafficStats
获取传输的通信量是可行的。这里是这个想法的一个实现,它测量了上游和下游的传输速率。您可以通过将TrafficSpeedMeasurer.TrafficType.mobile
传递给TrafficSpeedMeasurer
构造函数来测量移动网络的速率,否则使用TrafficSpeedMeasurer.TrafficType.All
将导致测量一般通信量(WiFi/mobile)。另外,通过在mainactivity
中设置show_speed_in_bits=true
,可以将速度测量单位更改为bit
s每秒。
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final boolean SHOW_SPEED_IN_BITS = false;
private TrafficSpeedMeasurer mTrafficSpeedMeasurer;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.connection_class);
mTrafficSpeedMeasurer = new TrafficSpeedMeasurer(TrafficSpeedMeasurer.TrafficType.ALL);
mTrafficSpeedMeasurer.startMeasuring();
}
@Override
protected void onDestroy() {
super.onDestroy();
mTrafficSpeedMeasurer.stopMeasuring();
}
@Override
protected void onPause() {
super.onPause();
mTrafficSpeedMeasurer.removeListener(mStreamSpeedListener);
}
@Override
protected void onResume() {
super.onResume();
mTrafficSpeedMeasurer.registerListener(mStreamSpeedListener);
}
private ITrafficSpeedListener mStreamSpeedListener = new ITrafficSpeedListener() {
@Override
public void onTrafficSpeedMeasured(final double upStream, final double downStream) {
runOnUiThread(new Runnable() {
@Override
public void run() {
String upStreamSpeed = Utils.parseSpeed(upStream, SHOW_SPEED_IN_BITS);
String downStreamSpeed = Utils.parseSpeed(downStream, SHOW_SPEED_IN_BITS);
mTextView.setText("Up Stream Speed: " + upStreamSpeed + "\n" + "Down Stream Speed: " + downStreamSpeed);
}
});
}
};
}
TrafficSpeedMeasurer.java
import android.net.TrafficStats;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
public class TrafficSpeedMeasurer {
private ITrafficSpeedListener mTrafficSpeedListener;
private SamplingHandler mHandler;
private TrafficType mTrafficType;
private long mLastTimeReading;
private long mPreviousUpStream = -1;
private long mPreviousDownStream = -1;
public TrafficSpeedMeasurer(TrafficType trafficType) {
mTrafficType = trafficType;
HandlerThread thread = new HandlerThread("ParseThread");
thread.start();
mHandler = new SamplingHandler(thread.getLooper());
}
public void registerListener(ITrafficSpeedListener iTrafficSpeedListener) {
mTrafficSpeedListener = iTrafficSpeedListener;
}
public void removeListener() {
mTrafficSpeedListener = null;
}
public void startMeasuring() {
mHandler.startSamplingThread();
mLastTimeReading = SystemClock.elapsedRealtime();
}
public void stopMeasuring() {
mHandler.stopSamplingThread();
finalReadTrafficStats();
}
private void readTrafficStats() {
long newBytesUpStream = (mTrafficType == TrafficType.MOBILE ? TrafficStats.getMobileTxBytes() : TrafficStats.getTotalTxBytes()) * 1024;
long newBytesDownStream = (mTrafficType == TrafficType.MOBILE ? TrafficStats.getMobileRxBytes() : TrafficStats.getTotalRxBytes()) * 1024;
long byteDiffUpStream = newBytesUpStream - mPreviousUpStream;
long byteDiffDownStream = newBytesDownStream - mPreviousDownStream;
synchronized (this) {
long currentTime = SystemClock.elapsedRealtime();
double bandwidthUpStream = 0;
double bandwidthDownStream = 0;
if (mPreviousUpStream >= 0) {
bandwidthUpStream = (byteDiffUpStream) * 1.0 / (currentTime - mLastTimeReading);
}
if (mPreviousDownStream >= 0) {
bandwidthDownStream = (byteDiffDownStream) * 1.0 / (currentTime - mLastTimeReading);
}
if (mTrafficSpeedListener != null) {
mTrafficSpeedListener.onTrafficSpeedMeasured(bandwidthUpStream, bandwidthDownStream);
}
mLastTimeReading = currentTime;
}
mPreviousDownStream = newBytesDownStream;
mPreviousUpStream = newBytesUpStream;
}
private void finalReadTrafficStats() {
readTrafficStats();
mPreviousUpStream = -1;
mPreviousDownStream = -1;
}
private class SamplingHandler extends Handler {
private static final long SAMPLE_TIME = 1000;
private static final int MSG_START = 1;
private SamplingHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_START:
readTrafficStats();
sendEmptyMessageDelayed(MSG_START, SAMPLE_TIME);
break;
default:
throw new IllegalArgumentException("Unknown what=" + msg.what);
}
}
void startSamplingThread() {
sendEmptyMessage(SamplingHandler.MSG_START);
}
void stopSamplingThread() {
removeMessages(SamplingHandler.MSG_START);
}
}
public enum TrafficType {
MOBILE,
ALL
}
}
public interface ITrafficSpeedListener {
void onTrafficSpeedMeasured(double upStream, double downStream);
}
import java.util.Locale;
public class Utils {
private static final long B = 1;
private static final long KB = B * 1024;
private static final long MB = KB * 1024;
private static final long GB = MB * 1024;
public static String parseSpeed(double bytes, boolean inBits) {
double value = inBits ? bytes * 8 : bytes;
if (value < KB) {
return String.format(Locale.getDefault(), "%.1f " + (inBits ? "b" : "B") + "/s", value);
} else if (value < MB) {
return String.format(Locale.getDefault(), "%.1f K" + (inBits ? "b" : "B") + "/s", value / KB);
} else if (value < GB) {
return String.format(Locale.getDefault(), "%.1f M" + (inBits ? "b" : "B") + "/s", value / MB);
} else {
return String.format(Locale.getDefault(), "%.2f G" + (inBits ? "b" : "B") + "/s", value / GB);
}
}
}
问题内容: 设置INTERNET_ACCESS等后,出现此错误。 这是我测试过的代码的另一部分,仍然收到该异常 } 问题答案: “ftp.194.90.81.149”: No address associated with hostname 您收到UnknownHostException的事实意味着ftp.194.90.81.149在DNS中不是真实的主机名。我怀疑其中的数字部分是您真正想要的。即
我已经成功地使用Parse将声音片段上传到设备或从设备下载。我也在尝试类似的事情,但有图像。我有一个imageView,它包含用户选择的图像。我想将此图像作为嵌套在parseObject(称为“profileParseObject”)中的parseFile发送 我看到了这篇文章:将图库中的图像放入 ParseFile android 但是,使用: 导致“当前上下文中不存在名称”位图“”(我不知道需
问题内容: 在我的项目中,我具有文件上传功能。文件通过FTP上传。我需要配置一个侦听器,该侦听器将检查新文件并仅在文件上传完成后才调用脚本。因为如果我在检测到新文件后立即运行此脚本,它将开始处理未完全上载的文件,这将导致错误。谁能说出这在LINUX上是否可行,我该怎么做? 问题答案: 我会尝试使用inotify,事件代码为IN_CLOSE_WRITE。
非常感谢之前对我的查询的响应,我有JSONArray在buffer,我说的这些话可能是错的,所以请纠正我。 public void get_data(字符串数据){ 我已经创建了一个dbhlper.java类来处理它,但是我是新手,我缺少了一些东西,而不是将这些数据传送到namearray[i]等等。 公共布尔insertData(String rtrname,String ctgname,Str
问题所在 问题很怪异,出乎意料。上传/下载工作完美,但非常慢。 我在一个良好的互联网连接1MBps,但这些文件从来没有以这种速度传输。它大约是100-150KBps,这几乎是我的网络可用速度的15%。 请注意,我没有更改上载和下载的超时设置。他们违约了。
问题内容: 有一种套接字方法可以获取给定网络接口的IP: 返回以下内容: 是否有类似的方法返回该接口的网络传输?我知道我可以阅读,但是我想使用套接字方法。 问题答案: 轮询以太网接口统计信息的最佳方法是通过SNMP … 看起来您正在使用linux …如果是,请在 / etc / defaults / snmpd中*安装后,使用以下选项加载您的Linux (确保其中的行如下所示): * 您可能还需要