android线程操作类(暂停、重新开启、停止)

来源:互联网 发布:邢台网络推广招聘 编辑:程序博客网 时间:2024/06/08 15:23
场景:
在程序中如果需要在后台长时间做一件事情,比如联网获取数据等操作,就要用到线程。
但为了提高用户体验,有以下几点需要注意:
1、程序可见时线程开始运行;
2、程序不可见时线程暂停;
3、程序退出时停止线程;

以下根据我自己的程序提出一个公用的代码,大家可以把自己的业务逻辑套进去:

Java代码  收藏代码
  1. public class NetUtil2 extends Thread {  
  2.   
  3.     //NewsBrief为新闻实体类;  
  4.     private List<NewsBrief> loopList = new ArrayList<NewsBrief>();  
  5.       
  6.     private boolean isClose = false;  
  7.       
  8.     //IObtainData为一个接口,因为很多程序在用,因此拿Map存储;  
  9.     private Map<Integer, IObtainData> obtainMap = new HashMap<Integer, IObtainData>();  
  10.     private int currentPage = 0;  
  11.     private boolean isPause = false;  
  12.   
  13.     private NetUtil2() {  
  14.     }  
  15.   
  16.     private static NetUtil2 single = null;  
  17.   
  18.     public synchronized static NetUtil2 getInstance() {  
  19.         if (single == null) {  
  20.             single = new NetUtil2();  
  21.         }  
  22.         return single;  
  23.     }  
  24.   
  25.     public synchronized void addNewsBrief(NewsBrief newsBrief) {  
  26.         loopList.add(newsBrief);  
  27.         //有数据要处理时唤醒线程  
  28.         this.notify();  
  29.     }  
  30.   
  31.     public void setObtainDataListener(int channelId, IObtainData iod) {  
  32.         //添加回调  
  33.         if (!obtainMap.containsKey(channelId)) {  
  34.             obtainMap.put(channelId, iod);  
  35.         }  
  36.     }  
  37.   
  38.     public void setCurrentPage(int page) {  
  39.         currentPage = page;  
  40.     }  
  41.   
  42.     /** 
  43.      * 暂停线程 
  44.      */  
  45.     public synchronized void onThreadPause() {  
  46.         isPause = true;  
  47.     }  
  48.   
  49.     /** 
  50.      * 线程等待,不提供给外部调用 
  51.      */  
  52.     private void onThreadWait() {  
  53.         try {  
  54.             synchronized (this) {  
  55.                 this.wait();  
  56.             }  
  57.         } catch (Exception e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * 线程继续运行 
  64.      */  
  65.     public synchronized void onThreadResume() {  
  66.         isPause = false;  
  67.         this.notify();  
  68.     }  
  69.   
  70.     /** 
  71.      * 关闭线程 
  72.      */  
  73.     public synchronized void closeThread() {  
  74.         try {  
  75.             notify();  
  76.             setClose(true);  
  77.             interrupt();  
  78.         } catch (Exception e) {  
  79.             e.printStackTrace();  
  80.         }  
  81.     }  
  82.   
  83.     public boolean isClose() {  
  84.         return isClose;  
  85.     }  
  86.   
  87.     public void setClose(boolean isClose) {  
  88.         this.isClose = isClose;  
  89.     }  
  90.   
  91.     @Override  
  92.     public void run() {  
  93.         while (!isClose && !isInterrupted()) {  
  94.             if (loopList.size() > 0 && !isPause) {  
  95.                 int index = 0;  
  96.                 NewsBrief newsBrief = null;  
  97.                 // 当前页面优先加载  
  98.                 for (int i = 0; i < loopList.size(); i++) {  
  99.                     if (loopList.get(i).getChannelId() == currentPage) {  
  100.                         newsBrief = loopList.get(i);  
  101.                         index = i;  
  102.                         break;  
  103.                     }  
  104.                 }  
  105.                 if (null == newsBrief) {  
  106.                     newsBrief = loopList.get(0);  
  107.                 }  
  108.                 String reslut = getNewsContent(newsBrief);  
  109.   
  110.                 if (!"-1".equals(reslut)) {  
  111.                     // 获取成功,更新  
  112.                     if (obtainMap.containsKey(newsBrief.getChannelId())) {  
  113.                         obtainMap.get(newsBrief.getChannelId())  
  114.                                 .updateNewsBrief(newsBrief);  
  115.                     }  
  116.                     synchronized (loopList) {  
  117.                         loopList.remove(index);  
  118.                     }  
  119.                 } else {  
  120.                     //获取失败,移至队尾  
  121.                     synchronized (loopList) {  
  122.                         NewsBrief nb = loopList.remove(index);  
  123.                         loopList.add(nb);  
  124.                     }  
  125.                 }  
  126.                 try {  
  127.                     Thread.sleep(300);  
  128.                 } catch (InterruptedException e) {  
  129.                     e.printStackTrace();  
  130.                 }  
  131.             } else {  
  132.                 onThreadWait();  
  133.             }  
  134.         }  
  135.     }  
  136. }  


注意:
线程的暂停用isPause控制,说白了其实就是不让线程进入wait状态;
0 0
原创粉丝点击