android例項之——流量監控懸浮窗(實時網速的獲取)
轉自:http://blog.csdn.net/csxwc/article/details/10110753
前幾天想做一個類似於360流量監控懸浮窗的效果,可以實時顯示網速。
重要的讀取系統的流量檔案,檔案路徑/proc/self/net/dev
下面是複製下來的dev檔案
Inter-| Receive | Transmit
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
lo: 5200 48 0 0 0 0 0 0 5200 48 0 0 0 0 0 0
dummy0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet0: 43413730 45920 0 0 0 0 0 0 4447114 39247 0 0 0 0 0 0
rmnet1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet3: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet4: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet5: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet6: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
rmnet7: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
usb0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
tunl0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sit0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ip6tnl0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
其中/eth是乙太網資訊 tiwlan0 是 Wifi rmnet0 是 GPRS
當然,對於不同的機型,檔案格式也可能不相同,可自行下載ES檔案瀏覽器檢視,並不在sd卡中,在根目錄下。
下面我們就來看看是怎麼讀取跟顯示的
首先是懸浮窗,佈局檔案如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <RelativeLayout
- android:id="@+id/rl_left"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <ImageView
- android:id="@+id/img_float"
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:src="@drawable/ic_launcher" />
- <ImageView
- android:id="@+id/img_close"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@android:drawable/presence_busy"
- android:visibility="gone" />
- </RelativeLayout>
- <TextView
- android:id="@+id/tv_show"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:paddingLeft="5dp"
- android:textColor="#ffff4444"
- android:textSize="15sp" />
- </RelativeLayout>
程式碼如下:
- public class FloatView extends View {
- WindowManager mWManger;
- WindowManager.LayoutParams mWManParams;
- public View view;
- //初始位置
- private float startX;
- private float startY;
- //座標
- private float x;
- private float y;
- //
- private float mTouchSatrtX;
- private float mTouchStartY;
- //元件
- public ImageView img_folat,img_close;
- public TextView tv_show;
- Context mContext;
- public FloatView(Context context) {
- super(context);
- this.mContext = context;
- }
- /**
- * 初始化mWManger,mWManParams
- */
- public void show(){
- mWManger = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
- mWManParams = new WindowManager.LayoutParams();
- //設定LayoutParams的引數
- mWManParams.type = 2002;//設定系統級視窗
- mWManParams.flags |= 8;
- //調整懸浮窗到左上角
- mWManParams.gravity = Gravity.TOP|Gravity.LEFT;
- //以螢幕左上角為源點,設定x,y
- mWManParams.x = 0;
- mWManParams.y = 0;
- //懸浮窗的長寬資料
- mWManParams.width = 80;
- mWManParams.height = 40;
- mWManParams.format = -3;//透明
- //載入懸浮窗佈局檔案
- view = LayoutInflater.from(mContext).inflate(R.layout.traffic_view, null);
- mWManger.addView(view, mWManParams);
- view.setOnTouchListener(new OnTouchListener() {
- /**
- * 改變懸浮窗位置
- */
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- //獲取相對螢幕的位置,即以螢幕左上角為原點
- x = event.getRawX();
- y = event.getRawY() - 25;//25為系統狀態列的高度
- //Log.e("初始位置", x+"======="+y);
- switch(event.getAction()){
- case MotionEvent.ACTION_DOWN:
- startX = x;
- startY = y;
- //獲取相對View的座標,以view的左上角為原點
- mTouchSatrtX = event.getX();
- mTouchStartY = event.getY();
- //Log.e("相對view的位置", mTouchSatrtX+"--------"+mTouchStartY);
- break;
- case MotionEvent.ACTION_MOVE:
- updatePosition();
- break;
- case MotionEvent.ACTION_UP:
- updatePosition();
- show_img_close();
- mTouchSatrtX = mTouchStartY =0;
- break;
- }
- return true;
- }
- });
- img_close = (ImageView) view.findViewById(R.id.img_close);
- /**
- * 關閉懸浮窗圖示點選事件
- */
- img_close.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(mContext,Traffic_Service.class);
- mContext.stopService(intent);
- view.setVisibility(View.GONE);
- }
- });
- tv_show = (TextView) view.findViewById(R.id.tv_show);
- }
- /**
- * 更新懸浮窗的位置
- *
- */
- public void updatePosition(){
- mWManParams.x = (int) (x - mTouchSatrtX);
- mWManParams.y = (int) (y - mTouchStartY);
- mWManger.updateViewLayout(view, mWManParams);
- }
- /**
- * 控制關閉懸浮窗按鈕的顯示與隱藏
- */
- public void show_img_close() {
- if (Math.abs(x - startX) < 1.5 && Math.abs(y - startY) < 1.5
- && !img_close.isShown()) {
- img_close.setVisibility(View.VISIBLE);
- } else if (img_close.isShown()) {
- img_close.setVisibility(View.GONE);
- }
- }
- }
下面看看程式碼:
- public class Traffic_Service extends Service {
- //
- private Handler mHandler;
- // 系統流量檔案
- public final String DEV_FILE = "/proc/self/net/dev";
- // 流量資料
- String[] ethData = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0", "0", "0", "0", "0" };
- String[] gprsData = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0", "0", "0", "0", "0", "0" };
- String[] wifiData = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0", "0", "0", "0", "0", "0" };
- // 用來儲存前一個時間點的資料
- String[] data = { "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
- "0" };
- // 乙太網
- final String ETHLINE = "eth0";
- // wifi
- final String WIFILINE = "wlan0";
- // gprs
- final String GPRSLINE = "rmnet0";
- /**
- * 定義執行緒週期性地獲取網速
- */
- private Runnable mRunnable = new Runnable() {
- // 每3秒鐘獲取一次資料,求平均,以減少讀取系統檔案次數,減少資源消耗
- @Override
- public void run() {
- refresh();
- mHandler.postDelayed(mRunnable, 3000);
- }
- };
- /**
- * 啟動服務時就開始啟動執行緒獲取網速
- */
- @Override
- public void onStart(Intent intent, int startId) {
- mHandler.postDelayed(mRunnable, 0);
- };
- /**
- * 在服務結束時刪除訊息佇列
- */
- @Override
- public void onDestroy() {
- mHandler.removeCallbacks(mRunnable);
- super.onDestroy();
- };
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- final FloatView view = new FloatView(this);
- view.show();
- mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- if (msg.what == 1) {
- // System.out.println((float) (msg.arg1 / 1024) + "kb/s");
- view.tv_show.setText((float) (msg.arg1 / (1024 * 3))
- + "k/s");
- }
- }
- };
- }
- /**
- * 讀取系統流量檔案
- */
- public void readDev() {
- FileReader fr = null;
- try {
- fr = new FileReader(DEV_FILE);
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(this, "系統流量檔案讀取失敗", Toast.LENGTH_SHORT).show();
- }
- BufferedReader bufr = new BufferedReader(fr, 500);
- String line;
- String[] data_temp;
- String[] netData;
- int k;
- int j;
- // 讀取檔案,並對讀取到的檔案進行操作
- try {
- while ((line = bufr.readLine()) != null) {
- data_temp = line.trim().split(":");
- if (line.contains(ETHLINE)) {
- netData = data_temp[1].trim().split(" ");
- for (k = 0, j = 0; k < netData.length; k++) {
- if (netData[k].length() > 0) {
- ethData[j] = netData[k];
- j++;
- }
- }
- } else if (line.contains(GPRSLINE)) {
- netData = data_temp[1].trim().split(" ");
- for (k = 0, j = 0; k < netData.length; k++) {
- if (netData[k].length() > 0) {
- gprsData[j] = netData[k];
- j++;
- }
- }
- } else if (line.contains(WIFILINE)) {
- netData = data_temp[1].trim().split(" ");
- for (k = 0, j = 0; k < netData.length; k++) {
- wifiData[j] = netData[k];
- j++;
- }
- }
- }
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 實時讀取系統流量檔案,更新
- */
- public void refresh() {
- // 讀取系統流量檔案
- readDev();
- // 計算增量
- int[] delta = new int[12];
- delta[0] = Integer.parseInt(ethData[0]) - Integer.parseInt(data[0]);
- delta[1] = Integer.parseInt(ethData[1]) - Integer.parseInt(data[1]);
- delta[2] = Integer.parseInt(ethData[8]) - Integer.parseInt(data[2]);
- delta[3] = Integer.parseInt(ethData[9]) - Integer.parseInt(data[3]);
- delta[4] = Integer.parseInt(gprsData[0]) - Integer.parseInt(data[4]);
- delta[5] = Integer.parseInt(gprsData[1]) - Integer.parseInt(data[5]);
- delta[6] = Integer.parseInt(gprsData[8]) - Integer.parseInt(data[6]);
- delta[7] = Integer.parseInt(gprsData[9]) - Integer.parseInt(data[7]);
- delta[8] = Integer.parseInt(wifiData[0]) - Integer.parseInt(data[8]);
- delta[9] = Integer.parseInt(wifiData[1]) - Integer.parseInt(data[9]);
- delta[10] = Integer.parseInt(wifiData[8]) - Integer.parseInt(data[10]);
- delta[11] = Integer.parseInt(wifiData[9]) - Integer.parseInt(data[11]);
- data[0] = ethData[0];
- data[1] = ethData[1];
- data[2] = ethData[8];
- data[3] = ethData[9];
- data[4] = gprsData[0];
- data[5] = gprsData[1];
- data[6] = gprsData[8];
- data[7] = gprsData[9];
- data[8] = wifiData[0];
- data[9] = wifiData[1];
- data[10] = wifiData[8];
- data[11] = wifiData[9];
- // 每秒下載的位元組數
- int traffic_data = delta[0] + delta[4] + delta[8];
- // System.out.println("每秒流量"+traffic_data);
- Message msg = mHandler.obtainMessage();
- msg.what = 1;
- msg.arg1 = traffic_data;
- mHandler.sendMessage(msg);
- }
- }
附原始碼:下載原始碼
相關文章
- Android實現流量統計和網速監控懸浮窗Android
- Android懸浮窗--獲取記憶體Android記憶體
- 懸浮窗的一種實現 | Android懸浮窗Window應用Android
- Android 懸浮窗Android
- Android 懸浮視窗的實現Android
- Android懸浮窗的學習Android
- Android 高仿UC瀏覽器監控剪下板彈出懸浮窗功能Android瀏覽器
- Andorid 任意介面懸浮窗,實現懸浮窗如此簡單
- Android應用內懸浮窗的實現方案Android
- Android 懸浮窗 System Alert WindowAndroid
- 【轉載】使用WindowManage實現Android懸浮窗Android
- 如何獲取Vivo系統的懸浮窗許可權狀態
- QPM 之懸浮窗設定資訊
- 滑鼠懸浮實現翻牌效果程式碼例項
- 通過反射獲取窗體例項反射
- linux網路流量實時監控指令碼Linux指令碼
- iftop--實時網路介面流量監控工具
- QPM 之懸浮窗助力效能優化優化
- 滑鼠懸浮實現抖動效果例項程式碼
- Android 攝像頭預覽懸浮窗Android
- Android仿微信文章懸浮窗效果Android
- jQuery獲取視窗尺寸的例項程式碼jQuery
- Android懸浮框的實現Android
- 懸浮窗開發設計實踐
- Linux下網路流量實時監控工具大全Linux
- 在Linux中,如何實時監控網路流量?Linux
- Android實現仿360手機衛士懸浮窗效果Android
- javascript獲取視窗大小例項程式碼JavaScript
- 實時獲取當前時間程式碼例項
- 滑鼠懸浮文字實現霓虹燈效果程式碼例項
- Android 輔助許可權與懸浮窗Android
- 下沉式通知的一種實現 | Android懸浮窗Window應用Android
- 滑鼠懸浮評分效果程式碼例項
- iOS開發——實時監控網速(僅作參考)iOS
- 類似網路螞蟻的懸浮窗體 (轉)
- css滑鼠懸浮tips效果程式碼例項CSS
- javascript滑鼠懸浮行變色程式碼例項JavaScript
- nagios監控例項 -- PostgreSQL監控iOSSQL