スレッドメソッド

3458 ワード

 Thread+handler , 


 final int PROGRESS_DIALOG = 0;
	ProgressThread progressThread;
	ProgressDialog progressDialog;
	Activity activity;
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			progressDialog = new ProgressDialog(activity);
			progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progressDialog.setMessage(" ...");
			progressDialog.setCancelable(true);
			progressThread = new ProgressThread(handler);
			progressThread.start();
			return progressDialog;
		default:
			return null;
		}
	}

	private class ProgressThread extends Thread {
		//  Handler (UI ), Message ,
		Handler mHandler;

		ProgressThread(Handler h) {
			mHandler = h;
		}

		public void run() {
			String buffer = "";

		

			Message msg = mHandler.obtainMessage();
			Bundle b = new Bundle();
			b.putString("buffer", buffer);
			msg.setData(b);
			mHandler.sendMessage(msg);
		}
	}

	final Handler handler = new Handler() {
		
		public void handleMessage(Message msg) {
			//  
			String buffer = msg.getData().getString("buffer");
			activity.removeDialog(PROGRESS_DIALOG);

		}
		
	};
  :AsynTask, :




@Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        MyAsyTast asytast =new MyAsyTast();  
        asytast.execute();  
    }  
      
      
    private class MyAsyTast extends AsyncTask<Integer, String, String>{  
        private ProgressDialog dialog;  
          
        @Override  
        protected String doInBackground(Integer... params) {  
            if (isCancelled()) {  
                return null;  
            }  
              
            // ,   
              
            return null;  
        }  
  
  
        @Override  
        protected void onPostExecute(String result) {  
            super.onPostExecute(result);  
            if (GrouponOrderCheck01.this.isFinishing()) {  
                return ;  
            }  
            if (dialog!=null) {  
                dialog.cancel();  
            }  
            if (isCancelled()) {  
                return;  
            }  
              
              
            //   
        }  
          
        @Override  
        protected void onPreExecute() {  
            super.onPreExecute();  
            dialog =DialogHelper.createProgressDialog(GrouponOrderCheck01.this);  
        }  
          
        @Override  
        protected void onProgressUpdate(String... values) {  
            super.onProgressUpdate(values);  
              
            if (isCancelled()) {  
                return;  
            }  
        }  
          
          
    }