Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android_BlockingQueue_並發線程

Android_BlockingQueue_並發線程

編輯:關於Android編程

 
/**Service提供的方法*/
public interface IMyBinder {
	public void add(Bean bean);
}
/** Service,主要處理網絡耗時等操作 */
public class MyService extends Service {

	/** 主要用handler傳遞一個操作 */
	private Handler handler = new Handler(Looper.getMainLooper());// 默認為MainLooper
	//
	private BlockingQueue queue;
	private WorkerThread thread;

	@Override
	public void onCreate() {
		queue = new ArrayBlockingQueue(5);
		thread = new WorkerThread();
		//開啟線程
		thread.start();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	@Override
	public void onDestroy() {
		if (thread != null && !thread.isInterrupted()) {
			thread.quit();
		}
		super.onDestroy();
	}

	public interface DoWorkCallback {
		public void onUpdateProgress(int progress);
		public void onUpdateCompleted();
	}

	public class MyBinder extends Binder implements IMyBinder {
		@Override
		public void add(Bean bean) {
			queue.add(bean);
		}
	}

	private class WorkerThread extends Thread {
		private boolean isQuited = false;

		@Override
		public void run() {
			while (true) {
				Bean bean = null;
				try {
					//該方法可能是阻塞的
					bean = queue.take();
					if (bean != null) {
						//傳遞一個操作,有MainLooper執行該runnable的run方法
						handler.post(bean.runnable);
					}
				} catch (InterruptedException e) {
					if (!isQuited) {
						continue;
					} else {
						return;
					}
				}
			}
		}

		//退出線程
		public void quit() {
			isQuited = true;
			queue.clear();
			interrupt();
		}
	}
}
public class MainActivity extends Activity {

	private ProgressBar bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		bar = (ProgressBar) findViewById(R.id.bar);
	}

	public void click(View view) {
		Intent intent = new Intent(this, MyService.class);
		bindService(intent, connection, BIND_AUTO_CREATE);
	}

	private ServiceConnection connection = new ServiceConnection() {

		/**
		 * 綁定完成,調用該方法,該方法運行在主線程中
		 */
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			IMyBinder binder = (IMyBinder)service;
			System.out.println(onServiceConnected:+Thread.currentThread());
			//doWork 接收一個url String 和一個回調接口,主要用於更新ui
			Bean bean = new Bean();
			bean.runnable = new Runnable() {
				
				@Override
				public void run() {
					//TODO:耗時操作
				}
			};
			bean.callback = new DoWorkCallback() {
				
				@Override
				public void onUpdateProgress(int progress) {
					
				}
				
				@Override
				public void onUpdateCompleted() {
					
				}
			};
			binder.add(bean);
		}
		@Override
		public void onServiceDisconnected(ComponentName name) {

		}
	};
}
public class Bean {
	public Runnable runnable;
	public DoWorkCallback callback;

}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved