{"id":490,"date":"2011-04-24T14:15:25","date_gmt":"2011-04-24T06:15:25","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=490"},"modified":"2011-05-23T13:39:03","modified_gmt":"2011-05-23T05:39:03","slug":"%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=490","title":{"rendered":"\u670d\u52a1"},"content":{"rendered":"<p>\u670d\u52a1\u662f\u4e00\u79cd\u53ef\u4ee5\u5728\u540e\u53f0\u6267\u884c\u957f\u671f\u8fd0\u884c\u64cd\u4f5c\u800c\u4e0d\u63d0\u4f9b\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u3002\u53e6\u5916\u7684\u67d0\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\uff0c\u4e4b\u540e\u8be5\u670d\u52a1\u4f1a\u4e00\u76f4\u8fd0\u884c\uff0c\u5373\u4f7f\u7528\u6237\u5207\u6362\u5230\u4e86\u53e6\u5916\u4e00\u4e2a\u7a0b\u5e8f\u3002\u6b64\u5916\uff0c\u4e00\u4e2a\u7ec4\u4ef6\u53ef\u4ee5\u4e0e\u670d\u52a1\u7ed1\u5b9a\uff0c\u4e0e\u4e4b\u4ea4\u4e92\uff0c\u751a\u81f3\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff0cinterprocess communication\uff09\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u670d\u52a1\u53ef\u4ee5\u5728\u540e\u53f0\u5b8c\u6210\u5904\u7406\u7f51\u7edc\u4e8b\u52a1\u3001\u64ad\u653e\u97f3\u4e50\u3001\u6267\u884c\u6587\u4ef6I\/O\u64cd\u4f5c\uff0c\u6216\u662f\u4e0e\u4e00\u4e2a\u5185\u5bb9\u63d0\u4f9b\u8005\uff08content provider\uff09\u4ea4\u4e92\u7b49\u5de5\u4f5c\u3002<\/p>\n<p>\u670d\u52a1\u901a\u5e38\u6709\u4e24\u79cd\u5b58\u5728\u5f62\u5f0f\uff1a<\/p>\n<p>\u542f\u52a8\u7684\uff08started\uff09<\/p>\n<p style=\"padding-left: 30px;\">\u4e00\u4e2a\u670d\u52a1\u5728\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\uff08\u4f8b\u5982\u6d3b\u52a8\uff09\u901a\u8fc7\u8c03\u7528startService()\u542f\u52a8\u5b83\u4e4b\u540e\u6210\u4e3a\u201c\u542f\u52a8\u7684\u201d\u3002\u4e00\u65e6\u88ab\u542f\u52a8\uff0c\u670d\u52a1\u53ef\u4ee5\u6c38\u8fdc\u8fd0\u884c\u4e8e\u540e\u53f0\uff0c\u5373\u4f7f\u542f\u52a8\u5b83\u7684\u7ec4\u4ef6\u672c\u8eab\u5df2\u88ab\u9500\u6bc1\u3002\u901a\u5e38\uff0c\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u5c06\u6267\u884c\u5355\u4e00\u7684\u64cd\u4f5c\u4e14\u4e0d\u8fd4\u56de\u5176\u8c03\u7528\u8005\u7ed3\u679c\u3002\u4f8b\u5982\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u4e0b\u8f7d\u6216\u4e0a\u4f20\u4e00\u4e2a\u6587\u4ef6\u3002\u5f53\u64cd\u4f5c\u5b8c\u6210\u540e\uff0c\u670d\u52a1\u5e94\u5f53\u80fd\u7ed3\u675f\u81ea\u5df1\u3002<\/p>\n<p>\u7ed1\u5b9a\u7684\uff08bound\uff09<\/p>\n<p style=\"padding-left: 30px;\">\u4e00\u4e2a\u670d\u52a1\u5728\u67d0\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528bindService()\u7ed1\u5b9a\u5b83\u4e4b\u540e\u6210\u4e3a\u201c\u7ed1\u5b9a\u7684\u201d\u3002\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\u5c06\u63d0\u4f9b\u4e00\u4e2a\u5ba2\u6237\u7aef-\u670d\u52a1\u5668\u63a5\u53e3\uff0c\u5141\u8bb8\u7ec4\u4ef6\u4e0e\u4e4b\u4ea4\u4e92\uff0c\u53d1\u9001\u8bf7\u6c42\uff0c\u63a5\u6536\u7ed3\u679c\uff0c\u751a\u81f3\u901a\u8fc7IPC\u6267\u884c\u8de8\u8fdb\u7a0b\u5de5\u4f5c\u3002\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\u5c06\u5728\u53e6\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u4e0e\u4e4b\u7ed1\u5b9a\u65f6\u8fd0\u884c\u3002\u591a\u4e2a\u7ec4\u4ef6\u53ef\u4ee5\u540c\u65f6\u7ed1\u5b9a\u5355\u4e2a\u670d\u52a1\uff0c\u4e0d\u8fc7\u5f53\u6240\u6709\u7684\u7ec4\u4ef6\u90fd\u89e3\u9664\u7ed1\u5b9a\u65f6\uff0c\u8be5\u670d\u52a1\u5c06\u4f1a\u9500\u6bc1\u3002<\/p>\n<p>\u5c3d\u7ba1\u672c\u6587\u6863\u539f\u5219\u4e0a\u5c06\u5206\u5f00\u8ba8\u8bba\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u670d\u52a1\uff0c\u4e00\u4e2a\u670d\u52a1\u53ef\u4ee5\u540c\u65f6\u4ee5\u8fd9\u4e24\u79cd\u5f62\u5f0f\u5b58\u5728\u2014\u2014\u5b83\u53ef\u4ee5\u88ab\u542f\u52a8\uff08\u800c\u6c38\u4e45\u8fd0\u884c\u4e0b\u53bb\uff09\u540c\u65f6\u4e5f\u5141\u8bb8\u88ab\u7ed1\u5b9a\u3002\u8fd9\u4e00\u5207\u53ea\u4e0d\u8fc7\u53d6\u51b3\u4e8e\u662f\u5426\u4f7f\u7528\u4e86\u56de\u9988\u65b9\u6cd5onStartCommand()\u5141\u8bb8\u7ec4\u4ef6\u542f\u52a8\u670d\u52a1\u5e76\u4e14\u8fd8\u4f7f\u7528\u4e86onBind()\u5141\u8bb8\u7ed1\u5b9a\u3002<\/p>\n<p>\u65e0\u8bba\u7a0b\u5e8f\u662f\u542f\u52a8\u7684\u3001\u7ed1\u5b9a\u7684\uff0c\u6216\u8005\u4e24\u8005\u90fd\u662f\uff0c\u4efb\u4f55\u5e94\u7528\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u50cf\u4f7f\u7528\u4e00\u4e2a\u6d3b\u52a8\u90a3\u6837\u4f7f\u7528\u4e00\u4e2a\u670d\u52a1\u2014\u2014\u5373\u901a\u8fc7Intent\u6765\u542f\u7528\u3002\u4e0d\u8fc7\uff0c\u670d\u52a1\u53ef\u4ee5\u5728manifest\u6587\u4ef6\u4e2d\u88ab\u58f0\u660e\u4e3a\u79c1\u6709\u7684\uff0c\u4ee5\u963b\u65ad\u5176\u4ed6\u7a0b\u5e8f\u5bf9\u5176\u7684\u8bfb\u53d6\u3002\u8fd9\u70b9\u5c06\u5728\u201c\u5728manifest\u4e2d\u58f0\u660e\u670d\u52a1\u201d\u4e00\u8282\u4e2d\u66f4\u52a0\u8be6\u7ec6\u5730\u8fdb\u884c\u8ba8\u8bba\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u4e00\u4e2a\u670d\u52a1\u8fd0\u884c\u4e8e\u5176\u5bbf\u4e3b\u8fdb\u7a0b\u7684\u4e3b\u7ebf\u7a0b\u4e2d\u2014\u2014\u670d\u52a1<strong>\u4e0d\u4f1a<\/strong>\u521b\u5efa\u5176\u81ea\u6709\u7684\u7ebf\u7a0b\uff0c\u4e5f\u4e0d\u4f1a\u8fd0\u884c\u4e8e\u53e6\u5916\u7684\u8fdb\u7a0b\uff08\u9664\u975e\u4f60\u7279\u522b\u6307\u5b9a\uff09\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u8981\u6267\u884cCPU\u9ad8\u5360\u7528\u5de5\u4f5c\u6216\u662f\u963b\u65ad\u6027\u7684\u64cd\u4f5c\uff08\u5982MP3\u64ad\u653e\u6216\u662f\u7f51\u7edc\u4efb\u52a1\uff09\uff0c\u5c31\u5e94\u8be5\u65b0\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u4ee4\u670d\u52a1\u5728\u5176\u4e2d\u5de5\u4f5c\u3002\u4f7f\u7528\u53e6\u5916\u7684\u7ebf\u7a0b\u5c06\u51cf\u4f4e\u7a0b\u5e8f\u4e0d\u54cd\u5e94\uff08ANR\uff0cApplication Not Responding\uff09\u7684\u98ce\u9669\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u7684\u4e3b\u7ebf\u7a0b\u4e2d\u7684\u6d3b\u52a8\u5bf9\u7528\u6237\u4ea4\u4e92\u4fdd\u6301\u6d41\u7545\u3002<\/p>\n<h2>\u57fa\u672c\u5185\u5bb9<\/h2>\n<p><em>\u5e94\u8be5\u4f7f\u7528\u670d\u52a1\u8fd8\u662f\u7ebf\u7a0b\uff1f<\/em><\/p>\n<p><em>\u670d\u52a1\u53ea\u662f\u4e00\u4e2a\u5728\u7528\u6237\u6ca1\u6709\u4e0e\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u65f6\u4e5f\u80fd\u4e8e\u540e\u53f0\u8fd0\u884c\u7684\u7ec4\u4ef6\u800c\u5df2\u3002\u56e0\u6b64\uff0c\u53ea\u5e94\u5f53\u5728\u5fc5\u8981\u65f6\u624d\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\u3002<\/em><\/p>\n<p><em>\u5982\u679c\u4ec5\u4ec5\u9700\u8981\u5728\u7528\u6237\u4e0e\u7a0b\u5e8f\u8fdb\u884c\u4ea4\u4e92\u65f6\u4e8e\u4e3b\u7ebf\u7a0b\u4e4b\u5916\u6267\u884c\u5de5\u4f5c\uff0c\u5e94\u5f53\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u800c\u4e0d\u662f\u4e00\u4e2a\u670d\u52a1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5e0c\u671b\u5728\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c\u65f6\u64ad\u653e\u97f3\u4e50\uff0c\u53ef\u4ee5\u5728onCreate()\u4e2d\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5728onStart()\u4e2d\u8fd0\u884c\u5b83\uff0c\u4e4b\u540e\u5728onStop()\u4e2d\u505c\u6b62\u5b83\u3002\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528AsyncTask\u6216\u662fHandlerThread\u6765\u53d6\u4ee3\u4f20\u7edf\u7684Thread\u7c7b\u3002\u5173\u4e8e\u7ebf\u7a0b\u7684\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u89c1\u201c\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u201d\u6587\u6863\u3002<\/em><\/p>\n<p><em>\u9700\u8981\u8bb0\u5f97\u7684\u662f\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u4e00\u4e2a\u670d\u52a1\uff0c\u5b83\u9ed8\u8ba4\u662f\u8fd0\u884c\u4e8e\u7a0b\u5e8f\u7684\u4e3b\u7ebf\u7a0b\u3002\u56e0\u6b64\u5e94\u5f53\u5728\u9700\u8981\u6267\u884c\u5bc6\u96c6\u6216\u662f\u963b\u585e\u6027\u7684\u64cd\u4f5c\u65f6\u4e3a\u670d\u52a1\u53e6\u5916\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/em><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u670d\u52a1\uff0c\u5c31\u5fc5\u987b\u521b\u5efa\u4e00\u4e2aService\uff08\u6216\u5b83\u7684\u4e00\u4e2a\u73b0\u6709\u5b50\u7c7b\uff09\u7684\u5b50\u7c7b\u3002\u5728\u5b9e\u73b0\u65f6\uff0c\u9700\u8981\u6b63\u786e\u8986\u76d6\u4e00\u4e9b\u63a7\u5236\u670d\u52a1\u7684\u751f\u547d\u5468\u671f\u5173\u952e\u65b9\u9762\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e0e\u5176\u4ed6\u7ec4\u4ef6\u76f8\u7ed1\u5b9a\u7684\u673a\u5236\u7684\u56de\u9988\u65b9\u6cd5\u3002\u6700\u4e3a\u91cd\u8981\u7684\u4e00\u4e9b\u9700\u8981\u88ab\u8986\u76d6\u7684\u65b9\u6cd5\u6709\uff1a<\/p>\n<p>onStartCommand()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728\u53e6\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u4f8b\u5982\u4e00\u4e2a\u6d3b\u52a8\uff0c\u901a\u8fc7\u8c03\u7528startService()\u6765\u8bf7\u6c42\u542f\u52a8\u670d\u52a1\u65f6\u5c06\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\u4e00\u65e6\u8fd9\u4e2a\u65b9\u6cd5\u88ab\u6267\u884c\uff0c\u670d\u52a1\u5c06\u88ab\u542f\u52a8\u5e76\u6c38\u4e45\u5730\u8fd0\u884c\u4e8e\u540e\u53f0\u3002\u5982\u679c\u4f7f\u7528\u4e86\u5b83\uff0c\u5c31\u5fc5\u987b\u8d1f\u8d23\u5728\u5de5\u4f5c\u5b8c\u6210\u540e\u901a\u8fc7\u8c03\u7528stopSelf()\u6216stopService()\u6765\u7ec8\u6b62\u670d\u52a1\u3002\uff08\u5982\u679c\u53ea\u662f\u60f3\u8981\u63d0\u4f9b\u7ed1\u5b9a\uff0c\u4e0d\u9700\u8981\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\uff09<\/p>\n<p>onBind()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u8981\u901a\u8fc7\u8c03\u7528bindService()\u6765\u4e0e\u670d\u52a1\u76f8\u7ed1\u5b9a\uff08\u4f8b\u5982\u6267\u884cRPC\uff09\u65f6\u5c06\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002\u5728\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\u4f9b\u5ba2\u6237\u7aef\u8fd4\u56deIBinder\u6765\u4e0e\u670d\u52a1\u901a\u4fe1\u3002\u8fd9\u4e2a\u65b9\u6cd5\u603b\u662f\u9700\u8981\u88ab\u4f7f\u7528\uff0c\u4e0d\u8fc7\u8981\u662f\u4e0d\u5141\u8bb8\u7ed1\u5b9a\u7684\u8bdd\uff0c\u5219\u5e94\u5f53\u8fd4\u56denull\u503c\u3002<\/p>\n<p>onCreate()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728\u670d\u52a1\u521d\u6b21\u88ab\u521b\u5efa\u65f6\u5c06\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u6765\u6267\u884c\u4e00\u6b21\u6027\u8bbe\u7f6e\u6b65\u9aa4\uff08\u8fd9\u4f1a\u5728\u8c03\u7528onStartCommand()\u6216onBind()\u4e4b\u524d\uff09\u3002\u5982\u679c\u670d\u52a1\u5df2\u7ecf\u5728\u8fd0\u884c\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5219\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<p>onDestroy()<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5c06\u5728\u670d\u52a1\u4e0d\u518d\u88ab\u4f7f\u7528\uff0c\u5373\u5c06\u88ab\u9500\u6bc1\u65f6\u8c03\u7528\u8be5\u65b9\u6cd5\u3002\u670d\u52a1\u5e94\u5f53\u4f7f\u7528\u5b83\u6765\u6e05\u9664\u7ebf\u7a0b\u3001\u5df2\u6ce8\u518c\u7684\u76d1\u542c\u5668\uff08registered listener\uff09\u3001\u63a5\u6536\u8005\uff08receiver\uff09\u7b49\u8d44\u6e90\u3002\u8fd9\u5c06\u662f\u6700\u540e\u4e00\u6b21\u8c03\u7528\u670d\u52a1\u63a5\u6536\u8005\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528startService()\uff08\u8fd9\u5c06\u4f1a\u8c03\u7528onStartCommand()\uff09\u6765\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\uff0c\u670d\u52a1\u4f1a\u4e0d\u505c\u5730\u8fd0\u884c\u76f4\u81f3\u6709stopSelf()\u6765\u4e2d\u6b62\u5176\u81ea\u8eab\u6216\u662f\u6709\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528stopService()\u6765\u4e2d\u6b62\u5b83\u3002<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u7ec4\u4ef6\u8c03\u7528bindService()\u6765\u521b\u5efa\u670d\u52a1\uff08\u6b64\u65f6onStartCommand()\u4e0d\u4f1a\u88ab\u8c03\u7528\uff09\uff0c\u90a3\u4e48\u670d\u52a1\u53ea\u4f1a\u5728\u7ec4\u4ef6\u4e0e\u4e4b\u7ed1\u5b9a\u65f6\u8fd0\u884c\u3002\u4e00\u65e6\u670d\u52a1\u4e0e\u6240\u6709\u7684\u5ba2\u6237\u7aef\u89e3\u9664\u7ed1\u5b9a\uff0c\u7cfb\u7edf\u5c31\u4f1a\u9500\u6bc1\u5b83\u3002<\/p>\n<p>Android\u7cfb\u7edf\u4f1a\u5728\u5185\u5b58\u4f4e\u65f6\u5f3a\u5236\u7ec8\u6b62\u4e00\u4e2a\u670d\u52a1\uff0c\u56e0\u4e3a\u5b83\u5fc5\u987b\u4fdd\u6709\u5904\u4e8e\u7528\u6237\u7126\u70b9\u7684\u6d3b\u52a8\u7684\u7cfb\u7edf\u8d44\u6e90\u3002\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u4e0e\u7528\u6237\u7126\u70b9\u4e0b\u7684\u6d3b\u52a8\u76f8\u7ed1\u5b9a\uff0c\u90a3\u5c31\u4e0d\u592a\u5bb9\u6613\u88ab\u6740\u9664\u3002\u5982\u679c\u8be5\u670d\u52a1\u88ab\u58f0\u660e\u4e8e\u524d\u53f0\u8fd0\u884c\uff08\u4e4b\u540e\u4f1a\u8ba8\u8bba\u8be5\u60c5\u51b5\uff09\uff0c\u90a3\u5b83\u5c31\u51e0\u4e4e\u4e0d\u53ef\u80fd\u88ab\u6740\u9664\u4e86\u3002\u53cd\u8fc7\u6765\u8bf4\uff0c\u5982\u679c\u670d\u52a1\u662f\u542f\u52a8\u7684\u5e76\u4e14\u957f\u671f\u8fd0\u884c\uff0c\u7cfb\u7edf\u5c06\u4f1a\u5728\u4e00\u6bb5\u65f6\u95f4\u540e\u964d\u4f4e\u5176\u5728\u540e\u53f0\u4efb\u52a1\u5217\u8868\u4e2d\u7684\u4f4d\u7f6e\uff0c\u8fd9\u6837\u670d\u52a1\u4f1a\u53d8\u5f97\u8f83\u4e3a\u53ef\u80fd\u88ab\u6740\u9664\u2014\u2014\u5982\u679c\u8fd9\u4e2a\u670d\u52a1\u662f\u542f\u52a8\u7684\uff0c\u90a3\u5c31\u5fc5\u987b\u505a\u597d\u7cbe\u5fc3\u8bbe\u8ba1\u4ee5\u4f7f\u7cfb\u7edf\u80fd\u591f\u91cd\u65b0\u542f\u52a8\u5b83\u3002\u5982\u679c\u7cfb\u7edf\u6740\u9664\u4e86\u8fd9\u4e2a\u670d\u52a1\uff0c\u5b83\u5c06\u5728\u8d44\u6e90\u53ef\u7528\u540e\u9a6c\u4e0a\u518d\u6b21\u542f\u52a8\uff08\u5c3d\u7ba1\u5982\u540e\u9762\u5c06\u8981\u63d0\u5230\u7684\u90a3\u6837\uff0c\u8fd9\u4e5f\u53d6\u51b3\u4e8eonStartCommand()\u6240\u8fd4\u56de\u7684\u503c\uff09\u3002\u5173\u4e8e\u7cfb\u7edf\u5982\u4f55\u9500\u6bc1\u670d\u52a1\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u9605\u201c\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u201d\u6587\u6863\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u5c0f\u8282\u4e2d\uff0c\u5c06\u80fd\u770b\u5230\u5982\u4f55\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u670d\u52a1\u4ee5\u53ca\u5982\u4f55\u5728\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n<h2>\u5728manifest\u4e2d\u58f0\u660e\u4e00\u4e2a\u670d\u52a1<\/h2>\n<p>\u5c31\u50cf\u6d3b\u52a8\uff08\u548c\u5176\u4ed6\u7ec4\u4ef6\uff09\u4e00\u6837\uff0c\u5fc5\u987b\u5728\u7a0b\u5e8f\u7684manifest\u6587\u4ef6\u4e2d\u58f0\u660e\u6240\u6709\u7684\u670d\u52a1\u3002<\/p>\n<p>\u8981\u58f0\u660e\u4e00\u4e2a\u670d\u52a1\uff0c\u6dfb\u52a0&lt;service&gt;\u4f5c\u4e3a&lt;application&gt;\u5143\u7d20\u7684\u5b50\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><span style=\"color: #000080;\">&lt;manifest ... &gt;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 ...<\/span>\r\n<span style=\"color: #000080;\">\u00a0 &lt;application ... &gt;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 &lt;service<\/span> <span style=\"color: #800080;\">android:name<\/span>=<span style=\"color: #008000;\">\".ExampleService\" <\/span><span style=\"color: #000080;\">\/&gt;<\/span><\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 ...<\/span>\r\n<span style=\"color: #000080;\">\u00a0 &lt;\/application&gt;<\/span>\r\n<span style=\"color: #000080;\">&lt;\/manifest&gt;<\/span><\/pre>\n<p>\u5728&lt;service&gt;\u5143\u7d20\u4e2d\u53ef\u4ee5\u5305\u542b\u5176\u4ed6\u7684\u4e00\u4e9b\u5c5e\u6027\u4ee5\u5b9a\u4e49\u5176\u7279\u6027\uff0c\u4f8b\u5982\u542f\u52a8\u670d\u52a1\u6240\u9700\u7684\u8bb8\u53ef\u548c\u670d\u52a1\u6240\u8981\u8fd0\u884c\u4e8e\u7684\u8fdb\u7a0b\u7b49\u3002\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u89c1&lt;service&gt;\u5143\u7d20\u7684\u53c2\u8003\u6587\u6863\u3002<\/p>\n<p>\u6b63\u5982\u6d3b\u52a8\u4e00\u6837\uff0c\u4e00\u4e2a\u670d\u52a1\u53ef\u4ee5\u5b9a\u4e49\u5176\u610f\u56fe\u8fc7\u6ee4\u5668\u4ee5\u5141\u8bb8\u5176\u4ed6\u7ec4\u4ef6\u4f7f\u7528\u975e\u663e\u5f0f\u7684\u610f\u56fe\u6765\u6fc0\u6d3b\u5b83\u3002\u901a\u8fc7\u58f0\u660e\u610f\u56fe\u8fc7\u6ee4\u5668\uff0c\u7528\u6237\u8bbe\u5907\u4e0a\u6240\u5b89\u88c5\u7684\u5176\u4ed6\u7a0b\u5e8f\u4e2d\u7684\u7ec4\u4ef6\u53ef\u4ee5\u5728\u670d\u52a1\u58f0\u660e\u7684\u610f\u56fe\u8fc7\u6ee4\u5668\u4e0e\u8be5\u7a0b\u5e8f\u4f20\u9012\u7ed9startService()\u7684\u610f\u56fe\u76f8\u5339\u914d\u65f6\u542f\u52a8\u8fd9\u4e2a\u670d\u52a1\u3002<\/p>\n<p>\u5982\u679c\u53ea\u6253\u7b97\u5728\u672c\u5730\u4f7f\u7528\u670d\u52a1\uff08\u5176\u4ed6\u7684\u7a0b\u5e8f\u4e0d\u4f1a\u4f7f\u7528\u8be5\u670d\u52a1\uff09\uff0c\u90a3\u5c31\u4e0d\u5fc5\uff08\u800c\u4e14\u4e0d\u5e94\u8be5\uff09\u63d0\u4f9b\u4efb\u4f55\u610f\u56fe\u8fc7\u6ee4\u5668\u3002\u6ca1\u6709\u4efb\u4f55\u610f\u56fe\u8fc7\u6ee4\u5668\u65f6\uff0c\u5fc5\u987b\u4f7f\u7528\u4e00\u4e2a\u663e\u5f0f\u5730\u6807\u660e\u4e86\u670d\u52a1\u7c7b\u7684\u540d\u79f0\u7684\u610f\u56fe\u6765\u542f\u52a8\u8be5\u670d\u52a1\u3002\u5173\u4e8e\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u7684\u66f4\u591a\u4fe1\u606f\u5c06\u5728\u4e4b\u540e\u8ba8\u8bba\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5f53\u5305\u542b\u4e86android:exported\u5c5e\u6027\u4e14\u5c06\u5176\u8bbe\u7f6e\u4e3a\u201c<span style=\"color: #008000;\">false<\/span>\u201d\u540e\u5c31\u53ef\u4ee5\u786e\u4fdd\u8be5\u670d\u52a1\u662f\u672c\u7a0b\u5e8f\u79c1\u6709\u7684\u3002\u8fd9\u79cd\u505a\u6cd5\u5728\u670d\u52a1\u652f\u6301\u610f\u56fe\u8fc7\u6ee4\u5668\u65f6\u5c06\u4f1a\u5f88\u6709\u6548\u3002<\/p>\n<p>\u5173\u4e8e\u4e3a\u670d\u52a1\u521b\u5efa\u610f\u56fe\u8fc7\u6ee4\u5668\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u53c2\u89c1\u201c\u610f\u56fe\u548c\u610f\u56fe\u8fc7\u6ee4\u5668\u201d\u6587\u6863\u3002<\/p>\n<h2>\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1<\/h2>\n<p>\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u662f\u7531\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528startService()\u6765\u542f\u52a8\u7684\uff0c\u8fd9\u4e5f\u5c06\u4f1a\u8c03\u7528\u8be5\u670d\u52a1\u7684onStartCommand()\u65b9\u6cd5\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u670d\u52a1\u662f\u542f\u52a8\u7684\u4e4b\u65f6\uff0c\u5b83\u62e5\u6709\u4e00\u4e2a\u548c\u542f\u52a8\u5b83\u7684\u7ec4\u4ef6\u4e0d\u76f8\u5173\u7684\u751f\u547d\u5468\u671f\uff0c\u56e0\u6b64\u8fd9\u4e2a\u670d\u52a1\u53ef\u4ee5\u5728\u540e\u53f0\u65e0\u9650\u671f\u5730\u8fd0\u884c\uff0c\u5373\u4f7f\u90a3\u4e2a\u542f\u52a8\u5b83\u7684\u7ec4\u4ef6\u88ab\u9500\u6bc1\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u670d\u52a1\u5e94\u5f53\u5728\u5de5\u4f5c\u5b8c\u6210\u540e\u901a\u8fc7\u8c03\u7528stopSelf()\u6765\u7ec8\u6b62\u81ea\u5df1\uff0c\u6216\u8005\u88ab\u522b\u7684\u7ec4\u4ef6\u8c03\u7528stopService()\u6765\u4e2d\u6b62\u3002<\/p>\n<p>\u8bf8\u5982\u6d3b\u52a8\u8fd9\u6837\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528startService()\u5e76\u4f20\u9012\u6307\u5411\u8be5\u670d\u52a1\u7684\u610f\u56fe\u4ee5\u53ca\u670d\u52a1\u6240\u9700\u7684\u6570\u636e\u6765\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u3002\u8fd9\u4e2a\u670d\u52a1\u5c06\u5728onStartCommand()\u65b9\u6cd5\u4e2d\u63a5\u6536\u8fd9\u4e2aIntent\u3002<\/p>\n<p>\u6bd4\u5982\u8bf4\uff0c\u5047\u5b9a\u6709\u4e00\u4e2a\u6d3b\u52a8\u9700\u8981\u5728\u4e00\u4e2a\u5728\u7ebf\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u4e00\u4e9b\u4fe1\u606f\u3002\u8fd9\u4e2a\u6d3b\u52a8\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u534f\u540c\u670d\u52a1\uff0c\u5c06\u9700\u8981\u4fdd\u5b58\u7684\u6570\u636e\u901a\u8fc7\u4e00\u4e2a\u610f\u56fe\u4f20\u9012\u7ed9startService()\u3002\u8be5\u670d\u52a1\u4f1a\u5728onStartCommand()\u4e2d\u63a5\u6536\u8fd9\u4e2a\u610f\u56fe\uff0c\u4e0e\u56e0\u7279\u7f51\u8fde\u63a5\u5e76\u6267\u884c\u6570\u636e\u5e93\u4e8b\u52a1\u3002\u5f53\u4e8b\u52a1\u5b8c\u6210\u540e\uff0c\u670d\u52a1\u5c06\u7ec8\u6b62\u81ea\u8eab\u5e76\u9500\u6bc1\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u670d\u52a1\u4e0e\u58f0\u660e\u5b83\u7684\u7a0b\u5e8f\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\uff0c\u4e14\u9ed8\u8ba4\u8fd0\u884c\u4e8e\u540c\u4e00\u4e2a\u7ebf\u7a0b\u3002\u56e0\u6b64\uff0c\u5982\u679c\u670d\u52a1\u8981\u5728\u7528\u6237\u548c\u8be5\u7a0b\u5e8f\u7684\u4e00\u4e2a\u6d3b\u52a8\u8fdb\u884c\u4ea4\u4e92\u65f6\u6267\u884c\u5bc6\u96c6\u578b\u6216\u963b\u585e\u6027\u7684\u64cd\u4f5c\uff0c\u5c31\u4f1a\u964d\u4f4e\u6d3b\u52a8\u7684\u6027\u80fd\u8868\u73b0\u3002\u8981\u907f\u514d\u8fd9\u6837\u7684\u7a0b\u5e8f\u6027\u80fd\u4e0b\u964d\uff0c\u5c31\u5e94\u5f53\u4e3a\u670d\u52a1\u65b0\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u6709\u4e24\u4e2a\u7c7b\u53ef\u4ee5\u7ee7\u627f\u4ee5\u521b\u5efa\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\uff1a<\/p>\n<p>Service<\/p>\n<p style=\"padding-left: 30px;\">\u8fd9\u662f\u6240\u6709\u670d\u52a1\u7684\u57fa\u7c7b\u3002\u5728\u7ee7\u627f\u8fd9\u4e2a\u7c7b\u4e4b\u540e\uff0c\u7531\u4e8e\u670d\u52a1\u9ed8\u8ba4\u8fd0\u884c\u4e8e\u7a0b\u5e8f\u7684\u4e3b\u7ebf\u7a0b\u800c\u4f1a\u964d\u4f4e\u7a0b\u5e8f\u5185\u6b63\u5728\u8fd0\u884c\u7684\u6d3b\u52a8\u7684\u6027\u80fd\u8868\u73b0\uff0c\u56e0\u6b64\u5fc5\u987b\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ebf\u7a0b\u4f9b\u670d\u52a1\u8fd0\u884c\u3002<\/p>\n<p>IntentService<\/p>\n<p style=\"padding-left: 30px;\">\u8fd9\u662fService\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u4f7f\u7528\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\uff08worker thread\uff09\u6765\u4f9d\u6b21\u5904\u7406\u6240\u6709\u7684\u8bf7\u6c42\u3002\u5982\u679c\u670d\u52a1\u4e0d\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\u7684\u8bdd\uff0c\u8fd9\u5c06\u662f\u6700\u597d\u7684\u9009\u62e9\u3002\u53ea\u9700\u8981\u4f7f\u7528onHandleIntent()\u6765\u63a5\u6536\u6bcf\u4e00\u4e2a\u542f\u52a8\u8bf7\u6c42\u4fbf\u53ef\u4ee5\u5728\u540e\u53f0\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u5c0f\u8282\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528\u8fd9\u4e24\u4e2a\u7c7b\u6765\u5b9e\u73b0\u4e00\u4e2a\u670d\u52a1\u3002<\/p>\n<h2>\u7ee7\u627fIntentService\u7c7b<\/h2>\n<p>\u7531\u4e8e\u5927\u90e8\u5206\u542f\u52a8\u7684\u670d\u52a1\u4e0d\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\uff08\u8fd9\u5c06\u662f\u4e00\u79cd\u5371\u9669\u7684\u591a\u7ebf\u7a0b\u60c5\u51b5\uff09\uff0c\u901a\u8fc7IntentService\u7c7b\u6765\u5b9e\u73b0\u4e00\u4e2a\u670d\u52a1\u53ef\u8bf4\u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<p>IntentService\u5c06\u4f1a\u505a\u4e00\u4e0b\u5de5\u4f5c\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u4e0d\u540c\u4e8e\u7a0b\u5e8f\u4e3b\u7ebf\u7a0b\u7684\u9ed8\u8ba4\u5de5\u4f5c\u7ebf\u7a0b\u4ee5\u6267\u884c\u6240\u6709\u4f20\u9001\u81f3onStartCommand()\u7684\u610f\u56fe\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u5de5\u4f5c\u961f\u5217\u4ee5\u4f9d\u6b21\u4f20\u9012\u610f\u56fe\u81f3onHandleIntent()\uff0c\u8fd9\u6837\u5c31\u4e0d\u5fc5\u518d\u62c5\u5fc3\u591a\u7ebf\u7a0b\u95ee\u9898\u3002<\/li>\n<li>\u5728\u6240\u6709\u8bf7\u6c42\u88ab\u5904\u7406\u540e\u7ec8\u6b62\u670d\u52a1\uff0c\u8fd9\u6837\u5c31\u4e0d\u5fc5\u518d\u8c03\u7528stopSelf()\u3002<\/li>\n<li>\u63d0\u4f9b\u4e00\u4e2a\u8fd4\u56denull\u503c\u7684onBind()\u9ed8\u8ba4\u5b9e\u73b0\u3002<\/li>\n<li>\u63d0\u4f9b\u4e00\u4e2a\u80fd\u53d1\u9001\u610f\u56fe\u81f3\u5de5\u4f5c\u961f\u5217\u7684onStartCommand()\u9ed8\u8ba4\u5b9e\u73b0\uff08\u610f\u56fe\u4e4b\u540e\u5c06\u88ab\u4f20\u9001\u81f3onHandleIntent()\uff09\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u4e00\u6765\u5c31\u53ea\u9700\u5b9e\u73b0onHandleIntent()\u4ee5\u6267\u884c\u5ba2\u6237\u7aef\u63d0\u4f9b\u7684\u5de5\u4f5c\u5373\u53ef\u3002\uff08\u4e0d\u8fc7\uff0c\u4ecd\u7136\u9700\u8981\u4e3a\u670d\u52a1\u63d0\u4f9b\u4e00\u4e2a\u7b80\u5355\u7684\u6784\u9020\u51fd\u6570\u3002\uff09<\/p>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u4f7f\u7528IntentService\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public class<\/span> <span style=\"color: #800080;\">HelloIntentService <\/span><span style=\"color: #000080;\">extends<\/span> <span style=\"color: #800080;\">IntentService<\/span> {<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \/** <\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0* A constructor is required, and must call the super <code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/IntentService.html#IntentService(java.lang.String)\">IntentService(String)<\/a><\/code><\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0* constructor with a name for the worker thread.<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0*\/<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 public<\/span> <span style=\"color: #800080;\">HelloIntentService<\/span>() {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 super<\/span>(<span style=\"color: #008000;\">\"HelloIntentService\"<\/span>);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \/**<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0* The IntentService calls this method from the default worker thread with<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0* the intent that started the service. When this method returns, IntentService<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0* stops the service, as appropriate.<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0*\/<\/span>\r\n<span style=\"color: #008080;\">\u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 protected void<\/span> onHandleIntent(<span style=\"color: #800080;\">Intent<\/span> intent) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \/\/ Normally we would do some work here, like download a file.<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \/\/ For our sample, we just sleep for 5 seconds.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 long<\/span> endTime = <span style=\"color: #800080;\">System<\/span>.currentTimeMillis() + <span style=\"color: #008080;\">5<\/span>*<span style=\"color: #008080;\">1000<\/span>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 while<\/span> (<span style=\"color: #800080;\">System<\/span>.currentTimeMillis() &lt; endTime) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 synchronized<\/span> (<span style=\"color: #000080;\">this<\/span>) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 try<\/span> {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 wait(endTime - <span style=\"color: #800080;\">System<\/span>.currentTimeMillis());<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<span style=\"color: #000080;\"> catch<\/span> (<span style=\"color: #800080;\">Exception<\/span> e) {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u6240\u6709\u9700\u8981\u7684\u5c31\u53ea\u662f\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u4ee5\u53ca\u4e00\u4e2aonHandleIntent()\u3002<\/p>\n<p>\u5982\u679c\u8fd8\u8981\u8986\u76d6\u5176\u4ed6\u7684\u56de\u9988\u65b9\u6cd5\uff0c\u6bd4\u5982onCreate()\uff0conStartCommand()\u6216\u662fonDestroy()\uff0c\u5c31\u5fc5\u987b\u8981\u8c03\u7528\u5176\u7236\u7c7b\u7684\u5b9e\u73b0\uff0c\u8fd9\u6837IntentService\u624d\u80fd\u6b63\u786e\u5904\u7406\u5de5\u4f5c\u7ebf\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>\u4f8b\u5982\uff0conStartCommand()\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u7684\u5b9e\u73b0\uff08\u5c31\u50cf\u88ab\u610f\u56fe\u83b7\u53d6\u5e76\u4f20\u9012\u7ed9onHandleIntent()\u7684\u90a3\u6837\uff09\uff1a<\/p>\n<pre><span style=\"color: #008080;\">@Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">public int<\/span> onStartCommand(<span style=\"color: #800080;\">Intent<\/span> intent, <span style=\"color: #000080;\">int <\/span>flags, <span style=\"color: #000080;\">int<\/span> startId) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 Toast<\/span>.makeText(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #008000;\">\"service starting\"<\/span>, <span style=\"color: #800080;\">Toast<\/span>.LENGTH_SHORT).show();<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 return super<\/span>.onStartCommand(intent,flags,startId);<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u9664\u4e86onHandleIntent()\uff0c\u4ec5\u6709\u7684\u53e6\u4e00\u4e2a\u4e0d\u5fc5\u8c03\u7528\u5176\u7236\u7c7b\u7684\u65b9\u6cd5\u662fonBind()\uff08\u4e0d\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u53ea\u6709\u5728\u670d\u52a1\u5141\u8bb8\u7ed1\u5b9a\u65f6\u624d\u4f1a\u88ab\u4f7f\u7528\uff09\u3002<\/p>\n<p>\u4e0b\u4e00\u8282\u4e2d\u5c06\u4f1a\u4ecb\u7ecd\u5728\u7ee7\u627fService\u57fa\u7c7b\u65f6\u76f8\u540c\u7c7b\u578b\u7684\u670d\u52a1\u662f\u5982\u4f55\u88ab\u4f7f\u7528\u7684\u3002\u4ee3\u7801\u4f1a\u7a0d\u6709\u4e9b\u957f\uff0c\u4e0d\u8fc7\u5728\u5904\u7406\u540c\u6b65\u5f00\u59cb\u7684\u8bf7\u6c42\u7684\u60c5\u51b5\u4e0b\u5f88\u6709\u7528\u3002<\/p>\n<h2>\u7ee7\u627fService\u7c7b<\/h2>\n<p>\u6b63\u5982\u5728\u4e4b\u524d\u7684\u5c0f\u8282\u4e2d\u6240\u89c1\u5230\u7684\uff0c\u4f7f\u7528IntentService\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u5b9e\u73b0\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u9700\u8981\u4e00\u4e2a\u670d\u52a1\u53ef\u4ee5\u591a\u7ebf\u7a0b\u5de5\u4f5c\uff08\u800c\u4e0d\u662f\u901a\u8fc7\u5de5\u4f5c\u961f\u5217\u5904\u7406\u542f\u52a8\u8bf7\u6c42\uff09\uff0c\u5c31\u8981\u7ee7\u627fService\u7c7b\u6765\u5904\u7406\u6bcf\u4e00\u4e2a\u610f\u56fe\u3002<\/p>\n<p>\u4e3a\u4e86\u505a\u5bf9\u6bd4\uff0c\u4e0b\u9762\u7684\u8303\u4f8b\u4ee3\u7801\u4e2dService\u7c7b\u7684\u5b9e\u73b0\u548c\u4e4b\u524d\u7684IntentService\u5c06\u6267\u884c\u76f8\u540c\u7684\u5de5\u4f5c\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u542f\u52a8\u8bf7\u6c42\uff0c\u5c06\u4f9d\u6b21\u4e00\u4e2a\u4e2a\u5730\u4f7f\u7528\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b\u6765\u6267\u884c\u4efb\u52a1\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public class <\/span><span style=\"color: #800080;\">HelloService<\/span> <span style=\"color: #000080;\">extends <\/span><span style=\"color: #800080;\">Service<\/span> {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 private <\/span><span style=\"color: #800080;\">Looper<\/span> mServiceLooper;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 private <\/span><span style=\"color: #800080;\">ServiceHandler<\/span> mServiceHandler;<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \/\/ Handler that receives messages from the thread<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 private final class <\/span><span style=\"color: #800080;\">ServiceHandler<\/span> <span style=\"color: #000080;\">extends <\/span><span style=\"color: #800080;\">Handler<\/span> {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 public <\/span><span style=\"color: #800080;\">ServiceHandler<\/span>(<span style=\"color: #800080;\">Looper<\/span> looper) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 super<\/span>(looper);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 public void <\/span>handleMessage(<span style=\"color: #800080;\">Message<\/span> msg) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Normally we would do some work here, like download a file.<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ For our sample, we just sleep for 5 seconds.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 long <\/span>endTime = <span style=\"color: #800080;\">System<\/span>.currentTimeMillis() + <span style=\"color: #008080;\">5<\/span>*<span style=\"color: #008080;\">1000<\/span>;<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 while <\/span>(<span style=\"color: #800080;\">System<\/span>.currentTimeMillis() &lt; endTime) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 synchronized <\/span>(<span style=\"color: #000080;\">this<\/span>) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 try <\/span>{<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 wait(endTime - <span style=\"color: #800080;\">System<\/span>.currentTimeMillis());<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 } <span style=\"color: #000080;\">catch <\/span>(<span style=\"color: #800080;\">Exception <\/span>e) {<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ Stop the service using the startId, so that we don't stop<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ the service in the middle of handling another job<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 stopSelf(msg.arg1);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 public void <\/span>onCreate() {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/\/ Start up the thread running the service. \u00a0Note that we create a<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/\/ separate thread because the service normally runs in the process's<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/\/ main thread, which we don't want to block. \u00a0We also make it<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/\/ background priority so CPU-intensive work will not disrupt our UI.<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 HandlerThread <\/span>thread = <span style=\"color: #000080;\">new <\/span><span style=\"color: #800080;\">HandlerThread<\/span>(<span style=\"color: #008000;\">\"ServiceStartArguments\"<\/span>,<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Process.THREAD_PRIORITY_BACKGROUND);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 thread.start();<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \/\/ Get the HandlerThread's Looper and use it for our Handler <\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 mServiceLooper = thread.getLooper();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 mServiceHandler = <span style=\"color: #000080;\">new <\/span><span style=\"color: #800080;\">ServiceHandler<\/span>(mServiceLooper);<\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 public int <\/span>onStartCommand(<span style=\"color: #800080;\">Intent <\/span>intent, <span style=\"color: #000080;\">int <\/span>flags, <span style=\"color: #000080;\">int <\/span>startId) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 Toast<\/span>.makeText(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #008000;\">\"service starting\"<\/span>, <span style=\"color: #800080;\">Toast<\/span>.LENGTH_SHORT).show();<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \/\/ For each start request, send a message to start a job and deliver the<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \/\/ start ID so we know which request we're stopping when we finish the job<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 Message <\/span>msg = mServiceHandler.obtainMessage();<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 msg.arg1 = startId;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 mServiceHandler.sendMessage(msg);<\/span>\r\n\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \/\/ If we get killed, after returning from here, restart<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 return <\/span>START_STICKY;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 public <\/span><span style=\"color: #800080;\">IBinder <\/span>onBind(<span style=\"color: #800080;\">Intent <\/span>intent) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \/\/ We don't provide binding, so return null<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 return null<\/span>;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 public void <\/span>onDestroy() {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 Toast<\/span>.makeText(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #008000;\">\"service done\"<\/span>, <span style=\"color: #800080;\">Toast<\/span>.LENGTH_SHORT).show(); <\/span>\r\n<span style=\"color: #000000;\">\u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u6bd4\u4f7f\u7528IntentService\u8981\u590d\u6742\u8bb8\u591a\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u56e0\u4e3a\u662f\u7531\u81ea\u5df1\u6765\u5904\u7406\u6bcf\u4e00\u4e2aonStartCommand()\u7684\u8c03\u7528\u800c\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u8bf7\u6c42\u3002\u672c\u8303\u4f8b\u5e76\u6ca1\u6709\u8fd9\u4e48\u505a\uff0c\u4f46\u662f\u5728\u9700\u8981\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4e3a\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u6765\u8fd0\u884c\uff08\u800c\u4e0d\u9700\u8981\u7b49\u5f85\u524d\u4e00\u4e2a\u8bf7\u6c42\u5b8c\u6210\uff09\u3002<\/p>\n<p>\u6ce8\u610f\uff0conStartCommand()\u65b9\u6cd5\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2a\u6574\u578b\u6570\u3002\u8be5\u6574\u578b\u6570\u662f\u7528\u4ee5\u63cf\u8ff0\u7cfb\u7edf\u5e94\u5f53\u5982\u4f55\u5728\u8981\u6740\u9664\u8be5\u670d\u52a1\u65f6\u8fdb\u884c\u64cd\u4f5c\u7684\u503c\uff08\u5c31\u50cf\u4e4b\u524d\u6240\u8ba8\u8bba\u7684\uff0cIntentService\u7684\u9ed8\u8ba4\u5b9e\u73b0\u5df2\u7ecf\u5904\u7406\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4fee\u6539\u5176\u884c\u4e3a\uff09\u3002onStartCommand()\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u662f\u4ee5\u4e0b\u5e38\u91cf\u4e4b\u4e00\uff1a<\/p>\n<p>START_NOT_STICKY<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728onStartCommand()\u8fd4\u56de\u540e\u6740\u9664\u670d\u52a1\uff0c\u4e14\u4e0d\u91cd\u5efa\u8be5\u670d\u52a1\uff08\u9664\u975e\u6709\u672a\u5904\u7406\u7684\u610f\u56fe\u8981\u4f20\u9012\uff09\u3002\u8981\u907f\u514d\u670d\u52a1\u5728\u4e0d\u9700\u8981\u65f6\u6216\u662f\u7a0b\u5e8f\u53ef\u4ee5\u8f7b\u677e\u5730\u91cd\u542f\u4efb\u4f55\u672a\u5b8c\u6210\u7684\u5de5\u4f5c\u65f6\u8fd0\u884c\uff0c\u8fd9\u5c06\u662f\u6700\u5b89\u5168\u7684\u9009\u9879\u3002<\/p>\n<p>START_STICKY<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728onStartCommand()\u8fd4\u56de\u540e\u6740\u9664\u670d\u52a1\uff0c\u91cd\u5efa\u8be5\u670d\u52a1\u5e76\u8c03\u7528onStartCommand()\u3002\u4e0d\u8fc7\u7cfb\u7edf\u4e0d\u4f1a\u91cd\u65b0\u4f20\u9012\u6700\u540e\u4e00\u4e2a\u610f\u56fe\uff0c\u800c\u662f\u4ee5\u4e00\u4e2a\u7a7a\uff08null\uff09\u610f\u56fe\u8c03\u7528onStartCommand()\uff08\u9664\u975e\u6709\u672a\u5904\u7406\u7684\u610f\u56fe\u8981\u542f\u52a8\u670d\u52a1\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u90a3\u4e9b\u610f\u56fe\u5c06\u88ab\u4f20\u9012\uff09\u3002\u8fd9\u4e2a\u9009\u9879\u9002\u7528\u4e8e\u5a92\u4f53\u64ad\u653e\u5668\u4e4b\u7c7b\u7684\u4e0d\u6267\u884c\u6307\u4ee4\u4f46\u662f\u8981\u6c38\u8fdc\u8fd0\u884c\u4ee5\u7b49\u5f85\u4efb\u52a1\u7684\u670d\u52a1\u3002<\/p>\n<p>START_REDELIVER_INTENT<\/p>\n<p style=\"padding-left: 30px;\">\u7cfb\u7edf\u5728onStartCommand()\u8fd4\u56de\u540e\u6740\u9664\u670d\u52a1\uff0c\u4ee5\u6700\u540e\u4f20\u9012\u7ed9\u670d\u52a1\u7684\u90a3\u4e2a\u610f\u56fe\u6765\u91cd\u5efa\u8be5\u670d\u52a1\u5e76\u8c03\u7528onStartCommand()\u3002\u6240\u6709\u672a\u5904\u7406\u7684\u610f\u56fe\u5c06\u88ab\u4f9d\u6b21\u4f20\u9012\u3002\u8fd9\u9002\u7528\u4e8e\u4e3b\u52a8\u6267\u884c\u90a3\u4e9b\u9700\u8981\u8fc5\u901f\u88ab\u7ee7\u7eed\u7684\u5de5\u4f5c\uff08\u4f8b\u5982\u4e0b\u8f7d\u6587\u4ef6\uff09\u4e4b\u7c7b\u7684\u670d\u52a1\u3002<\/p>\n<p>\u5173\u4e8e\u8fd9\u4e9b\u8fd4\u56de\u503c\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u6bcf\u4e00\u4e2a\u5e38\u91cf\u7684\u94fe\u63a5\u53c2\u8003\u6587\u6863\u3002<\/p>\n<h2>\u542f\u52a8\u4e00\u4e2a\u670d\u52a1<\/h2>\n<p>\u901a\u8fc7\u5411startService()\u4f20\u9012\u4e00\u4e2aIntent\uff08\u6765\u6307\u5b9a\u8981\u542f\u52a8\u7684\u670d\u52a1\uff09\uff0c\u53ef\u4ee5\u5728\u4e00\u4e2a\u6d3b\u52a8\u6216\u662f\u5176\u4ed6\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u4e2d\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u3002Android\u7cfb\u7edf\u5c06\u8c03\u7528\u670d\u52a1\u7684onStartCommand()\u65b9\u6cd5\u5e76\u5c06\u8be5Intent\u4f20\u9012\u7ed9\u5b83\u3002\uff08\u4e0d\u5e94\u5f53\u76f4\u63a5\u8c03\u7528onStartCommand()\u3002\uff09<\/p>\n<p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u6d3b\u52a8\u53ef\u4ee5\u663e\u5f0f\u5730\u4f7f\u7528\u610f\u56fe\u548cstartService()\u6765\u542f\u52a8\u524d\u9762\u4e00\u8282\u7684\u8303\u4f8b\u670d\u52a1\uff08<span style=\"color: #008000;\">helloService<\/span>\uff09\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">Intent <\/span>intent = <span style=\"color: #000080;\">new <\/span><span style=\"color: #800080;\">Intent<\/span>(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #800080;\">HelloService<\/span>.<span style=\"color: #000080;\">class<\/span>);<\/span>\r\n<span style=\"color: #000000;\">startService(intent);<\/span><\/pre>\n<p>startService()\u65b9\u6cd5\u4f1a\u7acb\u5373\u8fd4\u56de\uff0c\u4e4b\u540eAndroid\u7cfb\u7edf\u5c06\u8c03\u7528\u670d\u52a1\u7684onStartCommand()\u65b9\u6cd5\u3002\u5982\u679c\u670d\u52a1\u6ca1\u6709\u6b63\u5728\u8fd0\u884c\uff0c\u7cfb\u7edf\u5c06\u5148\u8c03\u7528onCreate()\uff0c\u7136\u540e\u518d\u8c03\u7528onStartCommand()\u3002<\/p>\n<p>\u5982\u679c\u670d\u52a1\u6ca1\u6709\u63d0\u4f9b\u7ed1\u5b9a\uff0c\u90a3\u4e48\u4f20\u9012\u7ed9startService()\u7684\u610f\u56fe\u5c31\u6210\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u548c\u670d\u52a1\u4e4b\u95f4\u552f\u4e00\u7684\u901a\u4fe1\u6a21\u5f0f\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5e0c\u671b\u670d\u52a1\u56de\u9001\u4e00\u4e2a\u7ed3\u679c\uff0c\u90a3\u4e48\u53ef\u4ee5\u8ba9\u542f\u52a8\u4e86\u8fd9\u4e2a\u670d\u52a1\u7684\u5ba2\u6237\u7aef\u521b\u5efa\u4e00\u4e2aPendingIntent\uff08\u914d\u5408getBroadcast()\uff09\u6765\u5e7f\u64ad\uff0c\u4e4b\u540e\u5c06\u5176\u4f20\u9012\u7ed9\u542f\u52a8\u4e86\u670d\u52a1\u7684Intent\u5185\u7684\u8fd9\u4e2a\u670d\u52a1\u3002\u8fd9\u6837\u8fd9\u4e2a\u670d\u52a1\u5c31\u53ef\u4ee5\u7528\u5e7f\u64ad\u6765\u4f20\u9001\u7ed3\u679c\u3002<\/p>\n<p>\u591a\u4e2a\u670d\u52a1\u542f\u52a8\u8bf7\u6c42\u4f1a\u5f15\u8d77\u670d\u52a1\u7684onStartCommand()\u65b9\u6cd5\u591a\u6b21\u54cd\u5e94\u8c03\u7528\u3002\u4e0d\u8fc7\uff0c\u8981\u7ec8\u6b62\u4e00\u4e2a\u670d\u52a1\u5219\u53ea\u9700\u8981\u4e00\u4e2a\u8bf7\u6c42\uff08\u914d\u5408stopSelf()\u6216stopService()\uff09\u3002<\/p>\n<h2>\u7ec8\u6b62\u4e00\u4e2a\u670d\u52a1<\/h2>\n<p>\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u5fc5\u987b\u81ea\u5df1\u7ba1\u7406\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u9664\u975e\u7cfb\u7edf\u5185\u5b58\u4e0d\u8db3\u5e76\u4e14\u670d\u52a1\u5728onStartCommand()\u8fd4\u56de\u540e\u7ee7\u7eed\u8fd0\u884c\uff0c\u5426\u5219\u7cfb\u7edf\u4e0d\u4f1a\u7ec8\u6b62\u6216\u662f\u9500\u6bc1\u8be5\u670d\u52a1\u3002\u6240\u4ee5\uff0c\u670d\u52a1\u5fc5\u987b\u901a\u8fc7\u8c03\u7528stopSelf()\u6765\u81ea\u5df1\u7ec8\u6b62\u81ea\u5df1\uff0c\u6216\u8005\u8ba9\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u8c03\u7528stopService()\u6765\u7ec8\u6b62\u5b83\u3002<\/p>\n<p>\u4e00\u65e6\u7528stopSelf()\u6216\u662fstopService()\u6765\u8bf7\u6c42\u7ec8\u6b62\uff0c\u7cfb\u7edf\u4f1a\u7acb\u5373\u9500\u6bc1\u8fd9\u4e2a\u670d\u52a1\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u5982\u679c\u670d\u52a1\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2aonStartCommand()\u8bf7\u6c42\uff0c\u90a3\u5c31\u4e0d\u5e94\u8be5\u7ec8\u6b62\u8fd9\u4e2a\u670d\u52a1\uff0c\u56e0\u4e3a\u53ef\u80fd\u8fd8\u63a5\u6536\u5230\u4e86\u4e00\u4e2a\u65b0\u7684\u542f\u52a8\u8bf7\u6c42\uff08\u5728\u4e00\u4e2a\u8bf7\u6c42\u7684\u6700\u540e\u7ec8\u6b62\u8fd9\u4e2a\u670d\u52a1\u4e5f\u5c06\u4f1a\u7ec8\u6b62\u540e\u4e00\u4e2a\u8bf7\u6c42\uff09\u3002\u8981\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528stopSelf(int)\u6765\u786e\u4fdd\u7ec8\u6b62\u670d\u52a1\u7684\u8bf7\u6c42\u662f\u603b\u4e4b\u57fa\u4e8e\u6700\u540e\u63a5\u53d7\u7684\u542f\u52a8\u8bf7\u6c42\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u8c03\u7528stopSelf(int)\u65f6\uff0c\u9700\u8981\u5c06\u7ec8\u6b62\u8bf7\u6c42\u76f8\u5bf9\u5e94\u7684\u542f\u52a8\u8bf7\u6c42\u7684ID\uff08\u5373\u4f20\u9012\u7ed9onStartCommand()\u7684startID\uff09\u4f20\u5165\u3002\u8fd9\u6837\u5982\u679c\u670d\u52a1\u5728\u8c03\u7528stopSelf(int)\u4e4b\u524d\u63a5\u6536\u5230\u4e86\u4e00\u4e2a\u65b0\u7684\u542f\u52a8\u8bf7\u6c42\uff0cID\u5c31\u4f1a\u4e0d\u7b26\u5408\uff0c\u670d\u52a1\u4fbf\u4e0d\u4f1a\u88ab\u7ec8\u6b62\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u7a0b\u5e8f\u8981\u5728\u670d\u52a1\u4e0d\u5de5\u4f5c\u65f6\u7ec8\u6b62\u5b83\u662f\u5f88\u91cd\u8981\u7684\u3002\u8fd9\u6837\u4ee5\u907f\u514d\u6d6a\u8d39\u7cfb\u7edf\u8d44\u6e90\u53ca\u6d88\u8017\u7535\u529b\u3002\u5982\u679c\u5fc5\u8981\uff0c\u53ef\u4ee5\u8ba9\u5176\u4ed6\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528stopService()\u6765\u7ec8\u6b62\u670d\u52a1\u3002\u5373\u4f7f\u5141\u8bb8\u4e86\u670d\u52a1\u7684\u7ed1\u5b9a\uff0c\u4e5f\u5fc5\u987b\u603b\u662f\u5728\u670d\u52a1\u63a5\u6536\u8fc7onStartCommand()\u7684\u8c03\u7528\u65f6\u7ec8\u6b62\u670d\u52a1\u3002\uff08\u62b1\u6b49\u8fd9\u53e5\u7684\u7ffb\u8bd1\u6709\u70b9\u95ee\u9898\uff0c\u8bf7\u53c2\u8003\u539f\u6587 Even if you enable binding for the service, you must always stop the service yourself if it ever received a call to onStartCommand().\uff09<\/p>\n<p>\u5173\u4e8e\u670d\u52a1\u751f\u547d\u5468\u671f\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u4e4b\u540e\u5173\u4e8e\u201c\u7ba1\u7406\u670d\u52a1\u751f\u547d\u5468\u671f\u201d\u7684\u5c0f\u8282\u3002<\/p>\n<h2>\u521b\u5efa\u7ed1\u5b9a\u7684\u670d\u52a1<\/h2>\n<p>\u7ed1\u5b9a\u7684\u670d\u52a1\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528bindService()\u4e0e\u4e4b\u7ed1\u5b9a\u6765\u5efa\u7acb\u4e00\u4e2a\u957f\u671f\u8fde\u63a5\uff08\u5e76\u4e14\u901a\u5e38\u4e0d\u5141\u8bb8\u7ec4\u4ef6\u901a\u8fc7\u8c03\u7528startService()\u6765\u542f\u52a8\u5b83\uff09\u3002<\/p>\n<p>\u5e94\u5f53\u5728\u5e0c\u671b\u901a\u8fc7\u7a0b\u5e8f\u5185\u7684\u6d3b\u52a8\u6216\u5176\u4ed6\u7ec4\u4ef6\u4e0e\u670d\u52a1\u4ea4\u4e92\uff0c\u6216\u8005\u662f\u4ee5\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08interprocess communication\uff0cIPC\uff09\u8ba9\u672c\u7a0b\u5e8f\u7684\u90e8\u5206\u529f\u80fd\u5f00\u653e\u7ed9\u5176\u4ed6\u7a0b\u5e8f\u65f6\uff0c\u521b\u5efa\u7ed1\u5b9a\u7684\u670d\u52a1\u3002<\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\uff0c\u5fc5\u987b\u4f7f\u7528onBind()\u56de\u9988\u65b9\u6cd5\u6765\u8fd4\u56de\u4e00\u4e2a\u5b9a\u4e49\u4e86\u4e0e\u670d\u52a1\u901a\u4fe1\u7684\u63a5\u53e3\u7684IBinder\u3002\u8fd9\u6837\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u53ef\u4ee5\u8c03\u7528bindService()\u6765\u68c0\u7d22\u8fd9\u4e2a\u63a5\u53e3\u5e76\u8c03\u7528\u670d\u52a1\u7684\u65b9\u6cd5\u3002\u8be5\u670d\u52a1\u53ea\u4e3a\u4e86\u4e0e\u4e4b\u7ed1\u5b9a\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u800c\u5b58\u5728\uff0c\u56e0\u6b64\u5f53\u6ca1\u6709\u7ec4\u4ef6\u4e0e\u4e4b\u7ed1\u5b9a\u65f6\uff0c\u7cfb\u7edf\u5c06\u9500\u6bc1\u5b83\uff08\u4e0d\u9700\u8981\u50cf\u901a\u8fc7onStartCommand()\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\u65f6\u90a3\u6837\u6765\u7ec8\u6b62\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\uff09\u3002<\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\uff0c\u8981\u505a\u7684\u7b2c\u4e00\u4ef6\u4e8b\u662f\u5b9a\u4e49\u4e00\u4e2a\u6307\u5b9a\u4e86\u5ba2\u6237\u7aef\u5982\u4f55\u4e0e\u670d\u52a1\u901a\u4fe1\u7684\u63a5\u53e3\u3002\u8fd9\u4e2a\u670d\u52a1\u4e0e\u5ba2\u6237\u7aef\u4e4b\u95f4\u7684\u63a5\u53e3\u5fc5\u987b\u662fIBinder\u7684\u4e00\u79cd\u5b9e\u73b0\uff0c\u5e76\u4e14\u5fc5\u987b\u5728\u670d\u52a1\u7684onBind()\u56de\u9988\u65b9\u6cd5\u4e2d\u88ab\u8fd4\u56de\u3002\u4e00\u65e6\u5ba2\u6237\u7aef\u63a5\u6536\u5230\u4e86IBinder\uff0c\u5b83\u5c06\u80fd\u591f\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u5f00\u59cb\u4e0e\u670d\u52a1\u4ea4\u4e92\u3002<\/p>\n<p>\u591a\u4e2a\u5ba2\u6237\u7aef\u53ef\u4ee5\u540c\u65f6\u4e0e\u4e00\u4e2a\u670d\u52a1\u7ed1\u5b9a\u3002\u5f53\u4e00\u4e2a\u5ba2\u6237\u7aef\u5b8c\u6210\u4e86\u4e0e\u670d\u52a1\u7684\u4ea4\u4e92\u540e\uff0c\u5b83\u4f1a\u8c03\u7528unbindService()\u6765\u89e3\u9664\u7ed1\u5b9a\u3002\u4e00\u65e6\u6ca1\u6709\u4efb\u4f55\u5ba2\u6237\u7aef\u4e0e\u670d\u52a1\u7ed1\u5b9a\uff0c\u7cfb\u7edf\u5c06\u9500\u6bc1\u8fd9\u4e2a\u670d\u52a1\u3002<\/p>\n<p>\u6709\u591a\u79cd\u65b9\u6cd5\u6765\u5b9e\u73b0\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\uff0c\u5b83\u4eec\u6bd4\u5b9e\u73b0\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u8981\u590d\u6742\u5f97\u591a\uff0c\u56e0\u6b64\u5c06\u5728\u4e00\u7bc7\u72ec\u7acb\u7684\u6587\u6863\u4e2d\u6765\u8ba8\u8bba\u5173\u4e8e\u7ed1\u5b9a\u7684\u670d\u52a1\u7684\u95ee\u9898\u3002<\/p>\n<h2>\u5411\u7528\u6237\u53d1\u9001\u901a\u77e5<\/h2>\n<p>\u4e00\u4e2a\u670d\u52a1\u4e00\u65e6\u8fd0\u884c\uff0c\u5b83\u5c31\u53ef\u4ee5\u901a\u8fc7<a title=\"\u521b\u5efaToast\u901a\u77e5\" href=\"http:\/\/leybreeze.com\/blog\/?p=461\">Toast\u901a\u77e5<\/a>\u6216\u662f<a title=\"\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5\" href=\"http:\/\/leybreeze.com\/blog\/?p=470\">\u72b6\u6001\u680f\u901a\u77e5<\/a>\u6765\u63d0\u793a\u7528\u6237\u5f53\u524d\u4e8b\u4ef6\u3002<\/p>\n<p>Toast\u901a\u77e5\u662f\u4e00\u79cd\u5728\u5f53\u524d\u7a97\u53e3\u8868\u5c42\u51fa\u73b0\u4e00\u6bb5\u65f6\u95f4\u540e\u4fbf\u4f1a\u6d88\u5931\u7684\u6d88\u606f\uff0c\u800c\u72b6\u6001\u680f\u901a\u77e5\u5219\u5728\u72b6\u6001\u680f\u663e\u793a\u4e00\u4e2a\u5e26\u6709\u6d88\u606f\u7684\u56fe\u6807\uff0c\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4ee5\u8fdb\u884c\u5904\u7406\uff08\u4f8b\u5982\u542f\u52a8\u4e00\u4e2a\u6d3b\u52a8\uff09\u3002<\/p>\n<p>\u901a\u5e38\uff0c \u72b6\u6001\u680f\u901a\u77e5\u662f\u540e\u53f0\u5de5\u4f5c\u5b8c\u6210\u65f6\uff08\u4f8b\u5982\u6587\u4ef6\u4e0b\u8f7d\u5b8c\u6210\uff09\u6700\u4f73\u7684\u901a\u77e5\u65b9\u5f0f\uff0c\u7528\u6237\u53ef\u4ee5\u5bf9\u5b8c\u6210\u7684\u5de5\u4f5c\u8fdb\u884c\u5904\u7406\u3002\u5f53\u7528\u6237\u4ece\u6269\u5c55\u89c6\u56fe\u4e2d\u9009\u62e9\u8be5\u901a\u77e5\uff0c\u5b83\u5c31\u4f1a\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\uff08\u6bd4\u5982\u6765\u67e5\u770b\u4e0b\u8f7d\u7684\u6587\u4ef6\uff09\u3002<\/p>\n<p>\u53c2\u89c1<a title=\"\u521b\u5efaToast\u901a\u77e5\" href=\"http:\/\/leybreeze.com\/blog\/?p=461\">Toast\u901a\u77e5<\/a>\u6216\u662f<a title=\"\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5\" href=\"http:\/\/leybreeze.com\/blog\/?p=470\">\u72b6\u6001\u680f\u901a\u77e5<\/a>\u7684\u5f00\u53d1\u6307\u5357\u4ee5\u83b7\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<h2>\u5728\u524d\u53f0\u8fd0\u884c\u670d\u52a1<\/h2>\n<p>\u524d\u53f0\u670d\u52a1\u662f\u4e00\u79cd\u7528\u6237\u80fd\u591f\u610f\u8bc6\u5230\u7684\u670d\u52a1\uff0c\u4e14\u4e0d\u4f1a\u518d\u5185\u5b58\u4e0d\u8db3\u65f6\u88ab\u7cfb\u7edf\u6740\u9664\u3002\u4e00\u4e2a\u524d\u53f0\u670d\u52a1\u5fc5\u987b\u63d0\u4f9b\u72b6\u6001\u680f\u901a\u77e5\u5e76\u5f52\u4e8e\u201c\u6b63\u5728\u8fd0\u884c\u201d\u6807\u9898\u4e4b\u4e0b\uff0c\u8fd9\u610f\u5473\u7740\u9664\u975e\u8be5\u670d\u52a1\u88ab\u7ec8\u6b62\u6216\u662f\u79fb\u51fa\u524d\u53f0\uff0c\u5b83\u5c06\u4e00\u76f4\u5b58\u5728\u4e8e\u901a\u77e5\u7a97\u53e3\u4e2d\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4e00\u4e2a\u901a\u8fc7\u670d\u52a1\u6765\u64ad\u653e\u97f3\u4e50\u7684\u97f3\u4e50\u64ad\u653e\u5668\u5e94\u5f53\u88ab\u8bbe\u7f6e\u4e3a\u8fd0\u884c\u4e8e\u524d\u53f0\uff0c\u56e0\u4e3a\u7528\u6237\u5c06\u660e\u786e\u5730\u610f\u8bc6\u5230\u5b83\u6b63\u5728\u8fd0\u884c\u3002\u72b6\u6001\u680f\u4e2d\u7684\u901a\u77e5\u5e94\u8be5\u663e\u793a\u5f53\u524d\u6b4c\u66f2\uff0c\u5e76\u5141\u8bb8\u7528\u6237\u7531\u6b64\u901a\u77e5\u542f\u52a8\u4e00\u4e2a\u6d3b\u52a8\u6765\u4e0e\u97f3\u4e50\u64ad\u653e\u5668\u4ea4\u4e92\u3002<\/p>\n<p>\u8981\u8bf7\u6c42\u670d\u52a1\u8fd0\u884c\u4e8e\u524d\u53f0\uff0c\u9700\u8981\u8c03\u7528startForeground()\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528\u4e24\u4e2a\u53c2\u6570\uff1a\u4e00\u4e2a\u552f\u4e00\u8bc6\u522b\u4e86\u901a\u77e5\u7684\u6574\u578b\u6570\u4ee5\u53ca\u4e00\u4e2a\u72b6\u6001\u680f\u7684Notification\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #800080;\">Notification <\/span>notification = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Notification<\/span>(R.drawable.icon, getText(R.<span style=\"color: #000080;\">string<\/span>.ticker_text),<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">\u00a0 \u00a0 \u00a0 \u00a0 System<\/span>.currentTimeMillis());<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">Intent <\/span>notificationIntent = <span style=\"color: #000080;\">new<\/span> <span style=\"color: #800080;\">Intent<\/span>(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #800080;\">ExampleActivity<\/span>.<span style=\"color: #000080;\">class<\/span>);<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #800080;\">PendingIntent <\/span>pendingIntent = <span style=\"color: #800080;\">PendingIntent<\/span>.getActivity(<span style=\"color: #000080;\">this<\/span>, <span style=\"color: #008080;\">0<\/span>, notificationIntent, <span style=\"color: #008080;\">0<\/span>);<\/span>\r\n<span style=\"color: #000000;\">notification.setLatestEventInfo(<span style=\"color: #000080;\">this<\/span>, getText(R.<span style=\"color: #000080;\">string<\/span>.notification_title),<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 \u00a0 \u00a0 getText(R.<span style=\"color: #000080;\">string<\/span>.notification_message), pendingIntent);<\/span>\r\n<span style=\"color: #000000;\">startForeground(ONGOING_NOTIFICATION, notification);<\/span><\/pre>\n<p>\u8981\u628a\u670d\u52a1\u4ece\u524d\u53f0\u79fb\u51fa\uff0c\u9700\u8981\u8c03\u7528stopForeground()\u3002\u8be5\u65b9\u6cd5\u9700\u8981\u4e00\u4e2a\u5e03\u5c14\u91cf\u6765\u786e\u5b9a\u662f\u5426\u9700\u8981\u540c\u65f6\u79fb\u9664\u72b6\u6001\u680f\u901a\u77e5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5e76\u4e0d\u4f1a\u7ec8\u6b62\u670d\u52a1\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u7ec8\u6b62\u4e86\u4ecd\u5728\u524d\u53f0\u8fd0\u884c\u7684\u670d\u52a1\uff0c\u90a3\u4e48\u901a\u77e5\u4e5f\u5c06\u4f1a\u88ab\u4e00\u540c\u79fb\u9664\u3002<\/p>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1astartForegound()\u548cstopForeground()\u65b9\u6cd5\u662f\u5728Android 2.0\uff08API Level 5\uff09\u4e2d\u88ab\u5f15\u5165\u7684\u3002\u4e3a\u4e86\u5728\u8f83\u65e9\u7248\u672c\u7684\u5e73\u53f0\u4e0a\u4f7f\u670d\u52a1\u5728\u524d\u53f0\u8fd0\u884c\uff0c\u6bd4\u5982\u4f7f\u7528\u8f83\u65e9\u7684<span style=\"color: #008000;\">setForeground<\/span>()\u65b9\u6cd5\u2014\u2014\u53c2\u89c1startForegound()\u6587\u6863\u4ee5\u83b7\u53d6\u5173\u4e8e\u63d0\u4f9b\u5411\u4e0b\u517c\u5bb9\u6027\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u8981\u83b7\u53d6\u5173\u4e8e\u901a\u77e5\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u201c<a title=\"\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5\" href=\"http:\/\/leybreeze.com\/blog\/?p=470\">\u521b\u5efa\u72b6\u6001\u680f\u901a\u77e5<\/a>\u201d\u3002<\/p>\n<h2>\u7ba1\u7406\u670d\u52a1\u7684\u751f\u547d\u5468\u671f<\/h2>\n<p>\u670d\u52a1\u7684\u751f\u547d\u5468\u671f\u8981\u6bd4\u6d3b\u52a8\u7684\u7b80\u5355\u8bb8\u591a\u3002\u4e0d\u8fc7\uff0c\u9700\u8981\u66f4\u52a0\u5730\u6ce8\u610f\u4e00\u4e2a\u670d\u52a1\u662f\u600e\u6837\u88ab\u521b\u5efa\u548c\u9500\u6bc1\u7684\uff0c\u56e0\u4e3a\u670d\u52a1\u53ef\u4ee5\u4e0d\u88ab\u7528\u6237\u53d1\u89c9\u5730\u5728\u540e\u53f0\u8fd0\u884c\u3002<\/p>\n<p>\u670d\u52a1\u751f\u547d\u5468\u671f\u2014\u2014\u4ece\u5b83\u88ab\u521b\u5efa\u76f4\u5230\u88ab\u9500\u6bc1\u2014\u2014\u53ef\u4ee5\u6cbf\u7740\u4e24\u6761\u4e0d\u540c\u7684\u8def\u7ebf\u53d1\u5c55\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\n<ul>\n<li>\u670d\u52a1\u4f1a\u5728\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u8c03\u7528startService()\u65f6\u88ab\u521b\u5efa\u3002\u4e4b\u540e\u670d\u52a1\u5c06\u4f1a\u6c38\u4e45\u8fd0\u884c\uff0c\u5fc5\u987b\u8c03\u7528stopSelf()\u6765\u7ec8\u6b62\u5b83\u81ea\u5df1\u3002\u4e5f\u53ef\u4ee5\u7531\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u8c03\u7528stopService()\u6765\u7ec8\u6b62\u670d\u52a1\u3002\u5f53\u670d\u52a1\u88ab\u7ec8\u6b62\u540e\uff0c\u7cfb\u7edf\u5c06\u9500\u6bc1\u5b83\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e00\u4e2a\u7ed1\u5b9a\u7684\u670d\u52a1\n<ul>\n<li>\u670d\u52a1\u4f1a\u5728\u53e6\u4e00\u4e2a\u7ec4\u4ef6\uff08\u5ba2\u6237\u7aef\uff09\u8c03\u7528bindService()\u65f6\u88ab\u521b\u5efa\u3002\u5ba2\u6237\u7aef\u4e4b\u540e\u901a\u8fc7\u4e00\u4e2aIBinder\u63a5\u53e3\u4e0e\u670d\u52a1\u901a\u4fe1\u3002\u5ba2\u6237\u7aef\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528unbindService()\u6765\u5173\u95ed\u8fde\u63a5\u3002\u591a\u4e2a\u5ba2\u6237\u7aef\u53ef\u4ee5\u4e0e\u540c\u4e00\u4e2a\u670d\u52a1\u7ed1\u5b9a\u3002\u5f53\u6240\u6709\u5ba2\u6237\u7aef\u90fd\u4e0e\u4e4b\u89e3\u9664\u7ed1\u5b9a\u540e\uff0c\u7cfb\u7edf\u5c06\u9500\u6bc1\u8fd9\u4e2a\u670d\u52a1\u3002\uff08\u670d\u52a1\u4e0d\u5fc5\u7ec8\u6b62\u81ea\u5df1\u3002\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u6761\u8def\u7ebf\u5e76\u4e0d\u662f\u5b8c\u5168\u72ec\u7acb\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u53ef\u4ee5\u4e0e\u4e00\u4e2a\u901a\u8fc7startService()\u542f\u52a8\u7684\u670d\u52a1\u76f8\u7ed1\u5b9a\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u540e\u53f0\u97f3\u4e50\u670d\u52a1\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e00\u4e2a\u6307\u5b9a\u4e86\u6240\u8981\u64ad\u653e\u7684\u97f3\u4e50\u7684Intent\u6765\u8c03\u7528startService()\u542f\u52a8\u3002\u4e4b\u540e\uff0c\u5f53\u7528\u6237\u60f3\u8981\u63a7\u5236\u97f3\u4e50\u64ad\u653e\u6216\u662f\u83b7\u53d6\u5f53\u524d\u97f3\u4e50\u7684\u4fe1\u606f\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528bindService()\u6765\u5c06\u5176\u4e0e\u4e00\u4e2a\u6d3b\u52a8\u76f8\u7ed1\u5b9a\u3002\u5728\u8fd9\u6837\u7684\u60c5\u51b5\u4e0b\uff0c\u76f4\u5230\u6240\u6709\u7684\u5ba2\u6237\u7aef\u4e0e\u4e4b\u89e3\u9664\u7ed1\u5b9a\u4e4b\u524d\uff0cstopService()\u6216\u662fstopSelf()\u5e76\u4e0d\u4f1a\u771f\u6b63\u7ec8\u6b62\u8fd9\u4e2a\u670d\u52a1\u3002<\/p>\n<h2>\u4f7f\u7528\u751f\u547d\u5468\u671f\u56de\u9988<\/h2>\n<p>\u548c\u6d3b\u52a8\u4e00\u6837\uff0c\u670d\u52a1\u4e5f\u6709\u751f\u547d\u5468\u671f\u56de\u9988\u65b9\u6cd5\u3002\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u6765\u76d1\u89c6\u670d\u52a1\u7684\u72b6\u6001\u7684\u6539\u53d8\u4ee5\u5728\u6070\u5f53\u7684\u60c5\u51b5\u4e0b\u505a\u51fa\u6070\u5f53\u7684\u52a8\u4f5c\u3002\u4e0b\u9762\u7684\u4e00\u4e2a\u670d\u52a1\u7684\u6846\u67b6\u6f14\u793a\u4e86\u6bcf\u4e00\u79cd\u751f\u547d\u5468\u671f\u65b9\u6cd5\uff1a<\/p>\n<pre><span style=\"color: #000000;\"><span style=\"color: #000080;\">public class <\/span><span style=\"color: #800080;\">ExampleService <\/span><span style=\"color: #000080;\">extends <\/span><span style=\"color: #800080;\">Service <\/span>{<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 int <\/span>mStartMode; \u00a0 \u00a0 \u00a0 <span style=\"color: #993300;\">\/\/ indicates how to behave if the service is killed<\/span><\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 IBinder mBinder; \u00a0 \u00a0 \u00a0<span style=\"color: #993300;\">\/\/ interface for clients that bind<\/span><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 boolean <\/span>mAllowRebind; <span style=\"color: #993300;\">\/\/ indicates whether onRebind should be used<\/span><\/span>\r\n\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void <\/span><code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Service.html#onCreate()\">onCreate<\/a><\/code>() {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ The service is being created<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public int <\/span><code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Service.html#onStartCommand(android.content.Intent, int, int)\">onStartCommand<\/a><\/code>(<span style=\"color: #800080;\">Intent <\/span>intent, <span style=\"color: #000080;\">int <\/span>flags, <span style=\"color: #000080;\">int <\/span>startId) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ The service is starting, due to a call to <code><a href=\"http:\/\/developer.android.com\/reference\/android\/content\/Context.html#startService(android.content.Intent)\">startService()<\/a><\/code><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return <\/span><em>mStartMode<\/em>;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public <\/span><span style=\"color: #800080;\">IBinder <\/span><code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Service.html#onBind(android.content.Intent)\">onBind<\/a><\/code>(<span style=\"color: #800080;\">Intent <\/span>intent) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ A client is binding to the service with <code><a href=\"http:\/\/developer.android.com\/reference\/android\/content\/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)\">bindService()<\/a><\/code><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return <\/span><em>mBinder<\/em>;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public boolean <\/span><code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Service.html#onUnbind(android.content.Intent)\">onUnbind<\/a><\/code>(<span style=\"color: #800080;\">Intent <\/span>intent) {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ All clients have unbound with <code><a href=\"http:\/\/developer.android.com\/reference\/android\/content\/Context.html#unbindService(android.content.ServiceConnection)\">unbindService()<\/a><\/code><\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 \u00a0 \u00a0 return <\/span><em>mAllowRebind<\/em>;<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void <\/span><code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Service.html#onRebind(android.content.Intent)\">onRebind<\/a><\/code>(<span style=\"color: #800080;\">Intent <\/span>intent) {<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ A client is binding to the service with <code><a href=\"http:\/\/developer.android.com\/reference\/android\/content\/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)\">bindService()<\/a><\/code>,<\/span><\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ after onUnbind() has already been called<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #008080;\">\u00a0 \u00a0 @Override<\/span>\r\n<span style=\"color: #000000;\"><span style=\"color: #000080;\">\u00a0 \u00a0 public void <\/span><code><a href=\"http:\/\/developer.android.com\/reference\/android\/app\/Service.html#onDestroy()\">onDestroy<\/a><\/code>() {<\/span>\r\n<span style=\"color: #993300;\">\u00a0 \u00a0 \u00a0 \u00a0 \/\/ The service is no longer used and is being destroyed<\/span>\r\n<span style=\"color: #000000;\">\u00a0 \u00a0 }<\/span>\r\n<span style=\"color: #000000;\">}<\/span><\/pre>\n<figure id=\"attachment_529\" aria-describedby=\"caption-attachment-529\" style=\"width: 432px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/service_lifecycle.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-529\" title=\"service_lifecycle\" src=\"http:\/\/leybreeze.com\/blog\/wp-content\/uploads\/2011\/04\/service_lifecycle.png\" alt=\"\" width=\"432\" height=\"521\" \/><\/a><figcaption id=\"caption-attachment-529\" class=\"wp-caption-text\">\u56fe2. \u670d\u52a1\u7684\u751f\u547d\u5468\u671f\u3002\u5de6\u8fb9\u7684\u56fe\u8868\u5c55\u793a\u4e86\u7531startService()\u521b\u5efa\u7684\u670d\u52a1\u7684\u751f\u547d\u5468\u671f\uff0c\u53f3\u8fb9\u7684\u56fe\u6807\u5219\u662f\u7531bindService()\u521b\u5efa\u7684\u670d\u52a1\u7684\u751f\u547d\u5468\u671f\u3002<\/figcaption><\/figure>\n<p style=\"text-align: center;\">\u6ce8\u610f\uff1a\u548c\u6d3b\u52a8\u7684\u751f\u547d\u5468\u671f\u56de\u9988\u65b9\u6cd5\u4e0d\u540c\uff0c\u4e0d\u9700\u8981\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7236\u7c7b\u5b9e\u73b0\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u76d1\u89c6\u670d\u52a1\u751f\u547d\u5468\u671f\u4e2d\u7684\u4e24\u4e2a\u5d4c\u5957\u5faa\u73af\uff1a<\/p>\n<ul>\n<li>\u4e00\u4e2a\u670d\u52a1\u7684\u6574\u4e2a\u751f\u547d\u671f\u4eceonCreate()\u5f00\u59cb\uff0c\u81f3onDestroy()\u8fd4\u56de\u65f6\u7ed3\u675f\u3002\u548c\u6d3b\u52a8\u4e00\u6837\uff0c\u4e00\u4e2a\u670d\u52a1\u5728onCreate()\u4e2d\u8fdb\u884c\u521d\u59cb\u5316\u5de5\u4f5c\u5e76\u5728onDestroy()\u4e2d\u91ca\u653e\u6240\u6709\u5269\u4f59\u8d44\u6e90\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u97f3\u4e50\u64ad\u653e\u670d\u52a1\u5728onCreate()\u4e2d\u521b\u5efa\u97f3\u4e50\u64ad\u653e\u7ebf\u7a0b\uff0c\u5728onDestroy()\u4e2d\u7ec8\u6b62\u7ebf\u7a0b\u3002onCreate()\u548conDestroy()\u65b9\u6cd5\u80fd\u88ab\u6240\u6709\u7684\u670d\u52a1\u8c03\u7528\uff0c\u4e0d\u8bba\u5b83\u662f\u7531startService()\u8fd8\u662fbindService()\u521b\u5efa\u7684\u3002<\/li>\n<li>\u4e00\u4e2a\u670d\u52a1\u7684\u6d3b\u52a8\u751f\u547d\u671f\uff08active lifetime\uff09\u4eceonStartCommand()\u6216\u662fonBind()\u7684\u8c03\u7528\u5f00\u59cb\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5c06\u5206\u522b\u5904\u7406\u4f20\u9012\u7ed9startService()\u6216\u662fbindService()\u7684Intent\u3002\u5982\u679c\u670d\u52a1\u662f\u542f\u52a8\u7684\uff08started\uff09\uff0c\u5219\u6d3b\u52a8\u751f\u547d\u671f\u7684\u7ed3\u675f\u548c\u6574\u4e2a\u751f\u547d\u671f\u76f8\u540c\uff08\u670d\u52a1\u5728onStartCommand()\u8fd4\u56de\u540e\u4f9d\u7136\u662f\u6d3b\u52a8\u7684\uff09\u3002\u5982\u679c\u670d\u52a1\u662f\u7ed1\u5b9a\u7684\uff08bound\uff09\uff0c\u90a3\u4e48\u6d3b\u52a8\u751f\u547d\u671f\u5c06\u4f1a\u5728onUnbind()\u8fd4\u56de\u65f6\u7ed3\u675f\u3002<\/li>\n<\/ul>\n<p style=\"padding-left: 30px;\"><strong>\u6ce8\u610f<\/strong>\uff1a\u867d\u7136\u4e00\u4e2a\u542f\u52a8\u7684\u670d\u52a1\u5728\u8c03\u7528stopSelf()\u6216stopService()\u65f6\u7ec8\u6b62\uff0c\u4f46\u5bf9\u6b64\u5374\u6ca1\u6709\u4e00\u4e2a\u4e13\u95e8\u7684\u56de\u9988\u65b9\u6cd5\uff08\u6ca1\u6709onStop()\u56de\u9988\u65b9\u6cd5\uff09\u3002\u6240\u4ee5\uff0c\u9664\u975e\u670d\u52a1\u4e0e\u5ba2\u6237\u7aef\u76f8\u7ed1\u5b9a\uff0c\u5426\u5219\u7cfb\u7edf\u5c06\u4f1a\u5728\u670d\u52a1\u7ec8\u6b62\u65f6\u9500\u6bc1\u5b83\u2014\u2014onDestroy()\u662f\u552f\u4e00\u4f1a\u88ab\u63a5\u6536\u7684\u56de\u9988\u65b9\u6cd5\u3002<\/p>\n<p>\u56fe2\u63cf\u7ed8\u4e86\u4e00\u4e2a\u670d\u52a1\u7684\u5178\u578b\u56de\u9988\u65b9\u6cd5\u3002\u5c3d\u7ba1\u5b83\u533a\u5206\u4e86\u901a\u8fc7startService()\u548cbindService()\u521b\u5efa\u7684\u670d\u52a1\uff0c\u8981\u6ce8\u610f\u4efb\u4f55\u670d\u52a1\uff0c\u65e0\u8bba\u662f\u5982\u4f55\u88ab\u521b\u5efa\u7684\uff0c\u90fd\u5141\u8bb8\u88ab\u5ba2\u6237\u7aef\u7ed1\u5b9a\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a\u6700\u521d\u7531onStartCommand()\u542f\u52a8\u7684\u670d\u52a1\uff08\u7531\u4e00\u4e2a\u5ba2\u6237\u7aef\u8c03\u7528startService()\uff09\u4e5f\u53ef\u4ee5\u63a5\u6536onBind()\u8c03\u7528\uff08\u5728\u5ba2\u6237\u7aef\u8c03\u7528bindService()\u65f6\uff09\u3002<\/p>\n<p>\u5173\u4e8e\u521b\u5efa\u53ef\u4ee5\u7ed1\u5b9a\u7684\u670d\u52a1\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u89c1\u201c\u7ed1\u5b9a\u7684\u670d\u52a1\u201d\u6587\u6863\u3002\u5176\u4e2d\u5305\u542b\u4e86\u201c\u7ba1\u7406\u7ed1\u5b9a\u7684\u670d\u52a1\u7684\u751f\u547d\u5468\u671f\u201d\u4e00\u8282\u4e2donRebind()\u56de\u9988\u65b9\u6cd5\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p>\u672c\u9875\u90e8\u5206\u5185\u5bb9\u6839\u636e<a title=\"Android Open Source Projec\" href=\"http:\/\/code.google.com\/intl\/zh-CN\/policies.html\" target=\"_blank\">Android Open Source Project<\/a>\u521b\u4f5c\u5e76\u5171\u4eab\u7684\u5185\u5bb9\u4fee\u6539\uff0c\u5e76\u5728<a title=\"\u77e5\u8bc6\u5171\u4eab \u7f72\u540d2.5\u8bb8\u53ef\u534f\u8bae\" href=\"http:\/\/creativecommons.org\/licenses\/by\/2.5\/\" target=\"_blank\">\u77e5\u8bc6\u5171\u4eab \u7f72\u540d2.5\u8bb8\u53ef\u534f\u8bae<\/a>\u4e2d\u6240\u8ff0\u6761\u6b3e\u7684\u9650\u5236\u4e0b\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u670d\u52a1\u662f\u4e00\u79cd\u53ef\u4ee5\u5728\u540e\u53f0\u6267\u884c\u957f\u671f\u8fd0\u884c\u64cd\u4f5c\u800c\u4e0d\u63d0\u4f9b\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u3002\u53e6\u5916\u7684\u67d0\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\u53ef\u4ee5\u542f\u52a8\u4e00\u4e2a\u670d\u52a1\uff0c &hellip; <a href=\"http:\/\/leybreeze.com\/blog\/?p=490\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u670d\u52a1\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10],"tags":[58],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/490"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=490"}],"version-history":[{"count":25,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/490\/revisions"}],"predecessor-version":[{"id":502,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/490\/revisions\/502"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=490"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}