Java 实时解析和存储数据

Java 实时解析和存储数据,java,android,parsing,background-process,Java,Android,Parsing,Background Process,我已经构建了一个带宽监视器应用程序,它使用以下方法成功地将数据上传到parse.com: testObject.put("dataOutput", String.valueOf(mStartTX)); 但是,数据始终存储为零。原因是我发送的长/字符串的初始值为零(当应用程序第一次启动时),但随着用户开始发送和接收数据,它会不断变化。这些数据可以实时解析吗?或者在数据更改时每隔几秒钟重新发送一次 完整资料来源: public class ParseStarterProjectActivit

我已经构建了一个带宽监视器应用程序,它使用以下方法成功地将数据上传到parse.com:

   testObject.put("dataOutput", String.valueOf(mStartTX));
但是,数据始终存储为零。原因是我发送的长/字符串的初始值为零(当应用程序第一次启动时),但随着用户开始发送和接收数据,它会不断变化。这些数据可以实时解析吗?或者在数据更改时每隔几秒钟重新发送一次

完整资料来源:

public class ParseStarterProjectActivity extends Activity {
TextView textSsid, textSpeed, textRssi;

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


/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parser);
textSsid = (TextView) findViewById(R.id.Ssid);
textSpeed = (TextView) findViewById(R.id.Speed);
textRssi = (TextView) findViewById(R.id.Rssi);
Long.toString(mStartTX);
ParseAnalytics.trackAppOpened(getIntent());
ParseObject testObject = new ParseObject("TestObject");
testObject.put("dataOutput", String.valueOf(mStartTX));
testObject.saveInBackground();


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 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);

您需要创建一个IntentService,该服务在您的应用程序运行时(或者甚至在关闭时)运行。使用AlarmService收集数据,以便它在设置的迭代中唤醒以收集数据。AlarmService设计为电池友好型

private final Runnable mRunnable = new Runnable() { // ... }
Runnable在Java中是一个好主意;在安卓系统中没有这么多。如果你只需要从UI线程中考虑一个AsiNCTASK()或类似的东西,但是,如上所述,后台服务可能是一个更好的选择。我不知道您对Java或命名约定的熟悉程度,所以这可能只是一个简单的疏忽,但是
mRunnable
应该只是
runnable
在您的代码中。前缀为“m”或“ux”(下划线)的变量是类似于
mHandler
mstartx
mStartRX
的字段


此外,单独的行
Long.toString(mStartTX)什么都不做。

我对这一切都是陌生的。我刚刚阅读了以下教程:我想我理解了一般概念,但在这个场景中实现它的确切方式——我不知道……类似的内容看起来正确吗?Intent Intent=new Intent(这个,MyService.class);pendingent pintent=pendingent.getService(this,0,intent,0);AlarmManager alarm=(AlarmManager)getSystemService(Context.alarm\u服务);alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),1*1000,pintent);它有用吗?;)你有很多方法可以实现我给你指出的。否定。(我希望如此!)正如你已经指出的。答案就在那里。我保证。