AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical" > <ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
import android.net.TrafficStats;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity { LinearLayout llayout;
String uploadSpeed, downloadSpeed = null;
String speedError = null;
int chechLength = 10;
TextView[] tvDownload, tvUpload; // ----
long BeforeTime ,TotalTxBeforeTest, TotalRxBeforeTest , TotalTxAfterTest , TotalRxAfterTest, AfterTime; double TimeDifference , rxDiff , txDiff;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); llayout = (LinearLayout)findViewById(R.id.layout1) ; tvDownload = new TextView[chechLength];
tvUpload = new TextView[chechLength];
for(int i=0;i<chechLength;i++) { tvDownload[i] = new TextView(this);
tvUpload[i] = new TextView(this);
try { Thread.sleep(1000); BeforeTime = System.currentTimeMillis();
TotalTxBeforeTest = TrafficStats.getTotalTxBytes();
TotalRxBeforeTest = TrafficStats.getTotalRxBytes();
} catch (InterruptedException e) { e.printStackTrace();
} /* DO WHATEVER NETWORK STUFF YOU NEED TO DO */
try { Thread.sleep(1000);
TotalTxAfterTest = TrafficStats.getTotalTxBytes();
TotalRxAfterTest = TrafficStats.getTotalRxBytes();
AfterTime = System.currentTimeMillis();
} catch (InterruptedException e) { e.printStackTrace(); } TimeDifference = AfterTime - BeforeTime; rxDiff = TotalRxAfterTest - TotalRxBeforeTest;
txDiff = TotalTxAfterTest - TotalTxBeforeTest;
if((rxDiff != 0) || (txDiff != 0)) { double rxBPS = (rxDiff / (TimeDifference/1000));
// total rx bytes per second.
double txBPS = (txDiff / (TimeDifference/1000));
// total tx bytes per second.
downloadSpeed = String.valueOf((rxBPS*8)/1024);
uploadSpeed = String.valueOf((txBPS*8)/1024);
/*downloadSpeed = String.valueOf((rxBPS)/1024) + "KB/s. Total rx = " + rxDiff;
uploadSpeed = String.valueOf((txBPS)/1024) + "KB/s. Total tx = " + txDiff;
*/
tvDownload[i].setText("DOWNLOAD : "+downloadSpeed+" Kpps");
tvUpload[i].setText("UPLOAD : "+uploadSpeed + " Kbps"); } else { speedError = "No uploaded or downloaded bytes.";
tvDownload[i].setText("DOWNLOAD : "+speedError);
tvUpload[i].setText("UPLOAD : "+speedError); } llayout.addView(tvDownload[i]);
llayout.addView(tvUpload[i]); } } }///
No comments:
Post a Comment