android service和activity的通讯
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的通讯的主要内容,如果未能解决你的问题,请参考以下文章
(Android数据传递)Service和Activity之间-- 借助BroadcastReceiver--的数据传递
Android service与activity绑定调用问题