Android WiFi P2P广播接收器onReceive方法Intent来源疑问
Hey Ramzan! Let's clarify exactly where that Intent in your onReceive method is coming from—this is a super common point of confusion when working with BroadcastReceivers, so let's break it down simply.
Short Answer
The Intent passed to onReceive(Context context, Intent intent) is created and sent by the Android system (or another app/component) when the specific broadcast event you're listening for occurs. In your case, it's the Android system sending this Intent whenever the WiFi P2P state changes.
Breakdown Using Your Code
Let's tie this directly to your implementation to make it concrete:
First, here's your BroadcastReceiver code (formatted for readability):
@Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) { int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); if (state == WifiP2pManager.WIFI_P2P_STATE_ENABLED) { Toast.makeText(context, "Wifi is ON", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "Wifi is OFF", Toast.LENGTH_SHORT).show(); } } }
And your MainActivity setup:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_find_device); wifiP2pManager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); channel = wifiP2pManager.initialize(this, getMainLooper(), null); broadcastReceiver = new WiFiDirectBroadcastReceiver(wifiP2pManager, channel, this); intentFilter = new IntentFilter(); intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION); }
Here's the Step-by-Step Flow:
- You tell Android what to listen for: By creating an
IntentFilterwithWifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTIONand registering yourWiFiDirectBroadcastReceiverwith this filter (I assume you do this later in your code, like viaregisterReceiver), you're essentially saying: "Notify me whenever the WiFi P2P state switches on or off." - The system detects a state change: When the user toggles WiFi P2P, or the system adjusts its state automatically, Android's framework builds a dedicated Intent for this event. This Intent includes:
- The action string
WIFI_P2P_STATE_CHANGED_ACTION(so your receiver knows exactly which event fired) - An extra value
EXTRA_WIFI_STATEholding the new state (enabled or disabled)
- The action string
- The system delivers the Intent: Android automatically passes this pre-built Intent to your receiver's
onReceivemethod. That's why you can callintent.getAction()to identify the trigger, andgetIntExtrato pull out the state details.
Answering Your First Question
When you call intent.getAction() in your receiver, you're accessing the action string that the system embedded in the Intent it sent when the broadcast event happened. This isn't directly coming from your own IntentFilter—instead, the system matches your filter's action to the event's action, then sends you the corresponding Intent with that action string included.
For context: If you were working with a custom broadcast (not a system one), you'd manually create an Intent with your own action string and send it via sendBroadcast(intent)—in that case, the Intent would come from your own code. But for system broadcasts like WiFi P2P state changes, the system handles all the Intent creation and delivery work for you.
内容的提问来源于stack exchange,提问作者Ramzan

