唤醒锁和Wi-Fi锁无法正常工作

7

我已经阅读了很多有关WakeLock和WifiLock的教程和SO上的帖子,但仍然没有解决我的问题。

我正在编写一个应用程序,当您启动它时,唯一的效果是创建和启动(前台)服务。此服务运行两个线程,一个是UDP广播监听器(使用java.io),另一个是TCP服务器(使用java.nio)。在服务的onCreate中,我获取了一个wakelock和一个wifilock,并在onDestroy中释放它们。

只要手机保持唤醒状态,一切都正常,但是当显示屏关闭时,UDP广播接收器停止接收广播消息,并且在我再次打开显示屏之前将不会收到任何消息。实际上,锁根本没有起作用,无论我把它们放在哪里……我错在哪里?我确定我在某个地方做了一些愚蠢的事情,但自己找不出来。

这是一些代码:

这是Activity所做的:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    onlyStartService = true;
}@Override
protected void onStart() {
    super.onStart();
    Intent bIntent = new Intent(this, FatLinkService.class);
    getApplicationContext().startService(bIntent);
    getApplicationContext().bindService(bIntent, flConnection, BIND_AUTO_CREATE);
} 
// service connection to bind idle service
private ServiceConnection flConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder binder) {
       linkService.MyLinkBinder flBinder = (LinkService.MylinkBinder) binder;
       flServiceInstance = flBinder.getService();
       if (onlyStartService) {
           condLog("Service bound and finishing activity...");
           finish();
       }
    }
    public void onServiceDisconnected(ComponentName className) {
        flServiceInstance = null;
    }
}; 
@Override
protected void onStop() {
    super.onStop();

    if (fatFinish) {
        Intent bIntent = new Intent(this, FatLinkService.class);
        flServiceInstance.stopServices();
        flServiceInstance.stopForeground(true);
        flServiceInstance.stopService(bIntent);
        condLog("Service stop and unbound");
        flServiceInstance = null;
    }
    getApplicationContext().unbindService(flConnection);
}

这就是服务的方式:

public class LinkService extends Service {
    InetAddress iaIpAddr, iaNetMask, iaBroadcast;
    private final IBinder mBinder = new MyLinkBinder();
    private linklistenBroadcast flBroadServer = null;
    private linkTCPServer flTCPServer = null;
    private linkUDPClient flBroadClient = null;
    List<String> tokens = new ArrayList<String>();
    private PowerManager.WakeLock wakeLock;
    private WifiManager.WifiLock wifiLock;

public class MylLinkBinder extends Binder {
    lLinkService getService() { return LinkService.this; }
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onCreate() {
    super.onCreate();
    getLocks();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{        
    instantiateServices();
    // notifies presence to other fat devices
    condLog("Service notifying fat presence...");
    flBroadClient = new LinkUDPClient();
    flBroadClient.startSending(LinkProtocolConstants.BRCMD_PRESENCE + String.valueOf(LinkProtocolConstants.tcpPort), iaBroadcast, LinkProtocolConstants.brPort);
    return START_STICKY;
}

public void getLocks() {
    // acquire a WakeLock to keep the CPU running
    condLog("Acquiring power lock");
    WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
    wifiLock.acquire();
    PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    wakeLock.acquire();
}

public void stopServices() {
    if (flTCPServer != null)
        flTCPServer.stopServer();
    if (flBroadServer != null)
        flBroadServer.stopSelf();
}

private void instantiateServices() {
    populateAddresses(); // just obtain iaIpAddr  
    if (flTCPServer == null) {
        condLog("Instantiating TCP server");
        flTCPServer = new LinkTCPServer(iaIpAddr, FatLinkProtocolConstants.tcpPort);
        flTCPServer.execute();
    }
    if (flBroadServer == null) {
        condLog("Instantiating UDP broadcast server");
        Intent notifyIntent = new Intent(this, LinkMain.class); // this is the main Activity class
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notifyIntent.setAction("FROM_NOTIFICATION");
        PendingIntent notifyPIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);

        Notification fixNotification = new Notification.Builder(getApplicationContext())
                .setContentTitle("Link")
                .setSmallIcon(R.mipmap.imgLink)
                .setContentIntent(notifyPIntent)
                .build();
        startForeground(1234, fixNotification);
        flBroadServer = new LinklistenBroadcast();
        flBroadServer.start();
    }
}

private final class LinklistenBroadcast extends Thread {
    private boolean bStopSelf = false;
    DatagramSocket socket;
    public void stopSelf() {
        bStopSelf = true;
        socket.close();
    }

