{"id":901,"date":"2015-07-21T16:03:22","date_gmt":"2015-07-21T08:03:22","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=901"},"modified":"2026-08-18T12:58:43","modified_gmt":"2026-08-18T04:58:43","slug":"%e5%b9%bf%e6%92%adbroadcastreceiver","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=901","title":{"rendered":"\u5e7f\u64adBroadcastReceiver"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\">\u5e7f\u64ad\u662f\u5e94\u7528\u4e4b\u95f4\u4f20\u6d88\u606f\u7684\u673a\u5236\u3002\u7535\u6c60\u72b6\u6001\u3001\u6765\u7535\u3001\u77ed\u4fe1\u90fd\u4f1a\u53d1\u51fa\u5e7f\u64ad\u3002\u5f00\u53d1\u8005\u5b9e\u73b0 <strong>BroadcastReceiver<\/strong> \u76d1\u542c\u8fd9\u4e9b Intent\uff0c\u518d\u505a\u4e1a\u52a1\u5904\u7406\u3002\u53d1\u9001\u9760 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">Context.sendBroadcast()<\/code> \/ <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">sendOrderedBroadcast()<\/code>\uff0c\u540c\u4e00\u6761\u5e7f\u64ad\u53ef\u88ab\u591a\u4e2a\u8ba2\u9605\u8005\u6536\u5230\u3002<\/p>\n<\/blockquote>\n<p><!--more--><\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e00\u3001\u5e7f\u64ad\u793a\u610f\u56fe\u4e0e\u4e24\u79cd\u6ce8\u518c<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">Android \u5e7f\u64ad\u793a\u610f\u56fe\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150721155254.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-902\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150721155254.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150721155254\" width=\"536\" height=\"315\" \/><\/a><\/p>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u9759\u6001\u5e7f\u64ad\uff08Manifest \u6ce8\u518c\uff09<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class MyBroadcastReceiver extends BroadcastReceiver {\n    String SMS_RECEIVED = \"android.provider.Telephony.SMS_RECEIVED\";\n\n    public void onReceive(Context context, Intent intent) {\n       if (intent.getAction().equals(SMS_RECEIVED)) {\n           \/\/ \u76f8\u5173\u5904\u7406\uff1a\u5730\u57df\u53d8\u6362\u3001\u7535\u91cf\u4e0d\u8db3\u3001\u6765\u7535\u6765\u4fe1\n       }\n   }\n}<\/code><\/pre>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;receiver android:name=\".MyBroadcastReceiver\"&gt;\n    &lt;intent-filter android:priority=\"1000\"&gt;\n        &lt;action android:name=\"android.provider.Telephony.SMS_RECEIVED\" \/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/receiver&gt;\n\n&lt;uses-permission android:name=\"android.permission.RECEIVE_SMS\" \/&gt;\n&lt;uses-permission android:name=\"android.permission.SEND_SMS\" \/&gt;<\/code><\/pre>\n<h3 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em;\">\u52a8\u6001\u5e7f\u64ad\uff08\u4ee3\u7801\u6ce8\u518c\uff09<\/h3>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {\n    @Override\n    public void onReceive(Context context, Intent intent) {\n        \/\/ \u76f8\u5173\u5904\u7406\uff0c\u5982\u6536\u77ed\u4fe1\uff0c\u76d1\u542c\u7535\u91cf\u53d8\u5316\n    }\n};\n\nIntentFilter intentFilter = new IntentFilter(\"android.provider.Telephony.SMS_RECEIVED\");\nregisterReceiver(myBroadcastReceiver, intentFilter);<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e8c\u3001\u751f\u547d\u5468\u671f\uff1aonReceive \u522b\u505a\u91cd\u6d3b<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u6bcf\u6b21\u5e7f\u64ad\u5230\u6765\u90fd\u4f1a<strong>\u65b0\u5efa<\/strong> Receiver \u5bf9\u8c61\u3001\u8c03\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">onReceive()<\/code>\uff0c\u6267\u884c\u5b8c\u5bf9\u8c61\u5373\u9500\u6bc1\u3002\u751f\u547d\u5468\u671f\u5927\u7ea6\u5341\u79d2\uff1b<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">onReceive<\/code> \u8d85\u8fc7\u7ea6 10 \u79d2\uff0c\u7cfb\u7edf\u4f1a\u5f53\u65e0\u54cd\u5e94\uff0c\u5f39\u51fa ANR\u3002<\/p>\n<p style=\"margin: 1em 0px;\">\u8017\u65f6\u5de5\u4f5c\u5e94\u53d1 Intent \u7ed9 <strong>Service<\/strong> \u53bb\u505a\uff0c<strong>\u4e0d\u8981\u5f00\u5b50\u7ebf\u7a0b\u7cca\u5f04<\/strong>\uff1aReceiver \u5f88\u5feb\u7ed3\u675f\uff0c\u6240\u5728\u8fdb\u7a0b\u53d8\u6210\u7a7a\u8fdb\u7a0b\uff0c\u5185\u5b58\u7d27\u5f20\u65f6\u4f18\u5148\u88ab\u6740\uff0c\u5b50\u7ebf\u7a0b\u4e00\u8d77\u6b7b\u3002<\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e09\u3001\u5e7f\u64ad\u7c7b\u578b\u4e0e\u6ce8\u518c\u6ce8\u9500<\/span><\/h2>\n<table style=\"border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; text-align: left; overflow: auto; display: table; overflow-wrap: break-word; word-break: break-all;\" width=\"817\">\n<thead>\n<tr>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u7c7b\u578b<\/th>\n<th style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top; font-weight: bold; background-color: #fff9f9; color: #ef7060;\">\u884c\u4e3a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u666e\u901a \/ \u65e0\u5e8f\u5e7f\u64ad<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u6240\u6709\u76d1\u542c\u8005\u90fd\u80fd\u6536\u5230<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u6709\u5e8f\u5e7f\u64ad<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u6309 intent-filter \u7684 priority\uff08-1000\uff5e1000\uff0c\u8d8a\u5927\u8d8a\u5148\uff09\u63a5\u6536\uff1b\u53ef\u6539\u5185\u5bb9\uff1b<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">abortBroadcast()<\/code> \u53ef\u4e2d\u6b62\u5411\u4e0b\u4f20\u64ad<\/td>\n<\/tr>\n<tr>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\">\u7c98\u6027 \/ \u5f02\u6b65\u5e7f\u64ad<\/td>\n<td style=\"font-size: 0.75em; padding: 9px 12px; line-height: 22px; border: 1px solid #ef7060; vertical-align: top;\"><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">sendStickyBroadcast(intent)<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">\u4ee3\u7801\u6ce8\u518c\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">registerReceiver(BroadcastReceiver, IntentFilter)<\/code>\u3002\u6ce8\u9500\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">unregisterReceiver(...)<\/code>\u3002Activity \u91cc\u5efa\u8bae\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; font-size: 0.9em; overflow-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0px 2px; color: #ef7060; background-color: rgba(27, 31, 35, 0.05); word-break: break-all;\">onPause()<\/code> \u6ce8\u9500\u3002<\/p>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u56db\u3001\u5b9e\u4f8b\uff1a\u77ed\u4fe1 \/ \u81ea\u5b9a\u4e49 \/ \u7535\u8bdd\u72b6\u6001<\/span><\/h2>\n<p style=\"margin: 1em 0px;\">\u6b65\u9aa4\uff1a1. \u5b9a\u4e49 Receiver\uff1b2. \u6ce8\u518c\u3002\u4e0b\u9762\u540c\u65f6\u5904\u7406\u77ed\u4fe1\u3001\u81ea\u5b9a\u4e49 Action\u3001\u53bb\u7535\u3001\u6765\u7535\u72b6\u6001\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">public class SmsBroadCastReceiver extends BroadcastReceiver {\n    private static boolean incomingFlag = false;\n    private static String incoming_number = null;\n\n    @Override\n    public void onReceive(Context context, Intent intent) {\n        String action = intent.getAction();\n\n        if (action.equals(\"android.provider.Telephony.SMS_RECEIVED\")) {\n            Toast.makeText(context, \"\u63a5\u6536\u77ed\u6d88\u606f\u9759\u6001\u5e7f\u64ad\", Toast.LENGTH_SHORT).show();\n            Logs.v(\"\u63a5\u6536\u77ed\u6d88\u606f\u9759\u6001\u5e7f\u64ad &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; \");\n\n        } else if (action.equals(MyReceiverActivity.ACTION_SCXH)) {\n            Toast.makeText(context, \"\u81ea\u5b9a\u4e49\u9759\u6001\u5e7f\u64ad\u63a5\u6536\u5230\", Toast.LENGTH_SHORT).show();\n            Logs.v(\"\u81ea\u5b9a\u4e49\u9759\u6001\u5e7f\u64ad &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; \");\n\n        } else if (action.equals(\"android.intent.action.NEW_OUTGOING_CALL\")) {\n            incomingFlag = false;\n            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);\n            Logs.i(\"\u62e8\u6253\u7535\u8bdd:\" + phoneNumber);\n\n        } else if (action.equals(\"android.intent.action.PHONE_STATE\")) {\n            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);\n            switch (tm.getCallState()) {\n            case TelephonyManager.CALL_STATE_RINGING:\n                incomingFlag = true;\n                incoming_number = intent.getStringExtra(\"incoming_number\");\n                Logs.i(\"\u6765\u7535  :\" + incoming_number);\n                break;\n            case TelephonyManager.CALL_STATE_OFFHOOK:\n                if (incomingFlag) {\n                    Logs.i(\"\u6458\u673a\uff08\u6b63\u5728\u901a\u8bdd\u4e2d\uff09 :\" + incoming_number);\n                }\n                break;\n            case TelephonyManager.CALL_STATE_IDLE:\n                if (incomingFlag) {\n                    Logs.i(\"\u7a7a\u95f2 &gt;&gt;&gt;&gt;&gt;\");\n                }\n                break;\n            }\n        }\n    }\n}<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">Manifest \u6ce8\u518c\u4e0e\u6743\u9650\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">&lt;receiver android:name=\"com.warmtel.android.receiver.systemreceiver.SmsBroadCastReceiver\" &gt;\n    &lt;intent-filter&gt;\n        &lt;action android:name=\"android.provider.Telephony.SMS_RECEIVED\" \/&gt;\n        &lt;action android:name=\"android.intent.action.PHONE_STATE\" \/&gt;\n        &lt;action android:name=\"android.intent.action.NEW_OUTGOING_CALL\" \/&gt;\n        &lt;action android:name=\"com.warmtel.android.receiver_ACTION_XH\" \/&gt;\n    &lt;\/intent-filter&gt;\n&lt;\/receiver&gt;\n\n&lt;uses-permission android:name=\"android.permission.READ_PHONE_STATE\" \/&gt;\n&lt;uses-permission android:name=\"android.permission.PROCESS_OUTGOING_CALLS\" \/&gt;<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u53d1\u9001\u81ea\u5b9a\u4e49\u5e7f\u64ad\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">Intent intent = new Intent(\"com.warmtel.android.receiver_ACTION_XH\");\nsendBroadcast(intent);<\/code><\/pre>\n<hr>\n<h2 style=\"margin: 1.2em 0px 1em; padding: 0px; font-weight: bold; font-size: 1.3em; border-bottom: 2px solid #ef7060; display: flex;\"><span style=\"display: inline-block; font-weight: bold; background: #ef7060; color: #ffffff; padding: 3px 10px 1px; border-top-right-radius: 3px; border-top-left-radius: 3px; margin-right: 3px;\">\u4e94\u3001\u7535\u8bdd\u76d1\u542c PhoneStateListener<\/span><\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 10px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 10px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; margin: 0.5em; padding: 3px 5px; overflow-x: auto; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">private void receiverPhoneStateListener(){\n    TelephonyManager tpm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);\n    tpm.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE);\n}\nclass MyPhoneStateListener extends PhoneStateListener {\n    @Override\n    public void onCallStateChanged(int state, String incomingNumber) {\n        switch (state) {\n        case TelephonyManager.CALL_STATE_IDLE:\n            Logs.d(\"\u7a7a\u95f2 &gt;&gt;&gt;&gt;&gt;  \");\n            break;\n        case TelephonyManager.CALL_STATE_RINGING:\n            Logs.v(\"\u6765\u7535 &gt;&gt;&gt;&gt;&gt;  \");\n            break;\n        case TelephonyManager.CALL_STATE_OFFHOOK:\n            Logs.w(\"\u6458\u673a\uff08\u6b63\u5728\u901a\u8bdd\u4e2d\uff09 &gt;&gt;&gt;&gt;&gt;  \");\n            break;\n        }\n    }\n}<\/code><\/pre>\n<blockquote style=\"margin: 20px 0px; display: block; font-size: 0.9em; overflow: auto; border-left: 3px solid #ef7060; color: #6a737d; padding: 10px 10px 10px 20px; background: #fff9f9;\">\n<p style=\"margin: 1em 0px;\"><strong>\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aBroadcastReceiver \u8fc7\u6ee4 Intent\uff1b\u9759\u6001\u5199 Manifest\u3001\u52a8\u6001 register\/unregister\u3002onReceive \u8981\u5feb\uff0c\u91cd\u6d3b\u4ea4\u7ed9 Service\u3002\u6709\u5e8f\u5e7f\u64ad\u9760 priority\uff0cabortBroadcast \u53ef\u622a\u6d41\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u5e7f\u64ad\u662f\u5e94\u7528\u4e4b\u95f4\u4f20\u6d88\u606f\u7684\u673a\u5236\u3002\u7535\u6c60\u72b6\u6001\u3001\u6765\u7535\u3001\u77ed\u4fe1\u90fd\u4f1a\u53d1\u51fa\u5e7f\u64ad\u3002\u5f00\u53d1\u8005\u5b9e\u73b0 BroadcastReceiver  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[199],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/901"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=901"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions"}],"predecessor-version":[{"id":908,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/901\/revisions\/908"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}