Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android service和activity的通訊

android service和activity的通訊

編輯:關於Android編程

我們需要用以下方式來啟動service:

		Intent intent = new Intent();
		intent.setClass(MainActivity.this, MyService.class);
		bindService(intent,conn,BIND_AUTO_CREATE);


	ServiceConnection conn = new ServiceConnection() {
		
		@Override
		public void onServiceDisconnected(ComponentName name) {
		}
		
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mService = ((MyService.MyBinder)service).getService();  
		}
	};
	


這時service會執行:onCreate和onBind


然後


我們去執行一些任務:

	ImageView item_image = (ImageView)findViewById(R.id.item_image);
		item_image.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mService.setHandler(mHandler);
			   mService.startCheckIfUpgrade();
			}
		});


	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GET_UPGRADE_INFORMATION_COMPLETE:
				System.out.println("=======notify need upgrade");
				AlertDialog dialog = getCustomDialog();
				dialog.show();
			default:
				break;
			}
		}
	};

Service的代碼是:

public class MyService extends Service {

	/** thread to execute background task*/
	private Thread mThread = null;
	
	/** flag indicates if the service has been stopped*/
	private boolean mStopFlag = false;
	
	private static final int GET_UPGRADE_INFORMATION_COMPLETE = 202;
	
	/** Handler to get notify from thread*/
	private Handler mHandler = null;
	
	public void setHandler(Handler handler) {
		this.mHandler = handler;
	}
	
	
	@Override
	public boolean onUnbind(Intent intent) {
		System.out.println("====service onUnbind");
		return super.onUnbind(intent);
	}

	public class MyBinder extends Binder {
		/**
		 * 獲取當前Service的實例
		 * 
		 * @return
		 */
		public MyService getService() {
			return MyService.this;
		}
	}
	
	public void startCheckIfUpgrade() {
		if(mThread == null){
			mThread = new UpgradeThread();
			mThread.start();
		}
	}
	   
	@Override
	public IBinder onBind(Intent arg0) {
		System.out.println("====service onBind");
		
		return new MyBinder();
	}
	
	/**
	 * @see android.app.Service#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("====service onCreate");
	}

	private class UpgradeThread extends Thread{

		/**
		 * @see java.lang.Thread#run()
		 */
		@Override
		public void run() {
			super.run();
			//在這裡寫去服務端查看是否需要升級的邏輯
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			boolean ret = true;
			if(ret && !mStopFlag){
				Message msg = new Message();
				msg.what = GET_UPGRADE_INFORMATION_COMPLETE;
				mHandler.sendMessage(msg);
			}
		}
		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Service#onDestroy()
	 */
	@Override
	public void onDestroy() {
		System.out.println("====ondestroy");
		mStopFlag = true;
		try {
			if (null != mThread && mThread.isAlive()) {
				try
				{
					mThread.stop();
				}
				catch (Exception e)
				{
				}
			}
			mThread = null;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
		System.gc();
	}

}

當activity銷毀後Service會執行:

onUnbind和onDestroy

這個說明這個service的生命周期和activity是一致的

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