    @Override
    public void run() {
        condLog( "Listening broadcast thread started");
        bStopSelf = false;
        try {
        //Keep a socket open to listen to all the UDP trafic that is destinated for this port
        socket = new DatagramSocket(null);
        socket.setReuseAddress(true);
        socket.setSoTimeout(LinkGeneric.BR_SOTIMEOUT_MILS);
        socket.setBroadcast(true);
        socket.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), FatLinkProtocolConstants.brPort));
        while (true) {
                condLog("Ready to receive broadcast packets...");
                //Receive a packet
                byte[] recvBuf = new byte[1500];

                DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
                try {
                    socket.receive(packet);
                } catch (InterruptedIOException sException) {
                    condLog(sockExcept.toString());
                    break;
                } catch (SocketException sockExcept) {
                   condLog(sockExcept.toString());
                }
                if (bStopSelf) {
                    condLog("Broadcast server stopped...");
                    break;
                }
                int len = packet.getLength();
                String datarecvd = new String(packet.getData()).trim();
                //datarecvd = datarecvd.substring(0, len);
                //Packet received
                String message = new String(packet.getData()).trim();
                condLog("<<< broadcast packet received from: " + packet.getAddress().getHostAddress() + " on port: " + packet.getPort() + ", message: " + message);
                if (packet.getAddress().equals(iaIpAddr)) {
                    condLog("Ooops, it's me! discarding packet...");
                    continue;
                }
                else
                    condLog("<<< Packet received; data size: " + len + " bytes, data: " +  datarecvd);

                //See if the packet holds the right command (message)

                // protocol decode
                // here do some tuff
        } catch (IOException ex) {
            condLog(ex.toString());
        }

        if (socket.isBound()) {
            condLog( "Closing socket");
            socket.close();
        }
        condLog( "UDP server thread end.");
        flTCPServer = null;
        flBroadServer = null;
    }

    public boolean isThreadRunning() {
        return !bStopSelf;
    };
}

// Utility functions
public boolean checkBroadcastConnection (DatagramSocket socket, int timeOutcycles) {
    int tries = 0;
    while (!socket.isConnected()) {
        tries++;
        if (tries >= timeOutcycles)
            return false;
    }
    return true;
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (wakeLock != null) {
        if (wakeLock.isHeld()) {
            wakeLock.release();                
        }
    }
    if (wifiLock != null) {
        if (wifiLock.isHeld()) {
            wifiLock.release();
        }
    }
}

最后,这是清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxxxxx.ink" >
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/imgLinkmascotte"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LinkMain"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".LinkService" />
    </application>
</manifest>
  • 我已经阅读了这篇文章,我怀疑问题是一样的,但实际上在我尝试过的所有手机上都发生了(Galaxy Tab 7.1、Galaxy Tag 10、Galaxy SIII、Galaxy Note 3 Neo、Galaxy SIII mini),而且它们的Android版本从4.0到4.4不等。

  • 我已经尝试了这里发布的解决方案,但什么也没有改变(又是一次令人沮丧的尝试)。

  • 我甚至尝试在我尝试过的所有手机上将“保持wifi选项处于待机状态”设置为“始终”,但仍然没有任何改变。

  • 我研究了WakeFulIntentService类,据说它可以像每个人说的那样工作,但我在我的代码中看不到任何显着的不同。

我真的希望有人能帮助我,自上周以来,我一直卡在这个问题上。

编辑:遵循waqaslam的答案,我在一个具有“Wifi优化”选项的设备上进行了检查,只要我取消选中该选项,它就可以正常工作。那么,现在问题变成了:我能否在没有显示高级菜单中该选项的设备上禁用Wifi优化?正如我在下面的评论中写的那样,这似乎与Android版本无关,因为我有两个(都是三星)带有4.4.2的设备,它们没有显示该选项。

新编辑:根据编辑后的waqaslam答案,我尝试将multicastlock添加到我的服务中,但仍然没有任何改变。这真的很烦人,在Android上几乎没有简单明了的解决方案。

非常感谢 C.


