如何保持从无线断开连接时手机是睡着了吗? [英] How do I keep Wifi from disconnecting when phone is asleep?

查看:111
本文介绍了如何保持从无线断开连接时手机是睡着了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个轮询服务器以一定的时间间隔的服务。我用一个AlarmManager和一个BroadcastReceiver来启动服务。我的问题是,在一段时间后,即使无线上网仍处于启用状态,但出于某种原因,我的应用程序无法联系服务器。我得到一个无法访问网络错误。

请注意,我已经获得了部分唤醒锁,以及一个wifilock。

下面是我的code代表的BroadcastReceiver。

 公共类ServiceAlarmBroadcastReceiver扩展的BroadcastReceiver {
    公共无效的onReceive(上下文的背景下,意图意图){
        WakeLock wakeLock = NULL;
        WifiLock wifiLock = NULL;
        尝试 {
            电源管理器PM =(电源管理器)上下文
                    .getSystemService(Context.POWER_SERVICE);

            //获取WakeLock保持CPU运行
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    MyWakeLock);
            如果(!wakeLock.isHeld()){
                wakeLock.acquire();
            }

            Log.i(ServiceAlarmBroadcastReceiver,WakeLock后天!);


            WifiManager WM =(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULLMyWifiLock);
            如果(!wifiLock.isHeld()){
                wifiLock.acquire();
            }

            Log.i(ServiceAlarmBroadcastReceiver,WifiLock后天!);
            context.startService(新意图(背景下,ThePollerService.class));
        } 最后 {
            //释放WakeLock让CPU睡觉
            如果(wakeLock!= NULL){
                如果(wakeLock.isHeld()){
                    wakeLock.release();
                    Log.i(ServiceAlarmBroadcastReceiver,WakeLock发布!);
                }
            }

            //释放WifiLock
            如果(wifiLock!= NULL){
                如果(wifiLock.isHeld()){
                    wifiLock.release();
                    Log.i(ServiceAlarmBroadcastReceiver,无线网络锁发布!);
                }
            }
        }
    }
}
 

解决方案

与code的问题张贴在这里是你获取和右释放WakeLock和WifiLock你的接收机内。除非你完成你的服务的ONSTART(其中,如果你是,为什么要花费心思服务580)为在你的整个任务,您的投票任务完成之前,锁将被释放。

我建议改变你的执行类似如下:

  1. 在具有广播接收器启动服务(这是所有)
  2. 有服务获取唤醒锁,并揭开序幕线程做你的轮询操作。最适当的位置将是您的服务的onCreate)
  3. 在您的查询操作完成后,你应该停止轮询服务
  4. 在您的服务的onDestroy,你应该释放你ONSTART获取的锁定

I have a service which polls a server at certain intervals. I use an AlarmManager and a BroadcastReceiver to start the service. My problem is that after a certain duration, even though the Wifi is still enabled, but for some reason, my application can't contact the server. I get an "Unreachable network" error.

Note that I've already acquired a partial wake lock as well as a wifilock.

Here's my code for the BroadcastReceiver.

public class ServiceAlarmBroadcastReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        WakeLock wakeLock = null;
        WifiLock wifiLock = null;
        try {
            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);

            // acquire a WakeLock to keep the CPU running
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    "MyWakeLock");
            if(!wakeLock.isHeld()){
                wakeLock.acquire();
            }

            Log.i("ServiceAlarmBroadcastReceiver", "WakeLock acquired!");


            WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL , "MyWifiLock");
            if(!wifiLock.isHeld()){
                wifiLock.acquire();
            }

            Log.i("ServiceAlarmBroadcastReceiver", "WifiLock acquired!");
            context.startService(new Intent(context, ThePollerService.class));
        } finally {
            // release the WakeLock to allow CPU to sleep
            if (wakeLock != null) {
                if (wakeLock.isHeld()) {
                    wakeLock.release();
                    Log.i("ServiceAlarmBroadcastReceiver", "WakeLock released!");
                }
            }

            // release the WifiLock
            if (wifiLock != null) {
                if (wifiLock.isHeld()) {
                    wifiLock.release();
                    Log.i("ServiceAlarmBroadcastReceiver", "WiFi Lock released!");
                }
            }
        }
    }
}

解决方案

The problem with the code posted here is that you acquire and release the WakeLock and WifiLock from right inside of your receiver. Unless you are completing your entire task inside of the onStart of your service (which if you are, why even bother having a service???), the locks will be released before your polling task completes.

I would suggest changing your implementation to something like the following:

  1. Have broadcast receiver start service (and that is all)
  2. Have service acquire wake locks and kick off the thread to do your polling operation. The most appropriate spot would be your service onCreate)
  3. After your polled operation is complete, you should stop your polling service
  4. In the onDestroy of your service, you should release the locks you acquired in onStart

这篇关于如何保持从无线断开连接时手机是睡着了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