android 流量统计功能 的实现
2013-11-06 09:41
429 查看
android.net.TrafficStats
类中,提供了多种静态方法,可以直接调用获取,返回类型均为long型,如果返回等于-1代表 UNSUPPORTED 当前设备不支持统计。
static long getUidRxBytes(int uid) //获取某个网络UID的接受字节数
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数
since device boot。注意,流量是从开机开始算的,这个经过验证了,没有问题,可以上官网上查看。
四个SharedPreferences :
long beforeData(中间值,无固定意义) ,totalData(MOS自安装以来的总流量) ,lastTotalData(MOS上一次发送的总流量,日期变化就会发送)。
String day
各种操作:
第一次安装:
将安装日期放入缓存,第一次安装中操作;
APP登录后:
加载完缓存后,开启service,登录后操作;
开机后:
发送一个广播,功能只是用来清零beforeData 。
Service功能:
每十分钟后查看一次已用流量,用这个值减去beforeData 再与 totalData 存到totalData 里面,然后将这个值赋给beforeData 。
每次查看已用流量都获取本地日期,然后和存起来的日期day相比,一样的话不进行操作,不一样就发送(totalData- lastTotalData)和 日期 到服务器。然后将最新日期保存到String里,并将此时的totalData保存到lastTotalData里。
服务器端:
接收数据后,插入到数据库中,关键数据:员工 + 日期 + 当日使用总流量 。
第一次安装:
添加如下代码:
登录后,添加如下代码:
所写服务:
所写广播:
类中,提供了多种静态方法,可以直接调用获取,返回类型均为long型,如果返回等于-1代表 UNSUPPORTED 当前设备不支持统计。
static long getUidRxBytes(int uid) //获取某个网络UID的接受字节数
static long getUidTxBytes(int uid) //获取某个网络UID的发送字节数
since device boot。注意,流量是从开机开始算的,这个经过验证了,没有问题,可以上官网上查看。
四个SharedPreferences :
long beforeData(中间值,无固定意义) ,totalData(MOS自安装以来的总流量) ,lastTotalData(MOS上一次发送的总流量,日期变化就会发送)。
String day
各种操作:
第一次安装:
将安装日期放入缓存,第一次安装中操作;
APP登录后:
加载完缓存后,开启service,登录后操作;
开机后:
发送一个广播,功能只是用来清零beforeData 。
Service功能:
每十分钟后查看一次已用流量,用这个值减去beforeData 再与 totalData 存到totalData 里面,然后将这个值赋给beforeData 。
每次查看已用流量都获取本地日期,然后和存起来的日期day相比,一样的话不进行操作,不一样就发送(totalData- lastTotalData)和 日期 到服务器。然后将最新日期保存到String里,并将此时的totalData保存到lastTotalData里。
服务器端:
接收数据后,插入到数据库中,关键数据:员工 + 日期 + 当日使用总流量 。
第一次安装:
添加如下代码:
sharedPrefer=this.getSharedPreferences("LIULIANG", Context.MODE_WORLD_WRITEABLE); Editor editor1 = sharedPrefer.edit(); editor1.putString("calendarDate", dateFormat.format(now)); editor1.commit();
登录后,添加如下代码:
//开启定时统计流量的服务 Intent intent = new Intent(mosApp, LiuLiangService.class); PendingIntent sender = PendingIntent.getService(mosApp, 0, intent, 0); long firstime = SystemClock.elapsedRealtime(); am = (AlarmManager) mosApp.getSystemService(Context.ALARM_SERVICE); // 十分钟一个周期,不停的开启服务 am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime, 10*60*1000, sender); //end
所写服务:
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.util.EntityUtils; import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.net.TrafficStats; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.preference.PreferenceManager; import android.widget.Toast; import com.alibaba.fastjson.JSONObject; /** * 定时统计流量,每十分钟一次统计一次 如果统计时日期为第二天,则发送前一天流量到服务器端 * * */ public class LiuLiangService extends Service { private SharedPreferences sharedPrefer; private String result; private JSONObject flowJsonObject = new JSONObject(); public CacheProcess cache; String dateStr = null; // 绑定activity @Override public IBinder onBind(Intent intent) { return null; } // 创建时调用 @Override public void onCreate() { sharedPrefer = this.getSharedPreferences("LIULIANG",Context.MODE_WORLD_WRITEABLE); super.onCreate(); } // 销毁时调用 @Override public void onDestroy() { super.onDestroy(); } // 开始service @Override public void onStart(Intent intent, int startId) { Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); dateStr = dateFormat.format(now); long amountData = 0; //日期与缓存日期时间不一样的话,就发送缓存日期当天流量 if (!dateStr.equals(sharedPrefer.getString("calendarDate", ""))) { // Toast.makeText(getApplicationContext(), // "该发前一天流量了,当前日期:" + dateStr + "缓存日期:"+ sharedPrefer.getString("calendarDate", ""), // Toast.LENGTH_SHORT).show(); // 发送上一天流量和日期 flowJsonObject.put("localNetId", this.getCacheProcess().getCacheValueInSharedPreferences(this, "localNetId")); flowJsonObject.put("areaId", this.getCacheProcess().getCacheValueInSharedPreferences(this, "areaId")); flowJsonObject.put("workAreaId", this.getCacheProcess().getCacheValueInSharedPreferences(this, "workAreaId")); flowJsonObject.put("staffId", this.getCacheProcess().getCacheValueInSharedPreferences(this, "staffId")); flowJsonObject.put("flowData", sharedPrefer.getLong("totalData", 0)-sharedPrefer.getLong("lastTotalData", 0)); flowJsonObject.put("stsDate", sharedPrefer.getString("calendarDate", "")); new sendThread().start(); } else {//日期仍然是当天 try {// 地图很费流量,而且已下载地图不保存,滑动时重新加载地图数据;图片每张大概100kb;列表几乎不费流量,几kb而已; PackageManager pm = getPackageManager(); ApplicationInfo ai = pm.getApplicationInfo("com.cattsoft.mos",PackageManager.GET_ACTIVITIES); long rxdata = TrafficStats.getUidRxBytes(ai.uid); rxdata = rxdata / 1024; long txdata = TrafficStats.getUidTxBytes(ai.uid); txdata = txdata / 1024; amountData = rxdata + txdata; } catch (NameNotFoundException e) { e.printStackTrace(); } Editor editor = sharedPrefer.edit(); editor.putLong("totalData",(amountData - sharedPrefer.getLong("beforeData", 0))+ sharedPrefer.getLong("totalData", 0)); editor.putLong("beforeData", amountData); editor.commit(); // Toast.makeText(getApplicationContext(),"lastTotal:" + sharedPrefer.getLong("lastTotalData", 0) + " kb; Total:" // + sharedPrefer.getLong("totalData", 0) + " kb", Toast.LENGTH_SHORT).show(); } super.onStart(intent, startId); } private class sendThread extends Thread { public void run() { Message msg=new Message(); try { result = Communication.getPostResponse("tm/deviceCoordinateAction.do?method=addFlowData4MOS", flowJsonObject.toString()); msg.what = 1; sendHandler.sendMessage(msg); } catch (ClientProtocolException e) { e.printStackTrace(); msg.what = 0; sendHandler.sendMessage(msg); return; } catch (IOException e) { e.printStackTrace(); msg.what = 0; sendHandler.sendMessage(msg); return; } } } private Handler sendHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 0: Toast.makeText(getApplicationContext(), "发送失败", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(getApplicationContext(), "已发送"+result+"日流量", Toast.LENGTH_SHORT).show(); Editor editor = sharedPrefer.edit(); editor.putLong("lastTotalData", sharedPrefer.getLong("totalData", 0)); editor.putString("calendarDate", dateStr); editor.commit(); break; } } }; public CacheProcess getCacheProcess() { if(cache==null) { cache=new CacheProcess(); } return cache; } }
所写广播:
package com.cattsoft.mos.service; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.SystemClock; /** * 用于流量统计服务 * */ public class LiuLiangBootReceiver extends BroadcastReceiver { public static AlarmManager am; private SharedPreferences sharedPrefer; @Override public void onReceive(Context context, Intent mintent) { sharedPrefer = context.getSharedPreferences("LIULIANG", Context.MODE_WORLD_WRITEABLE); Editor editor = sharedPrefer.edit(); editor.putLong("beforeData", 0); editor.commit(); } }
相关文章推荐
- android 流量统计实现思路
- php流量统计功能的实现代码
- Android基于TrafficStats实现实时流量统计
- Shell脚本实现的单机流量统计功能
- Android编程实现canvas绘制柱状统计图功能【自动计算宽高及分度值、可左右滑动】
- Python实现获取nginx服务器ip及流量统计信息功能示例
- 博客园公告栏微博相关功能及流量统计实现
- Shell脚本实现的单机流量统计功能
- 代码实现Android流量统计
- mapreduce实现简单的流量统计功能
- Android统计图形绘制及点击事等功能的实现-achartengine篇
- 使用目前hadoop内置的序列化类(不使用自定义序列化类),实现流量统计的功能
- 流量统计功能的实现代码
- Android实现流量统计和网速监控悬浮窗
- 基于Android MPAndroidChart实现腾讯QQ群数据统计报表核心功能
- android中实现简单的流量管理功能
- 基于Android MPAndroidChart实现腾讯QQ群数据统计报表核心功能
- Android编程实现简单流量管理功能实例
- Cocos2d-x3.3RC0通过JNI调用Android的Java层代码,实现分享功能