2013-08-26 29 views
1

我有應用程序,在這個應用程序中,我在特定的時間內刷新該應用程序。當給web服務打電話時檢測新數據

每次調用Web服務時,數據庫中的所有消息都被加載到列表視圖。

其進行如下:

public class Messages extends Activity { 


    protected Handler handler = new Handler(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_messages); 

     Intent intent = getIntent(); 

     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
     String id = intent.getStringExtra(MainActivity.EXTRA_ID); 
     String[] lst = null; 
     ListView lm=(ListView)findViewById(R.id.listView1); 
     TextView tv = (TextView)findViewById(R.id.textView1); 

     tv.setText("Welcome " + message); 

     handler.postDelayed(new UpdateTask(),750); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.messages, menu); 
     return true; 
    } 

    class UpdateTask implements Runnable { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

      setContentView(R.layout.activity_messages); 

      Intent intent = getIntent(); 

      String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
      String id = intent.getStringExtra(MainActivity.EXTRA_ID); 
      String[] lst = null; 
      ListView lm=(ListView)findViewById(R.id.listView1); 
      TextView tv = (TextView)findViewById(R.id.textView1); 

      tv.setText("Welcome " + message); 

      CallSoap cs=new CallSoap(); 

      lst=cs.GetMessage(id); 

      ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst); 

      lm.setAdapter(adpt); 

      handler.postDelayed(this, 500); 
     } 
    } 
} 

現在,我只是無法檢測到(高亮)在我的應用程序的新郵件,因爲當電話是給web服務它每一次檢索的所有消息。

我應該如何檢測這是新消息(或者可以說是作爲listview中的數據)在這個特定的時間間隔。

請幫幫我。

+1

PrashantAdesara的答案是正確的。還有,U可以去報警經理概念/通知管理器來通知用戶有新郵件 – Ruban

回答

1

在數據庫中添加一個字段(即狀態)。當你調用你的服務並返回所有來自數據庫的消息時,狀態需要改變1(0意味着web服務不能獲取,1表示在android端獲取)。因此,在新記錄插入您的服務後,只能獲取狀態爲0的記錄。

我希望這會對您有所幫助。

+0

我希望所有的記錄,但強調用紅色新 –

+1

好吧,那麼你可以管理在Android相同的狀態概念側。 – PrashantAdesara