【问题标题】:Detect new data when call is given to webservice调用 web 服务时检测新数据
【发布时间】:2013-08-26 07:22:44
【问题描述】:

我有一个应用程序,我会在特定时间内刷新该应用程序。

每次调用webservice,数据库中的所有消息都会加载到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);
        }
    }
}

现在,我无法在我的应用中检测(突出显示)新消息,因为每次调用 web 服务时,它都会检索所有消息。

我应该如何在这个特定的时间间隔内检测到这是新消息(或者可以说是 listview 中的数据)。

请帮帮我。

【问题讨论】:

  • PrashantAdesara 的回答是正确的。另外,您可以使用警报管理器概念/通知管理器来通知用户新消息

标签: android android-layout android-intent android-listview


【解决方案1】:

在数据库中添加一个字段(即状态)。当您调用您的服务并从数据库返回所有消息时,状态需要更改为 1(0 表示仍然没有获取 web 服务,1 表示它在 android 端获取)。因此,在插入新记录后,您的服务仅获取状态为 0 的记录。

希望对你有帮助。

【讨论】:

  • 我想要所有记录,但用红色突出显示新记录
猜你喜欢
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多