【问题标题】:How does Android/BroadcastReceiver determine if Wifi P2P is enabled?Android/BroadcastReceiver如何判断Wifi P2P是否开启?
【发布时间】:2016-12-28 16:14:33
【问题描述】:

我一直在关注 Google 的 WIFIDirect 演示,但在单击负责调用 discoverPeers() 的 acitionBar 按钮时,我收到一条消息,告诉我“WifiDirect 未启用”。这与广播接收器在下面的 onReceive sn-p 上检查的布尔值相关联:

if(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION.equals(action)){

    //Update UI to show WifiP2p status
    int state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,-1);

    if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){
        //Wifi Direct Enabled
        //we keep the WifiP2pEnabled boolean within the activity,
        //So it wont change, and can be followed
        activity.setIsWifiP2pEnabled(true);
    }else{
        activity.setIsWifiP2pEnabled(false);
        activity.resetData();
    }
    Log.d(WIFIDirectActivity.TAG,"P2P state changed" +state);

所以它自然不会在我的 LG G3 上读取我的 WIFI_P2P_STATE_ENABLED,但市场上的其他 WifiDirect 应用程序似乎可以工作?我已手动将布尔值设置为 true,在这种情况下,它可以正常工作并发现/连接到对等点而不会出现问题。

真的是我的安卓版本(4.4.2)有问题吗?

谢谢

【问题讨论】:

  • 您是否设置了具有正确意图过滤器和操作的接收器?
  • 你挖到了金子。我在接收器中留下了一个。谢谢你,祝你新年快乐!
  • 很高兴听到这个消息。当我开始使用接收器时,我也忘记了很多。也祝你新年快乐:)

标签: android android-broadcastreceiver wifip2p


【解决方案1】:

基于有根据的猜测以及我已经忘记了一百万次的事情。这里的问题是对清单的添加不完整。

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:theme="@style/AppTheme" >
   <receiver android:name="MyReceiver">

      <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED">
             <!-- BOOT_COMPLETED can be any action you might want to receive -->
         </action>
      </intent-filter>

   </receiver>
</application>

您在上面看到了一个示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多