【Android基础知识】【android多线程handler】

handler机制不需要明白太多,我认为可以参照例子来实现,每次使用的时候就用一下例子。

既有线程,又有mhandler对接收到的消息的处理。

package com.hengtiansoft.sportnow.news.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.hengtiansoft.sportnow.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hengtiansoft.sportnow.news.adapter.MainListViewAdapter;
import com.hengtiansoft.sportnow.news.app.NewsAppContext;
import com.hengtiansoft.sportnow.news.app.NewsAppException;
import com.hengtiansoft.sportnow.news.bean.News;
import com.hengtiansoft.sportnow.news.bean.NewsList;
import com.hengtiansoft.sportnow.news.common.UIHelper;

public class NewsActivity extends Activity {
    private PullToRefreshListView listview;
    private List<News> newsList;
    private NewsAppContext appContext;// 全局Context
    private MainListViewAdapter listViewAdapter;
    private ProgressDialog selectorDialog;
    private Button bt_news, bt_forum, bt_cartype, bt_favour, bt_more;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        appContext = (NewsAppContext) getApplication();
        // 网络连接判断
        if (!appContext.isNetworkConnected())
            UIHelper.ToastMessage(this, R.string.network_not_connected);
        initData();
    }

    
    //变量
    Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            selectorDialog.cancel();
            if (msg.what == 1) {
                newsList = (List<News>) msg.obj;
                listViewAdapter = new MainListViewAdapter(NewsActivity.this,
                        newsList);
                listview.setAdapter(listViewAdapter);
            } else if (msg.what == -1) {
                UIHelper.ToastMessage(NewsActivity.this, "没有数据");
            } else if (msg.what == -2) {
                UIHelper.ToastMessage(NewsActivity.this,
                        R.string.xml_parser_failed);
            }
        }
    };

    private void initData() {

        selectorDialog.show();
        new Thread() {
            public void run() {
                Message msg = new Message();
                boolean isRefresh = false;
                try {
                    NewsList list = appContext.getNewsList();
                    if (list.getNewsCount() > 0) {
                        msg.what = 1;
                        msg.obj = list.getNewslist();
                        appContext.saveObject(list, "newslist_");
                    } else {
                        msg.what = -1;
                    }
                } catch (NewsAppException e) {
                    e.printStackTrace();
                    msg.what = -2;
                    msg.obj = e;
                }
                mHandler.sendMessage(msg);
            }
        }.start();
    }
}

 

posted @ 2014-12-21 17:21  比企谷雪乃  阅读(189)  评论(0编辑  收藏  举报