{"id":166,"date":"2017-02-01T23:40:14","date_gmt":"2017-02-01T15:40:14","guid":{"rendered":"http:\/\/localhost\/wordpress\/?p=166"},"modified":"2017-02-02T00:11:24","modified_gmt":"2017-02-01T16:11:24","slug":"android-broadcast-receiver","status":"publish","type":"post","link":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/","title":{"rendered":"[Android] Broadcast Receiver"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>\u53c3\u8003\u9023\u7d50\uff1ahttps:\/\/developer.android.com\/guide\/components\/broadcasts.html<\/p>\n<p>\u4e00\u822c\u4f86\u8aaa Broadcast Receiver \u53ef\u4ee5\u5206\u6210\u4ee5\u4e0b\u5169\u7a2e\uff1a<br \/>\n1. Manifest-declared Receiver<br \/>\n\u5b9a\u7fa9 receiver \u5728 manifest \u4e2d\uff1a<\/p>\n<p><receiver android:name=\".MyBroadcastReceiver\"  android:exported=\"true\"><br \/>\n    <intent-filter><br \/>\n        <action android:name=\"android.intent.action.BOOT_COMPLETED\"\/><br \/>\n        <action android:name=\"android.intent.action.INPUT_METHOD_CHANGED\" \/><br \/>\n    <\/intent-filter><br \/>\n<\/receiver><\/p>\n<p>\u5b9a\u7fa9 receiver \u6210\u4e00\u500b BroadcastReceiver \u7684 subclass\uff0c\u4e26\u4e14\u8981\u57f7\u884c OnReceive \u7684\u529f\u80fd\uff0c\u5982\u540c\u4ee5\u4e0b\uff1a<\/p>\n<p>public class MyBroadcastReceiver extends BroadcastReceiver {<br \/>\n    private static final String TAG = &#8220;MyBroadcastReceiver&#8221;;<br \/>\n    @Override<br \/>\n    public void onReceive(Context context, Intent intent) {<br \/>\n        StringBuilder sb = new StringBuilder();<br \/>\n        sb.append(&#8220;Action: &#8221; + intent.getAction() + &#8220;\\n&#8221;);<br \/>\n        sb.append(&#8220;URI: &#8221; + intent.toUri(Intent.URI_INTENT_SCHEME).toString() + &#8220;\\n&#8221;);<br \/>\n        String log = sb.toString();<br \/>\n        Log.d(TAG, log);<br \/>\n        Toast.makeText(context, log, Toast.LENGTH_LONG).show();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u9019\u6a23\u5728\u7a0b\u5f0f\u5b89\u88dd\u7684\u6642\u5019\uff0c\u9019\u500b Receiver \u5c31\u6703\u88ab\u7368\u7acb\u5b89\u88dd\u5728\u6d3b\u52d5\u4e4b\u5916\uff0c\u56e0\u6b64\u9019\u500b Receiver \u53ef\u4ee5\u8b8a\u6210\u555f\u52d5\u7a0b\u5f0f\u7684\u4e00\u500b\u95dc\u9375\u3002<\/p>\n<p>2. Context-registered Receiver<\/p>\n<p>\u53e6\u5916\u4e5f\u53ef\u4ee5\u8a3b\u518a\u4e00\u500b Receiver \u7d66\u4e00\u500b\u7279\u5b9a\u7684 Context\uff0c\u5229\u7528\u4ee5\u4e0b\u7684\u65b9\u6cd5\uff1a<br \/>\n\u65b0\u589e\u4e00\u500b\u65b0\u5b9a\u7fa9\u7684 MyBroadcastReceiver:<\/p>\n<p>BroadcastReceiver br = new MyBroadcastReceiver();<\/p>\n<p>\u65b0\u5b9a\u7fa9\u4e00\u500b IntentFilter \u65b0\u589e\u5176\u61c9\u8a72\u8981\u53cd\u61c9\u7684\u529f\u80fd\uff0c\u7136\u5f8c\u8a3b\u518a\u9019\u500b Receiver\u3002\u65b0\u8a3b\u518a\u7684 BroadcastReceiver \u6703\u8ddf\u8457 Context \u4e00\u8d77\u9032\u884c\uff0c\u6240\u4ee5\u5982\u679c\u8a3b\u518a\u5728\u4e00\u500b Activity \u7684\u8a71\uff0c\u53ea\u8981\u9019\u500b Activity \u6c92\u6709\u88ab\u6467\u6bc0\u7684\u8a71\uff0c\u5ee3\u64ad\u90fd\u6703\u6709\u6548\uff0c\u5982\u679c\u662f\u8a3b\u518a\u5728\u4e00\u500b Application \u7684\u8a71\uff0c\u53ea\u8981\u9019\u500b Application \u90fd\u9084\u5728\u904b\u884c\uff0c\u5ee3\u64ad\u63a5\u6536\u529f\u80fd\u90fd\u4e0d\u6703\u5931\u6548\u3002<\/p>\n<p>IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);<br \/>\nintentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);<br \/>\nthis.registerReceiver(br, filter);<\/p>\n<p>\u4e00\u822c\u4f86\u8aaa\uff0cMyBroadcastReceiver \u6703\u57f7\u884c\u4efb\u4f55\u7684\u547c\u53eb(Intent)\uff0c\u4f46\u662f\u4f7f\u7528 intentfilter \u53ef\u4ee5\u6e1b\u5c11\u547c\u53eb\u7684\u7e3d\u985e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u9023\u7d50\uff1ahttps:\/\/developer.android.com\/guide\/components\/broadcasts.html \u4e00\u822c\u4f86\u8aaa Broadcast Receiver \u53ef\u4ee5\u5206\u6210\u4ee5\u4e0b\u5169\u7a2e\uff1a 1. Manifest-declared Receiver \u5b9a\u7fa9 receiver \u5728 manifest \u4e2d\uff1a \u5b9a\u7fa9 receiver \u6210\u4e00\u500b BroadcastReceiver \u7684 subclass\uff0c\u4e26\u4e14\u8981\u57f7\u884c OnReceive<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Android] Broadcast Receiver - \u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Android] Broadcast Receiver - \u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane\" \/>\n<meta property=\"og:description\" content=\"\u53c3\u8003\u9023\u7d50\uff1ahttps:\/\/developer.android.com\/guide\/components\/broadcasts.html \u4e00\u822c\u4f86\u8aaa Broadcast Receiver \u53ef\u4ee5\u5206\u6210\u4ee5\u4e0b\u5169\u7a2e\uff1a 1. Manifest-declared Receiver \u5b9a\u7fa9 receiver \u5728 manifest \u4e2d\uff1a \u5b9a\u7fa9 receiver \u6210\u4e00\u500b BroadcastReceiver \u7684 subclass\uff0c\u4e26\u4e14\u8981\u57f7\u884c OnReceive\" \/>\n<meta property=\"og:url\" content=\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\" \/>\n<meta property=\"og:site_name\" content=\"\u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-01T15:40:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-02-01T16:11:24+00:00\" \/>\n<meta name=\"author\" content=\"\u6ab8\u6aac\u7238\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u6ab8\u6aac\u7238\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\"},\"author\":{\"name\":\"\u6ab8\u6aac\u7238\",\"@id\":\"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b\"},\"headline\":\"[Android] Broadcast Receiver\",\"datePublished\":\"2017-02-01T15:40:14+00:00\",\"dateModified\":\"2017-02-01T16:11:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\"},\"wordCount\":117,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b\"},\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\",\"url\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\",\"name\":\"[Android] Broadcast Receiver - \u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane\",\"isPartOf\":{\"@id\":\"https:\/\/myoceane.fr\/#website\"},\"datePublished\":\"2017-02-01T15:40:14+00:00\",\"dateModified\":\"2017-02-01T16:11:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/myoceane.fr\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Android] Broadcast Receiver\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/myoceane.fr\/#website\",\"url\":\"https:\/\/myoceane.fr\/\",\"name\":\"M-Y-Oceane \u60f3\u65b9\u6d89\u6cd5\u3002\u91cf\u74f6\u5916\u7684\u5929\u7a7a\",\"description\":\"\u60f3\u65b9\u6d89\u6cd5, France, Taiwan, Health, Information Technology\",\"publisher\":{\"@id\":\"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/myoceane.fr\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b\",\"name\":\"\u6ab8\u6aac\u7238\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/myoceane.fr\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6cc678684664f8ad45a8d56a6630b183?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6cc678684664f8ad45a8d56a6630b183?s=96&d=mm&r=g\",\"caption\":\"\u6ab8\u6aac\u7238\"},\"logo\":{\"@id\":\"https:\/\/myoceane.fr\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/myoceane.fr\/index.php\/author\/johnny5584767gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Android] Broadcast Receiver - \u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/","og_locale":"en_US","og_type":"article","og_title":"[Android] Broadcast Receiver - \u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane","og_description":"\u53c3\u8003\u9023\u7d50\uff1ahttps:\/\/developer.android.com\/guide\/components\/broadcasts.html \u4e00\u822c\u4f86\u8aaa Broadcast Receiver \u53ef\u4ee5\u5206\u6210\u4ee5\u4e0b\u5169\u7a2e\uff1a 1. Manifest-declared Receiver \u5b9a\u7fa9 receiver \u5728 manifest \u4e2d\uff1a \u5b9a\u7fa9 receiver \u6210\u4e00\u500b BroadcastReceiver \u7684 subclass\uff0c\u4e26\u4e14\u8981\u57f7\u884c OnReceive","og_url":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/","og_site_name":"\u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane","article_published_time":"2017-02-01T15:40:14+00:00","article_modified_time":"2017-02-01T16:11:24+00:00","author":"\u6ab8\u6aac\u7238","twitter_card":"summary_large_image","twitter_misc":{"Written by":"\u6ab8\u6aac\u7238","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#article","isPartOf":{"@id":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/"},"author":{"name":"\u6ab8\u6aac\u7238","@id":"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b"},"headline":"[Android] Broadcast Receiver","datePublished":"2017-02-01T15:40:14+00:00","dateModified":"2017-02-01T16:11:24+00:00","mainEntityOfPage":{"@id":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/"},"wordCount":117,"commentCount":0,"publisher":{"@id":"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b"},"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/","url":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/","name":"[Android] Broadcast Receiver - \u60f3\u65b9\u6d89\u6cd5 - \u91cf\u74f6\u5916\u7684\u5929\u7a7a M-Y-Oceane","isPartOf":{"@id":"https:\/\/myoceane.fr\/#website"},"datePublished":"2017-02-01T15:40:14+00:00","dateModified":"2017-02-01T16:11:24+00:00","breadcrumb":{"@id":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/myoceane.fr\/index.php\/android-broadcast-receiver\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/myoceane.fr\/"},{"@type":"ListItem","position":2,"name":"[Android] Broadcast Receiver"}]},{"@type":"WebSite","@id":"https:\/\/myoceane.fr\/#website","url":"https:\/\/myoceane.fr\/","name":"M-Y-Oceane \u60f3\u65b9\u6d89\u6cd5\u3002\u91cf\u74f6\u5916\u7684\u5929\u7a7a","description":"\u60f3\u65b9\u6d89\u6cd5, France, Taiwan, Health, Information Technology","publisher":{"@id":"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/myoceane.fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/myoceane.fr\/#\/schema\/person\/4a4552fb8c27693083d465e12db7658b","name":"\u6ab8\u6aac\u7238","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/myoceane.fr\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6cc678684664f8ad45a8d56a6630b183?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6cc678684664f8ad45a8d56a6630b183?s=96&d=mm&r=g","caption":"\u6ab8\u6aac\u7238"},"logo":{"@id":"https:\/\/myoceane.fr\/#\/schema\/person\/image\/"},"url":"https:\/\/myoceane.fr\/index.php\/author\/johnny5584767gmail-com\/"}]}},"amp_enabled":false,"_links":{"self":[{"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":3,"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":169,"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/posts\/166\/revisions\/169"}],"wp:attachment":[{"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myoceane.fr\/index.php\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}