简体   繁体   English

我正在调用 getAction() 函数的意图值来自哪里? (这是 BroadcastReceiver 类的重写方法)

[英]From where value of intent is coming on which I am calling getAction() function? (This is overridden method of BroadcastReceiver class)

I have created a broadcast receiver class Which is working fine.我创建了一个工作正常的广播接收器类。 I am confused about from where the value of intent comes in onReceive method.我对 onReceive 方法中 Intent 值的来源感到困惑。

 @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();
           }
        }
}

Main Activity code where I have made an Intent Filter, where I have added an action.主要活动代码,我在其中创建了一个意图过滤器,我在其中添加了一个操作。

    @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);



        }

If your BroadcastReceiver works, then you must have initialized wifiP2pManager and called a function on the object.如果您的 BroadcastReceiver 工作,那么您必须初始化wifiP2pManager并在对象上调用一个函数。 When tbe state changes an intent is being broadcasted.当 tbe 状态改变时,一个意图被广播。 With the receiver you subscribe to those intents.通过接收器,您可以订阅这些意图。

You can read in more detail about the WifiP2pManager in the Android documentationWi-Fi Direct (peer-to-peer or P2P) overview您可以在 Android 文档Wi-Fi Direct(点对点或 P2P)概述中阅读有关 WifiP2pManager 的更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM