【问题标题】:Android thread Loop not workAndroid线程循环不起作用
【发布时间】:2017-10-08 00:45:02
【问题描述】:

我在 Runnable 的 run 函数中使用了 Looper.prepare 和 Looper.loop。但问题是线程根本不循环,Runnable 只运行一次。在 Activity1 中,我使用了三个 Runnable 线程,都是循环的。两个线程通过“while”循环不断从网络获取数据和图片(不需要更新UI),一个线程通过“循环”不断从本地sqlite中选择数据和图片。数据为:

protected void onCreate(Bundle savedInstanceState) {
......
new Thread(getMessageTask).start();
        getMessageHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                i++;
                System.out.println("niuanmata" + i);    //one appear the first one
                try {

                    ArrayList<Map<String, String>> listMessages = (ArrayList<Map<String, String>>)msg.obj;
                    boolean listchange = true;

                    if (oldMessages.size() != 0) {
                        if (listMessages.size() == oldMessages.size()) {
                            for (int i = 0; i < listMessages.size(); i++) {
                                Map<String, String> oldmessage = (Map<String, String>) oldMessages.get(i);
                                Map<String, String> newmessage = (Map<String, String>) listMessages.get(i);
                                if ((oldmessage.get("mID") != newmessage.get("mID")) || (oldmessage.get("mainContent") != newmessage.get("mainContent")) || (oldmessage.get("deadLine") != newmessage.get("deadLine"))) {
                                    break;
                                }
                                if (i == (listMessages.size() - 1)) {
                                    listchange = false;
                                }
                            }
                        }
                    }
                    if (listchange) {
                        SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, listMessages, R.layout.layout_invites,
                                new String[]{"mID", "creater", "mainContent", "deadLine", "mtype", "createrLogo"},
                                new int[]{R.id.tv_list_type, R.id.tv_list_name, R.id.tv_list_inviteword, R.id.tv_list_invitedate, R.id.tv_list_inviteid, R.id.iv_list_logo});
                        lvMessage.setAdapter(adapter);
                        oldMessages = listMessages;
                    }
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "wrong: " + e.toString(), Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        };

......
lvMessage.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {    //when creater click, update the message; when others click, reset the alarm

                Toast.makeText(MainActivity.this, "ok" , Toast.LENGTH_SHORT).show();
            }
        });
}

.........
Runnable synchroDataTask = new Runnable() {
        @Override
        public synchronized void run() {
            //data syschno
            while (IOHelper.loopjudge()) {
                 {
                     AccountsDB adb = new AccountsDB(MainActivity.this);
                     String thelastupdate = adb.getLastUpdate(account.getChatNO());     

                     Calendar calendar = IOHelper.StringToCalendar(thelastupdate);
                     calendar.add(Calendar.MINUTE, -30);   
                     String accountData = synchroDataWebservice(account.getChatNO(), IOHelper.CalendarToString(calendar));   //get the datas of the account synchroly

                     AccountBLL.saveDBofWebString(accountData, MainActivity.this, account);    //use static method to save the DB string as SQLite data
                }
            }
.........

@Override
        public synchronized void run() {
            while (IOHelper.loopjudge()) {
......
}


.......
Runnable getMessageTask = new Runnable() {
        @Override
        public synchronized void run() {
            Looper.prepare();
            //while (IOHelper.loopjudge() && (!stopThread)) {
                MessageDB messagedb = new MessageDB(MainActivity.this);
                List<MessageMain> messages = messagedb.getMessageByChatNO(account.getChatNO());
                ArrayList<Map<String, String>> listMessages = setMessaageListToMap(messages);
                Message msg = Message.obtain();
                msg.obj = listMessages;
                getMessageHandler.sendMessageDelayed(msg, 1000);
            //}
            Looper.loop();
        }
    };
......

在我有限的android经验中,我使用while来做getMessageTask中的循环,因为数据和UI的listview需要不断更新。但是无法单击列表视图。然后改成Looper,但是UI的listview不能不断更新....

【问题讨论】:

  • 根据 db 的数据,需要不断更新 UI 上的列表视图。但不工作。更重要的是,在我将 Runnable 的循环从 while 更改为 Looper 之后,UI 偶尔会突然转向另一个活动。提前致谢。

标签: android multithreading loops listview


【解决方案1】:

答案是我误解了 Looper 的意思,认为 Looper.prepare() 和 Looper.loop() 是 while() 循环,然后就犯了错误。 Looper.prepare() 和 Looper.loop() 只是表示这个线程可以循环,但是我必须自己写while循环或者for循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多