你的安卓版本和设备是什么? - waqaslam
感谢您的评论。我把它们写在了文章末尾。更确切地说,我已经尝试过4.0.x(现在不太记得了)、4.1.2和4.4.2。 - Cristiano Zambon
我也遇到了同样烦人的问题。你找到解决方案了吗? - Srikanth
不行。使用广播对我来说根本不可能。 - Cristiano Zambon
3个回答

1
写这篇文章只是为了标记帖子已被回答。我最终意识到,没有解决方案,或者至少没有适用于“所有”手机和“所有”Android发行版(或至少从JB开始)的解决方案。
通过重新审视我的应用程序的概念,我解决了个人问题,考虑到: - 发送UDP广播即使在空闲模式下也始终有效 - TCP服务器不受WiFi优化的影响。
感谢您所有的帮助和建议。 C.

“即使在空闲模式下,发送UDP广播也总是有效的…”,您是不是指的是TCP? - waqaslam
我的服务每2秒钟会发送一个UDP广播包,即使在关闭显示的空闲模式下也可以正常工作。你真的能用TCP发送/接收广播吗? - Cristiano Zambon
不,你不能用TCP实现这个功能。但作为替代,我建议你采用客户端-服务器模型,其中你的客户端(移动设备)通过TCP套接字向服务器注册。或者更好的选择是寻找基于XMPP的解决方案,它在网络消息处理方面更为复杂和可靠(但需要一些复杂的实现)。 - waqaslam
感谢waqaslam提供的宝贵建议。不幸的是,在我的情况下,客户端/服务器模型不适用,或者更好的说法是适用,但只有在广播用于通知网络中所有节点存在后才使用它。 - Cristiano Zambon
@CristianoZambon 我也遇到了同样的问题。一开始我使用了组播。我尝试切换到广播,但是我至少在一些手机上测试后发现广播也不可靠。只有TCP单播在所有Android设备上都是可靠的。 - greywolf82

1
我认为问题不在于WakeLocks,而在于Wi-Fi设置。
在较新版本的Android中,有一个额外的设置在“设置-〉Wi-Fi-〉高级”中称为“Wi-Fi优化”,如果打开此设置,则会在显示关闭时禁用所有低优先级通信(例如监听UDP广播)。
禁用该选项应允许您的设备在显示关闭时仍能够监听UDP广播。

Wi-Fi optimization


当屏幕关闭时,您可以使用WifiManager.MulticastLock来获取WiFi锁定,以便监听这些特殊广播。

WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = wifi.createMulticastLock("lockWiFiMulticast");
lock.setReferenceCounted(false);
lock.acquire();

而当锁完成时,调用以下内容:
lock.release();

此外,不要忘记在您的清单文件中添加以下权限:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>

如需更多信息,您可以阅读this


谢谢您的回答。在我的Note 3 Neo上,使用的是Android 4.4.2版本,我没有找到这个选项,我只有一个叫做“保持WiFi连接”的选项,我将它设为“始终”。 - Cristiano Zambon
那些有这个选项的设备怎么样?你可以在它们上面试试吗?我几个月前也遇到了类似的问题,禁用那个选项帮助我解决了它。 - waqaslam
是的,我刚在安卓4.4.2的One plus One上尝试了一下,这个选项是存在的。取消勾选这个选项后,它确实可以工作。那么,新的问题是,如何让没有这个选项的设备也能够工作呢? - Cristiano Zambon
从技术上讲,没有此选项的设备默认情况下应该可以正常工作。但是如果它们不能正常工作,则是经典案例,即设备制造商(使用自定义ROM)限制了此功能以补偿性能(如电池寿命)和/或安全性。但是,您仍然可以通过使用“MulticastLock”来获得此功能。请参阅我的更新答案。 - waqaslam
非常感谢您的评论和回答。这次我真的希望它是解决方案,但不幸的是,当显示器关闭时,服务仍然没有响应。谢谢。 - Cristiano Zambon

0

可能原因是this

从Android 6.0(API级别23)开始,Android引入了两个节电功能,通过管理设备未连接电源时应用程序的行为来延长用户的电池寿命。当设备长时间未使用时,Doze通过推迟应用程序的后台CPU和网络活动来减少电池消耗。App Standby会推迟与用户最近没有交互的应用程序的后台网络活动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接