Android項目實戰_手機安全衛士流量統計

來源:互聯網
上載者:User

標籤:cti   emd   amp   title   ges   手機安全   mat   protect   tac   

## 1.抽屜控制項
SlidingDrawer:一定要配置android:handle(把手)和android:content(內容),並在子View中添加把手和內容的布局
```java
<SlidingDrawer
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle"
android:orientation="horizontal" >

<ImageView
android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/detail" />

<LinearLayout
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#22000000"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是抽屜裡面的內容" />
</LinearLayout>
</SlidingDrawer>
```
## 2.擷取總流量
```java
TrafficStats.getMobileRxBytes(); //總的移動網路下載流量
TrafficStats.getMobileTxBytes();//總的移動網路上傳流量
TrafficStats.getTotalRxBytes();//總的下載流量
TrafficStats.getTotalTxBytes();//總的網路流量
```
## 3.擷取單個應用的流量
```java
PackageManager pm = context.getPackageManager();
List<PackageInfo> packInfos = pm.getInstalledPackages(0);

packageInfo.applicationInfo.uid

TrafficStats.getUidRxBytes(int uid);//某個應用的下載流量
TrafficStats.getUidTxBytes(int uid);//某個應用的上傳流量
```
## 4.流量資訊儲存位置
上傳:/proc/uid_stat/[uid]/tcp_snd |udp_snd
下載:/proc/uid_stat/[uid]/tcp_rcv |udp_rcv

## 5.擷取檔案的數字摘要
```java
/**
* 根據檔案路徑得到檔案的md5演算法產生的數字摘要
* @param path
* @return
*/
private String getFileMd5(String path){
try {
File file = new File(path);
//得到一個數字摘要器
MessageDigest digest = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer))!=-1){
digest.update(buffer,0,len);
}
byte[] result = digest.digest();
StringBuilder sb = new StringBuilder();
for(byte b:result){
int number = b&0xff;
String str = Integer.toHexString(number);
if(str.length()==1){
sb.append("0");
}
sb.append(str);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

 1 package com.hb.mobilesafe.activities; 2  3 import com.hb.demo_mobilesafe.R; 4  5 import android.app.Activity; 6 import android.net.TrafficStats; 7 import android.os.Bundle; 8 import android.text.format.Formatter; 9 import android.view.Window;10 import android.widget.TextView;11 12 public class FlowStatisticAcitivty extends Activity {13     private TextView tv_wifimobile;14     private TextView tv_mobilestastic;15     @Override16     protected void onCreate(Bundle savedInstanceState) {17         super.onCreate(savedInstanceState);18         requestWindowFeature(Window.FEATURE_NO_TITLE);19         setContentView(R.layout.activity_flowstastic);20         tv_mobilestastic=(TextView) findViewById(R.id.tv_mobilestastic);21         tv_wifimobile=(TextView) findViewById(R.id.tv_wifimobile);22         long totalRxBytes = TrafficStats.getTotalRxBytes();//下行23         long totalTxBytes = TrafficStats.getTotalTxBytes();//上行24         25         long mobileRxBytes = TrafficStats.getMobileRxBytes();26         long mobileTxBytes = TrafficStats.getMobileTxBytes();27         28         tv_wifimobile.setText(Formatter.formatFileSize(this, totalRxBytes + totalTxBytes));29         tv_mobilestastic.setText(Formatter.formatFileSize(this, mobileRxBytes + mobileTxBytes));30         31         32         33     }34 35 }

 

Android項目實戰_手機安全衛士流量統計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.