2013-04-12 48 views
0

我已经建立了成功地使用下面的数据上传到parse.com带宽监控应用:解析和存储数据的实时

testObject.put("dataOutput", String.valueOf(mStartTX)); 

但是数据始终存储为零。其原因是我发送IS为0的长/字符串的初始值(当应用程序第一次启动时),但是随着用户开始发送和接收数据,它不断变化。这个数据可以实时解析吗?或者随着数据变化每隔几秒就会怨恨?

完整的源:

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

回答

0

你会希望创建一个,而你的应用程序正在运行(或者甚至在关闭时)运行的IntentService。使用AlarmService收集您的数据,以便在集合迭代中唤醒以收集数据。 AlarmService被设计为电池友好型。

private final Runnable mRunnable = new Runnable() { // ... } 

Runnable在Java中是一个好主意;与Android不同的是,如果您只需要关闭UI线程,请考虑使用AsyncTask()或类似工具 - 但是,如上所述,后台服务可能是更好的选择。我不知道你对Java或命名约定的熟悉程度如此,也许这只是一个简单的疏忽,但在您的代码中mRunnable应该只是runnable。带有“m”或“_”(下划线)前缀的变量是类似于您的mHandler,mStartTXmStartRX的字段。

另外,独奏线Long.toString(mStartTX);什么都不做。

+0

我对这一切都非常陌生。我刚刚阅读了以下教程:http://www.vogella.com/articles/AndroidServices/article.html,我想我理解一般概念,但在此场景中实现它的确切方式 - 我不知道... –

+0

像这样看起来正确吗?Intent intent = new Intent(this,MyService.class); PendingIntent pintent = PendingIntent.getService(this,0,intent,0); AlarmManager alarm =(AlarmManager)getSystemService(Context.ALARM_SERVICE); alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),1 * 1000,pintent); –

+0

它工作吗? ;)有很多方法可以实现我指出的目标。 –