비동기 작업 AsyncTask 의 데모

4482 단어 androidAsyncTask
   1.   , UI            ,         ,        
   2.      AsyncTask  ,  :
public class MainActivity extends Activity {

    
    private final String PATH = "http://192.168.0.116:8080/Android_Server/res/1.wmv";
    private HttpURLConnection conn;
    private int max;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            URL url = new URL(PATH);
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5 * 1000);
            conn.setRequestProperty("Connection", "Keep-Alive");
            //       
            max = conn.getContentLength();
            myTask task = new myTask();
            /*
             *   AsyncTask doInBackground  
             *    url    
             * 
             */
            task.execute(url);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        
    }
    
    /*
     *                      
     *              
     *                     
     * 
     */
    public class myTask extends AsyncTask<URL,Integer,String>{
        
        private ProgressDialog proDialog;
        private int hasRead = 0;
        
        //AsyncTask   
        @Override
        protected String doInBackground(URL... params) {
            // TODO Auto-generated method stub
            try {
                InputStream is = conn.getInputStream();
                while(is.read()!=-1)
                {
                    hasRead++;
                    /*
                     *   AsyncTask  onProgressUpdate  
                     */
                    publishProgress(hasRead);
                }
                is.close();
                return "success";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
        
        /*
         *               (non-Javadoc)
         * doInBackground             
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            proDialog.dismiss();
        }
        
        //            
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            proDialog = new ProgressDialog(MainActivity.this);
            proDialog.setTitle("     ");
            proDialog.setIcon(R.drawable.ic_launcher);
            proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            /*
             *          
             * 
             */
            proDialog.setMax(max);
            proDialog.show();
        }
        
        /*
         *      
         *  doInBackground     publishProgress        
         * 
         */
        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            
            setTitle(values[0]+"");
            proDialog.setProgress(values[0]);
        }
        
    }
}

  3.  Demo         ,                      ,          ..
          ,          ,   HttpURLConnection  getContentLength()  ..


좋은 웹페이지 즐겨찾기