LocalBroadcastManager.sendBroadcast()的返回值(布尔值)未记录。
我认为当注册至少一个接收器时它返回true,如果没有注册接收器则返回false。这是对的吗?
有关此缺失文档的公开问题:Issue 59626
答案 0 :(得分:3)
是的,它检查可用的接收器,如果没有找到则返回false:
来自method 的
219 ArrayList<ReceiverRecord> receivers = null;
220 for (int i=0; i<entries.size(); i++) {
221 ReceiverRecord receiver = entries.get(i);
222 if (debug) Log.v(TAG, "Matching against filter " + receiver.filter);
223
224 if (receiver.broadcasting) {
225 if (debug) {
226 Log.v(TAG, " Filter's target already added");
227 }
228 continue;
229 }
230
231 int match = receiver.filter.match(action, type, scheme, data,
232 categories, "LocalBroadcastManager");
233 if (match >= 0) {
234 if (debug) Log.v(TAG, " Filter matched! match=0x" +
235 Integer.toHexString(match));
236 if (receivers == null) {
237 receivers = new ArrayList<ReceiverRecord>();
238 }
239 receivers.add(receiver);
240 receiver.broadcasting = true;
241 } else {
242 if (debug) {
243 String reason;
244 switch (match) {
245 case IntentFilter.NO_MATCH_ACTION: reason = "action"; break;
246 case IntentFilter.NO_MATCH_CATEGORY: reason = "category"; break;
247 case IntentFilter.NO_MATCH_DATA: reason = "data"; break;
248 case IntentFilter.NO_MATCH_TYPE: reason = "type"; break;
249 default: reason = "unknown reason"; break;
250 }
251 Log.v(TAG, " Filter did not match: " + reason);
252 }
253 }
254 }
255
256 if (receivers != null) {
257 for (int i=0; i<receivers.size(); i++) {
258 receivers.get(i).broadcasting = false;
259 }
260 mPendingBroadcasts.add(new BroadcastRecord(intent, receivers));
261 if (!mHandler.hasMessages(MSG_EXEC_PENDING_BROADCASTS)) {
262 mHandler.sendEmptyMessage(MSG_EXEC_PENDING_BROADCASTS);
263 }
264 return true;
265 }
266 }
267 }
268 return false;