我有应用程序,在该应用程序中,我会在特定时间内刷新该应用程序。
每次将呼叫提供给Web服务,并且数据库中的所有消息均加载到listview。
其完成如下:
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);
}
}
}
现在,我只是无法检测到(突出显示)我的应用程序中的新消息,因为每次调用Webservice时,它都会检索所有消息。
在此特定时间间隔内,我应如何检测到这是新消息(或可以说成listview中的数据)。
请帮我。
最佳答案
在数据库中添加一个字段(即状态)。当您调用服务并从数据库返回所有消息时,状态需要更改为1(0表示仍未获取Webservice,1表示在android端获取)。因此,在插入新记录后,您的服务仅获取状态为0的记录。
我希望这能帮到您。