<\/figure>\n<\/p>\n \/\/ \u5b9a\u4e49\u8017\u65f6\u64cd\u4f5c\u7684\u903b\u8f91<\/p>\n
runnable = new Runnable() {<\/p>\n
@Override<\/p>\n
public void run() {<\/p>\n
\/\/ \u4e0e\u670d\u52a1\u5668\u4ea4\u4e92\u7684\u903b\u8f91<\/p>\n
handler.postDelayed(runnable, 5000);<\/p>\n
}<\/p>\n
};<\/p>\n
}<\/p>\n
@Override<\/p>\n
public int onStartCommand(Intent intent, int flags, int startId) {<\/p>\n
handler.postDelayed(runnable, 5000); \/\/ \u542f\u52a8\u8017\u65f6\u64cd\u4f5c\u7684\u7ebf\u7a0b<\/p>\n
return super.onStartCommand(intent, flags, startId);<\/p>\n
}<\/p>\n
@Override<\/p>\n
public void onDestroy() {<\/p>\n
super.onDestroy();<\/p>\n
handler.removeCallbacks(runnable); \/\/ \u505c\u6b62\u8017\u65f6\u64cd\u4f5c\u7684\u7ebf\u7a0b<\/p>\n
}<\/p>\n
}<\/p>\n
“`<\/p>\n
\u5728 AndroidManifest.xml \u4e2d\u6ce8\u518c Service\uff1a<\/p>\n
“`xml<\/p>\n<\/p>\n
“`<\/p>\n
\u5728\u9700\u8981\u542f\u52a8 Service \u7684\u5730\u65b9\uff0c\u8c03\u7528\uff1a<\/p>\n
“`java<\/p>\n
Intent intent = new Intent(context, MyService.class);<\/p>\n
context.startService(intent);<\/p>\n
“`<\/p>\n
\u8fd9\u6837\uff0c\u5c31\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u540e\u53f0\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7531\u4e8e Service \u662f\u5728\u540e\u53f0\u8fd0\u884c\u7684\uff0c\u5982\u679c\u9700\u8981\u4e0e UI \u8fdb\u884c\u4ea4\u4e92\uff0c\u9700\u8981\u4f7f\u7528 Handler \u6216\u5176\u4ed6\u7ebf\u7a0b\u901a\u4fe1\u673a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u53ef\u4ee5\u505a\u540e\u53f0\uff0c\u4f46\u662f\u9700\u8981\u5229\u7528 Android \u7684 Service \u673a\u5236\u3002Service \u662f Android \u4e2d\u7684\u4e00\u79cd\u540e\u53f0\u8fd0\u884c\u7684\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u5728\u7a0b\u5e8f\u5173\u95ed\u540e\u7ee7\u7eed\u8fd0\u884c\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a1. \u521b\u5efa<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5489,10405,1638,10406,6152],"class_list":{"0":"post-5515","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-5489","8":"tag-app","9":"tag-1638","11":"tag-6152"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/5515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=5515"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/5515\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=5515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=5515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=5515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}