zoukankan      html  css  js  c++  java
  • 异步任务判断服务器是否开启

        在判断是否有无线网络时,UI界面假死,这时我们需要使用AsyncTask。代码:

     1 /** 异步任务 :判断服务器开启才取后台数据
     2   * 2014-7-31
     3   * @author 吴chunyuan
     4   *
     5   */
     6  public class MyTask extends AsyncTask<String, Integer, String> {                
     7      //onPreExecute方法用于在执行后台任务前做一些UI操作                           
     8      @Override                                                                    
     9      protected void onPreExecute() {                                              
    10          Log.i(TAG, "onPreExecute() called");  
    11            
    12      }                                                                            
    13                                                                                   
    14      //doInBackground方法内部执行后台任务,不可在此方法内修改UI                    
    15      @Override                                                                    
    16      protected String doInBackground(String... params) { 
    17       //totalText = (TextView) findViewById(R.id.total);
    18          Log.i(TAG, "doInBackground(Params... params) called");                   
    19        
    20          try{
    21     Socket socket2  =new Socket();
    22     //socket2.connect(new  InetSocketAddress(SERVER_ADDRESS, SERVER_PORT));
    23     //设置连接超时 10s
    24     socket2.connect(new InetSocketAddress(ServerIP, SERVER_PORT),10000);
    25      socket2.close(); 
    26      
    27     return  String.valueOf(1)+"|"+params[0];
    28     
    29           } catch (Exception e) {                                                  
    30              Log.e(TAG, e.getMessage());   
    31              return  String.valueOf(0);
    32           }  
    33                                                             
    34      }                                                                       
    35                                                                              
    36      //onProgressUpdate方法用于更新进度信息                                  
    37      @Override                                                               
    38      protected void onProgressUpdate(Integer... progresses) {                
    39          Log.i(TAG, "onProgressUpdate(Progress... progresses) called");      
    40     
    41      }                                                                       
    42                                                                              
    43            
    44     // String result 来自doInBackground 
    45      @Override                                                               
    46      protected void onPostExecute(String result) {                           
    47          Log.i(TAG, "onPostExecute(Result result) called");
    48          String [] sa =result.split("\\|");
    49             if(result.equals("0")){
    50              MyUtil.showToast(MenuActivity.this, "无法连接服务器");  
    51                }else //网络正常
    52 
    53           {
    54 
    55             //网络正常,执行任务......
    56 
    57           }
    58           }                                                                       
    59                                                                           
    60      //onCancelled方法用于在取消执行中的任务时更改UI                         
    61      @Override                                                               
    62      protected void onCancelled() {                                          
    63          Log.i(TAG, "onCancelled() called");                                 
    64       }  
    65  }
     
  • 相关阅读:
    LeetCode Power of Three
    LeetCode Nim Game
    LeetCode,ugly number
    LeetCode Binary Tree Paths
    LeetCode Word Pattern
    LeetCode Bulls and Cows
    LeeCode Odd Even Linked List
    LeetCode twoSum
    549. Binary Tree Longest Consecutive Sequence II
    113. Path Sum II
  • 原文地址:https://www.cnblogs.com/realhope/p/4258986.html
Copyright © 2011-2022 走看看