2017-11-17 12 views
0

こんにちは私は、ユーザーのデータ消費を監視するアンドロイドアプリケーションを作成しています。しかし、私は悩んで、私は最後に消費されたデータを保存したいと思うし、彼らは再びサービスを開始すると、最後に消費されたデータなどから始めます。最後に受信したバイト数TrafficStats

これは私のサービスクラスです:

//THIS METHOD IS CALLED EVERY 3 seconds to check for new DATA CONSUMED 
    public void displayNotificationWIFI() { 

sharedPreferences = getSharedPreferences(SHARED_PREF_WIFI, Context.MODE_PRIVATE); 
    consumed = sharedPreferences.getString(WIFI_DATA_BYTES, null); 

    //CHECK IF CONSUMED IS NULL, IF NULL GET NEW DATA CONSUMED 
    if (consumed == null) { 

     rxBytes = TrafficStats.getTotalRxBytes() - mStartRX; 

     if (rxBytes >= 1024) { 
      rxKB = rxBytes/1024; 
      received = String.format("%.0f", rxKB); 
      unit = "KB"; 
      if (rxKB >= 1024) { 
       DecimalFormat formatMB = new DecimalFormat("0.0"); 
       rxMB = rxKB/1024; 
       received = formatMB.format(rxMB); 
       unit = "MB"; 
       if (rxMB >= 1024) { 
        DecimalFormat formatGB = new DecimalFormat("0.0"); 
        rxGB = rxMB/1024; 
        received = formatGB.format(rxGB); 
        unit = "GB"; 
       } 
      } 
      sharedPreferences = getSharedPreferences(SHARED_PREF_WIFI, Context.MODE_PRIVATE); 
      SharedPreferences.Editor editor = sharedPreferences.edit(); 
      editor.putString(WIFI_DATA_BYTES, String.valueOf(rxBytes)); 
      editor.putString(WIFI_DATA, received + " " + unit); 
      editor.apply(); 
     } 
    } 

//SINCE NOT NULL, START FROM LAST DATA CONSUMED 
     else { 
     //STARTS FROM LAST DATA CONSUMED, BUT I THINK THIS IS WRONG? 
     rxBytes = (TrafficStats.getTotalRxBytes() - mStartRX) + Double.parseDouble(consumed); 

     if (rxBytes >= 1024) { 
       rxKB = rxBytes/1024; 
       received = String.format("%.0f", rxKB); 
       unit = "KB"; 

       if (rxKB >= 1024) { 
        DecimalFormat formatMB = new DecimalFormat("0.0"); 
        rxMB = rxKB/1024; 
        received = formatMB.format(rxMB); 
        unit = "MB"; 

        if (rxMB >= 1024) { 
         DecimalFormat formatGB = new DecimalFormat("0.0"); 
         rxGB = rxMB/1024; 
         received = formatGB.format(rxGB); 
         unit = "GB"; 
        } 
       } 
       //SAVING THE NEW LAST CONSUMED DATA 
       sharedPreferences = getSharedPreferences(SHARED_PREF_WIFI,Context.MODE_PRIVATE); 
       SharedPreferences.Editor editors = sharedPreferences.edit(); 
       editors.putString(WIFI_DATA_BYTES, String.valueOf(rxBytes)); 
       editors.apply(); 
      } 
     } 
     remoteView = new RemoteViews(getPackageName(), R.layout.custom_notification_pingwifi); 
     remoteView.setTextViewText(R.id.txtData, received + " " + unit); 

     NotificationCompat.Builder n = new NotificationCompat.Builder(serviceClass.this); 
     n.setCustomContentView(remoteView) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setOngoing(true); 

     notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notificationManager.notify(0, n.build()); 
    } 

私のコードの問題は、サービスが再起動されたときに、ということです。古いデータには新しいデータが追加され、追加や追加が行われ、インターネットを使用していなくても停止しません。

+0

3秒ごとにループすると、デバイスのバッテリに極端な悪影響が発生します。 –

答えて

0

アプリケーションでデータを送受信するサービスでグローバルint変数を作成します。実際にはデータを送信してデータを受信するという意味で、すべてがデータ使用と見なされます。 ので、あなたがメッセージを取得するたびにそのバイトをカウントし、データはこのようにもそれをカウント送ったび:最後に

totalBytes +=numberOfSent 
tatalBytes +=numberOfReceived 

が、あなたはバイトのデータ使用量を取得します^ 6 10でそれを分割。

幸運!

関連する問題