0

我试图每隔几秒钟从我的应用程序中捕获和发布(带宽)数据,但是每次我解析它显示为 0 的数据时。我能够成功地查看我正在寻找的值在 android 设备的屏幕上更改实时(发送和接收的数据量)但是当我尝试将它们解析到服务器时。我不断地发布一个零值 - 而不是当前发送或接收的数据量的值。起初我以为我可能使用了不正确的长值(mStartTX),但是即使我将其更改为“txBytes”,它仍然显示为零值。

我只需要帮助来解析当前发送和接收的字节的值——但我似乎做不到。

mStartTX 的初始值为 0,但我认为我应该能够通过使用 txBytes 来捕获它的值——但两者在到达服务器时都显示为零值——即使它们在屏幕上显示为动态变化的值(其中我正在尝试捕捉。)

我想我向服务器发送了错误的值 - 但我真的不确定。

PS我也尝试过发送其他值:

testObject.put("DataO", String.valueOf(textRssi));
  testObject.put("DataI", String.valueOf(textSpeed));

但是,它们显示为 null 而不是零。由于某种原因,我似乎无法发布任何动态变化的数据......而且我不明白为什么。

服务器截图:

https://docs.google.com/file/d/0B7v2rXEILF-aWnRUUlZEMkxQRm8/edit?usp=sharing

来源:Service_class.java

public class Service_class extends Service {
  TextView textSsid, textSpeed, textRssi;

  public Handler mHandler = new Handler();
    public long mStartRX = 0;
  public long mStartTX = 0;
  public long txBytes;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate();

  textSsid = (TextView) findViewById(R.id.Ssid);
  textSpeed = (TextView) findViewById(R.id.Speed);
  textRssi = (TextView) findViewById(R.id.Rssi);
  Long.toString(mStartTX);
  Long.toString(mStartRX);
  Long.toString(txBytes);

   mStartRX = TrafficStats.getTotalRxBytes();
     mStartTX = TrafficStats.getTotalTxBytes();
     if (mStartRX == TrafficStats.UNSUPPORTED || mStartTX == TrafficStats.UNSUPPORTED) {
      AlertDialog.Builder alert = new AlertDialog.Builder(this);
          alert.setTitle("Uh Oh!");
          alert.setMessage("Your device does not support traffic stat monitoring.");
          alert.show();
     } else {
      mHandler.postDelayed(mRunnable, 1000);
     }
  }

 private TextView findViewById(int speed) {=
      return null;
  }

private final Runnable mRunnable = new Runnable() {
     public void run() {
      TextView RX = (TextView)findViewById(R.id.RX);
      TextView TX = (TextView)findViewById(R.id.TX);
      long rxBytes = TrafficStats.getTotalRxBytes()- mStartRX;
      RX.setText(Long.toString(rxBytes));
      long txBytes = TrafficStats.getTotalTxBytes()- mStartTX;
      TX.setText(Long.toString(txBytes));
      mHandler.postDelayed(mRunnable, 1000);
      ParseObject testObject = new ParseObject("TestObject");
      testObject.put("DataOut", String.valueOf(txBytes));
      testObject.put("DataIn", String.valueOf(mStartRX));
      testObject.put("DataRSSI", String.valueOf(textRssi));
      testObject.put("DataSpeed", String.valueOf(textSpeed));
      testObject.saveInBackground();

      final Chronometer myChronometer = (Chronometer)findViewById(R.id.chronometer);
      myChronometer.start();

      DisplayWifiState();
      this.registerReceiver(this.myWifiReceiver, new IntentFilter(
              ConnectivityManager.CONNECTIVITY_ACTION));
  }

  private Chronometer findViewById(int chronometer) {
      return null;
  }

  private void registerReceiver(BroadcastReceiver myWifiReceiver2,
              IntentFilter intentFilter) {
      }

      private BroadcastReceiver myWifiReceiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context arg0, Intent arg1) {
          NetworkInfo networkInfo = (NetworkInfo) arg1
                  .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
          if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
              DisplayWifiState();
          }
      }
  };

  public void DisplayWifiState() {

      ConnectivityManager myConnManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
      NetworkInfo myNetworkInfo = myConnManager
              .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();

      if (myNetworkInfo.isConnected()) {
          textSsid.setText(myWifiInfo.getSSID());
          textSpeed.setText(String.valueOf(myWifiInfo.getLinkSpeed()) + " "
                  + WifiInfo.LINK_SPEED_UNITS);
          textRssi.setText(String.valueOf(myWifiInfo.getRssi()));
      } else {
          textSsid.setText("---");
          textSpeed.setText("---");
          textRssi.setText("---");
         }}};

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
        Log.d(TAG, "starting service");
        ParseObject testObject = new ParseObject("TestObject");
      testObject.put("DataOut", String.valueOf(txBytes));
      testObject.put("DataIn", String.valueOf(mStartRX));
      testObject.put("DataRSSI", String.valueOf(textRssi));
      testObject.put("DataSpeed", String.valueOf(textSpeed));
      testObject.saveInBackground();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

数据以下列方式出现在服务器中:

DataIn: Appears as 0 every 12 seconds
DataOut Appears as 0 every 12 seconds
DataRSSI Appears as null every 12 seconds
DataSpeed Appears as null every 12 seconds
4

1 回答 1

0

我相信您需要在 Runnable 的 run() 方法中添加对 ParseObject.saveXX() 的另一个调用。

onStartCommand() 仅在服务启动时运行,而不是在可运行对象的每次“迭代”之后运行

于 2013-04-15T01:20:52.367 回答