2013-04-12 37 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

它工作嗎? ;)有很多方法可以實現我指出的目標。 –