android – 防止WiFi-direct在不使用时关闭

我的目标是API 17,并使用下面的代码启用WiFi直连(P2P).
一切正常(找到并连接到同行),但是当没有使用wifi-direct时不断关闭(看起来它取决于Android手机 – 我的约3-5分钟)这也将关掉WiFi让我失去了互联网连接.

我有一个接收器,可以检测P2P状态何时发生变化以重新打开它,但即使没有连接到任何对等端,也能保持P2P始终处于开启状态.

是否有可能继续ping Android手机本身才能做到这一点?还有其他建议吗?

public void onReceive(Context context,Intent intent) {
    String action = intent.getAction();
    if (wifip2pManager.WIFI_P2P_STATE_CHANGED_ACTION.equals(action)) {

        // UI update to indicate wifi p2p status.
        int state = intent.getIntExtra(wifip2pManager.EXTRA_WIFI_STATE,-1);
        if (state == wifip2pManager.WIFI_P2P_STATE_ENABLED) {
            // Wifi Direct mode is enabled
            activity.setIswifip2pEnabled(true);
        } else {
            activity.setIswifip2pEnabled(false);
            activity.resetData();

        }
最佳答案
你应该获得WifiLock.或者你呢?

来自WakeLock的javadoc条目:

normally the Wi-Fi radio may turn off when the user has not used the
device in a while. Acquiring a WifiLock will keep the radio on until
the lock is released.

在WifiLock上查看更多信息:
http://developer.android.com/reference/android/net/wifi/WifiManager.WifiLock.html

阅读回答这个问题:Is using WakeLock overkill while using WifiLock on Android?

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...