android service和activity的通讯

我们需要用以下方式来启动service:

</pre><pre name="code" class="java"><span style="white-space:pre">		</span>Intent intent = new Intent();
		intent.setClass(MainActivity.this, MyService.class);
		<strong>bindService(intent,conn,BIND_AUTO_CREATE);</strong>


	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) {
				<strong>mService.setHandler(mHandler);
			   mService.startCheckIfUpgrade();</strong>
			}
		});


	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是一致的

android service和activity的通讯,布布扣,bubuko.com

android service和activity的通讯

上一篇:Microsoft Azure Remoteapp预览


下一篇:Android 适配器教程(一)