用Android Studio写一个流量监控程序,可以在另一个界面实现分时间段查询流量使用情况和流量统计,发送完整代码
时间: 2024-02-13 08:04:00 浏览: 179
好的,以下是一个简单的流量监控程序的完整代码:
MainActivity.java
```
import android.content.Intent;
import android.net.TrafficStats;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private int uid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uid = android.os.Process.myUid();
}
public void onTimeButtonClick(View view) {
long startTime = System.currentTimeMillis() - 1000 * 60 * 60;
long endTime = System.currentTimeMillis();
long rxBytes = TrafficStats.getTotalRxBytes() - TrafficStats.getUidRxBytes(uid, startTime, endTime);
long txBytes = TrafficStats.getTotalTxBytes() - TrafficStats.getUidTxBytes(uid, startTime, endTime);
TextView text = findViewById(R.id.text);
text.setText("当前时间段内的流量使用情况:\n接收:" + rxBytes + " 字节\n发送:" + txBytes + " 字节");
}
public void onTotalButtonClick(View view) {
long totalRxBytes = TrafficStats.getTotalRxBytes();
long totalTxBytes = TrafficStats.getTotalTxBytes();
TextView text = findViewById(R.id.text);
text.setText("总的流量使用情况:\n接收:" + totalRxBytes + " 字节\n发送:" + totalTxBytes + " 字节");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_statistics:
Intent intent = new Intent(this, StatisticsActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textSize="18sp" />
<Button
android:id="@+id/time_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="时间段查询"
android:onClick="onTimeButtonClick" />
<Button
android:id="@+id/total_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="总流量查询"
android:onClick="onTotalButtonClick" />
</LinearLayout>
```
StatisticsActivity.java
```
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import androidx.appcompat.app.AppCompatActivity;
public class StatisticsActivity extends AppCompatActivity {
private SQLiteDatabase db;
private SimpleCursorAdapter adapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statistics);
listView = findViewById(R.id.list_view);
db = new TrafficStatisticsDbHelper(this).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM traffic_statistics ORDER BY id DESC", null);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor,
new String[] { "start_time", "end_time", "rx_bytes", "tx_bytes" },
new int[] { R.id.text1, R.id.text2, R.id.text3, R.id.text4 }, 0);
listView.setAdapter(adapter);
}
public void onClearButtonClick(View view) {
db.delete("traffic_statistics", null, null);
adapter.changeCursor(db.rawQuery("SELECT * FROM traffic_statistics ORDER BY id DESC", null));
}
}
```
activity_statistics.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#cccccc"
android:dividerHeight="1dp"
android:padding="16dp"
android:textSize="18sp" />
<Button
android:id="@+id/clear_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空记录"
android:onClick="onClearButtonClick" />
</LinearLayout>
```
list_item.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="16dp" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="16dp" />
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginStart="16dp" />
</LinearLayout>
```
TrafficStatisticsDbHelper.java
```
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class TrafficStatisticsDbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "traffic_statistics.db";
public TrafficStatisticsDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE traffic_statistics (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"start_time INTEGER, " +
"end_time INTEGER, " +
"rx_bytes INTEGER, " +
"tx_bytes INTEGER" +
")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
```
注意:为了让代码更加清晰,省略了一些异常处理和注释。在实际开发中,请务必添加适当的异常处理和注释。
阅读全文