关于 BroadcastReceiver 类的 Android WiFi Direct (P2P) 问题

Posted

技术标签:

【中文标题】关于 BroadcastReceiver 类的 Android WiFi Direct (P2P) 问题【英文标题】:Andoird WiFi Direct (P2P) question about BroadcastReceiver class 【发布时间】:2021-02-27 11:37:03 【问题描述】:

我正在尝试开发 WiFi Direct 数据传输应用程序,this official help page 是我目前的信息来源。不过,这句话我没看懂-

For the class' constructor, you will use parameters for the WifiP2pManager, WifiP2pManager.Channel, and the activity that this broadcast receiver will be registered in.

他们所说的活动是下面类中的私有成员-

/**
* A BroadcastReceiver that notifies of important Wi-Fi p2p events.
*/
public class WiFiDirectBroadcastReceiver extends BroadcastReceiver 

   private WifiP2pManager manager;
   private Channel channel;
   private MyWiFiActivity activity; // <-- THIS ONE

   public WiFiDirectBroadcastReceiver(WifiP2pManager manager, Channel channel,
           MyWifiActivity activity) 
       super();
       this.manager = manager;
       this.channel = channel;
       this.activity = activity;
   


当我尝试按原样实现该类时,我收到错误 Cannot resolve symbol MyWiFiActivity

我应该包含该类型的导入,还是取决于我以后需要做的其他事情?

【问题讨论】:

【参考方案1】:

他们所指的活动是在与教程关联的sample project 中创建的。它注册广播接收器并处理生命周期事件等。您应该更改对您选择在其中实现相应回调的任何类的引用。

【讨论】:

以上是关于关于 BroadcastReceiver 类的 Android WiFi Direct (P2P) 问题的主要内容,如果未能解决你的问题,请参考以下文章

BroadCastReceiver的注册

BroadcastReceiver之实现锁屏解锁样例

关于BroadCastReceiver安全性的思考

BroadcastReceiver 的 onReceive 中 Intent extra 为空

BroadcastReceiver 是不是只监听变化?

BroadcastReceiver