{"id":42335,"date":"2023-12-11T14:56:15","date_gmt":"2023-12-11T06:56:15","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=42335"},"modified":"2023-12-11T14:56:15","modified_gmt":"2023-12-11T06:56:15","slug":"android-activity-%e6%80%8e%e4%b9%88%e5%92%8cservice-%e7%bb%91%e5%ae%9a%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/android-activity-%e6%80%8e%e4%b9%88%e5%92%8cservice-%e7%bb%91%e5%ae%9a%ef%bc%9f\/","title":{"rendered":"Android Activity \u600e\u4e48\u548cService \u7ed1\u5b9a\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u8fd9\u9700\u8981\u5b9e\u73b0service\u4e2d\u7684onBind()\u51fd\u6570\u4ee5\u8fd4\u56deservice\u5b9e\u4f8b\u7ed9activity<\/p>\n<p>1\u3001\u521b\u5efaservice\u7c7b\u548cactivity\u7c7b\u3002<\/p>\n<p>2\u3001\u5728service\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u5185\u90e8\u7c7b\u7ee7\u627f\u81eaBinder()\u7c7b\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;public class MyBinder extends Binder{<br \/>\n     public Service1 getService(){<br \/>\n          return Service1.this;<br \/>\n     }<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u5b9e\u4f8b\u5316onBind()\u65b9\u6cd5\uff1a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;private final IBinder binder = new MyyBinder();<br \/>\n@Override<br \/>\npublic IBinder onBind(Intent intent){<br \/>\n    Log.i(LOG\uff0c&quot;&quot;onBind&#8230;&#8230;&quot;&quot;);<br \/>\n    return binder;<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>3\u3001\u5728activity\u4e2d\u5b8c\u6210\u7ed1\u5b9a<\/p>\n<p>&lt;pre&gt;&lt;code&gt;Intent intent = new Intent(Activity1.this,Activity2.class);<br \/>\nbindService(intent,conn,Context.BIND_AUTO_CREATE);<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>bindService\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2aServiceConnection\u7c7b\u578b\u7684\u53c2\u6570\u3002service\u548c\u5176\u4ed6\u7ec4\u4ef6\u4e4b\u95f4\u7684\u8fde\u63a5\u90fd\u8868\u793a\u4e3a\u4e00\u4e2aServiceConnection\uff0c\u8981\u60f3\u5c06service\u548c\u5176\u4ed6\u7ec4\u4ef6\u8fdb\u884c\u7ed1\u5b9a\uff0c\u5c31\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u65b0\u7684ServiceConnection\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;   public ServiceConnection conn= new ServiceConnection() {<br \/>\n   @Override<br \/>\n    public void onServiceDisconnected(ComponentName name) {<br \/>\n            \/\/\u5f53\u8fde\u63a5\u610f\u5916\u65ad\u5f00\u65f6\u8c03\u7528<br \/>\n            Log.i(LOG, &quot;&quot;onServiceDisconnected&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;&quot;);<br \/>\n            myservice = null;  <\/p>\n<p>   } <\/p>\n<p>   @Override<br \/>\n    public void onServiceConnected(ComponentName name, IBinder service) {<br \/>\n    \/\/\u5f53\u5efa\u7acb\u8fde\u63a5\u65f6\u8c03\u7528<br \/>\n   Log.i(LOG, &quot;&quot;onServiceConnected&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;&quot;);<br \/>\n        myservice = ((Service1.MyBinder)service).getService();<br \/>\n    }<br \/>\n};<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>bindService\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e00\u4e2aflag\u3002<br \/>\n   \u53ef\u4ee5\u4f7f\u7528\u7684flag\u6709\uff1a<\/p>\n<p>BIND_AUTO_CREATE\uff1a\u7ed1\u5b9a\u5b8c\u6210\u540e\u5c31\u542f\u52a8\u76ee\u6807service<\/p>\n<p>BIND_DEBUG_UNBIND\uff1a\u8fd9\u53ea\u5728debug\u65f6\u4f7f\u7528\uff0c\u8ddfunbind\u6709\u5173\u3002<\/p>\n<p>BIND_NOT_FOREGROUND\uff1a\u786e\u4fdd\u88ab\u7ed1\u5b9a\u7684service\u6c38\u8fdc\u4e0d\u4f1a\u6709\u8fd0\u884c\u4e8e\u524d\u53f0\u7684\u4f18\u5148\u7ea7\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7ed1\u5b9a\u4e00\u4e2aservice\u4f1a\u63d0\u9ad8\u5b83\u7684\u4f18\u5148\u7ea7<\/p>\n<p>BIND_ABOVE_CLIENT\uff1a\u786e\u4fdd\u5ba2\u6237\u7aef\u5904\u4e8e\u524d\u53f0\u65f6\uff0c\u7ed1\u5b9a\u7684service\u4e5f\u53d8\u4e3a\u524d\u53f0\u8fdb\u7a0b<\/p>\n<p>BIND_ALLOW_OOM_MANAGEMENT\uff1a\u5141\u8bb8\u7cfb\u7edf\u5728\u4f4e\u5185\u5b58\u7b49\u72b6\u6001\u4e0b\u5220\u9664\u8be5service\uff08\u8fd9\u662f\u81ea\u5df1\u5bf9\u6e90\u7801\u4e2d\u6ce8\u91ca\u7684\u7406\u89e3\uff09<\/p>\n<p>BIND_WAIVE_PRIORITY\uff1a\u7ed1\u5b9aservice\u65f6\u4e0d\u6539\u53d8\u5176\u4f18\u5148\u7ea7<\/p>\n<p>BIND_ADJUST_WITH_ACTIVITY\uff1a\u7cfb\u7edf\u6839\u636eservice\u6240\u7ed1\u5b9a\u7684activity\u7684\u91cd\u8981\u7a0b\u5ea6\u6765\u8c03\u6574\u8fd9\u4e2aservice\u7684\u4f18\u5148\u7ea7\u3002<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u8fd9\u9700\u8981\u5b9e\u73b0service\u4e2d\u7684onBind()\u51fd\u6570\u4ee5\u8fd4\u56deservice\u5b9e\u4f8b\u7ed9ac [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-42335","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=42335"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42335\/revisions"}],"predecessor-version":[{"id":42336,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42335\/revisions\/42336"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=42335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=42335"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=42335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}