• 【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();
        }
    }
  • 相关阅读:
    Python批量爬取唯美类图片
    查找Windows启动程序
    在Windows10中进行蓝屏排查
    在Windows 10中应该记住的完整的多指触摸板手势
    通过命令行工具远程注销用户
    Windows 7,8.1和10中的隐藏退出资源管理器选项
    找出正在使用的全局热键
    windows10中创建环境变量
    mysql高级(锁机制)
    mysql高级(查询截取分析)
  • 原文地址:https://www.cnblogs.com/hikigaya-yukino/p/4176794.html
Copyright © 2020-2023  润新知