无线上网睡觉,即使有锁 [英] Wifi sleeps, even with Lock

查看:109
本文介绍了无线上网睡觉,即使有锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:即使被收购的wifi锁,当手机电池运行,无线网络是一段时间后断开连接。

Summary: even when wifi lock is acquired, when the phone is running on batteries, WiFi is disconnected after a while.

我已经简化问题,以一个单一的活动与一个按钮,启动一个线程。它只是发出100.000串到PC(一个字符串每100ms)上运行的回显服务器。见下面code。我可以看到流量使用Wireshark,而且回声服务器显示的字符串。请注意,WiFi和电动锁是如何开始发送​​之前获得(和之后发布的,当然)。

I've simplified the problem to a single activity with a button that launches a thread. It just sends 100.000 strings to an echo server running on a PC (one string every 100ms). See code below. I can see the traffic with WireShark, and also the echo server shows the strings. Notice how WiFi and power locks are acquired before starting to send (and released after, of course).

然而,当手机电池运行和用户关闭手机,则会持续发送串了一段时间,然后无线网络连接断开,手机甚至不响应Ping。它从600到699到6000系列,以断开(数字是圆的,所以我觉得他们是很重要的)。

However, when the phone is running on battery and the user turns off the phone, it keeps sending strings for some time and then WiFi is disconnected and the phone does not even respond to ping. It takes from 600s to 6000s to be disconnected (the figures are that round, so I think they are important).

它完美地工作,当A / C连接,所以我想这是主题相关的电源管理。

It perfectly works when A/C is connected, so I guess it is somehow related to power management.

要测试它,我只是启动活动,启动响应服务器,开始是Wireshark,preSS的开始按钮(安卓的onClick =DOSTART ),块的电话,并让它在桌子上。我去吃午饭或任何与600-6000s后,我可以看到的是Wireshark的TX错误,回声服务器已停止接收流量和手机不响应Ping。

To test it I just launch the activity, start the echo server, start WireShark, press the "Start" button (android:onClick="doStart"), blocks the phone and let it on the table. I go for lunch or whatever and after 600-6000s I can see the tx errors on WireShark, the echo server has stopped receiving traffic and the phone does not respond to ping.

这款手机是2.2,带WiFi的政策设置为15米后休眠。

The phone is 2.2, with WiFi policy set to "sleep after 15m".

package Odroid.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.Button;

public class Test extends Activity {
  PowerManager _powerManagement = null;
  PowerManager.WakeLock _wakeLock = null;
  WifiManager.WifiLock _wifiLock = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  public void doStart(View v) {
    DoerThreadFake t = new DoerThreadFake();
    t.start();
  }

  private class DoerThreadFake extends Thread {
    public void run() {
      runOnUiThread(new Runnable() {
        public void run() {
          ((Button) findViewById(R.id.start)).setText("Doing...");
        }
      });
      _keepOnStart();
      Socket s;
      byte[] buffer = new byte[1000];

      try {
        s = new Socket("192.168.0.16", 2000);
        PrintStream ps = new PrintStream(s.getOutputStream());
        InputStream is = s.getInputStream();
        for (int i = 0; i < 100000; i++) {
          ps.println(System.currentTimeMillis() +"("+(new Date()).toString() +") : " + i);
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          while (is.available() > 0) {
            int a = is.available();
            if (a > 1000) a = 1000;
            is.read(buffer, 0, a); // Clean echo
          }
        }
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      _keepOnStop();
      runOnUiThread(new Runnable() {
        public void run() {
          ((Button) findViewById(R.id.start)).setText("Done");
        }
      });
    }

    private void _keepOnStart() {
      if (_powerManagement == null) {
        _powerManagement = (PowerManager) getSystemService(Context.POWER_SERVICE);
      }
      if (_wakeLock == null) {
        _wakeLock = _powerManagement.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "0 Backup power lock");
      }
      _wakeLock.acquire();
      WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      if (wifiManager != null) {
        _wifiLock = wifiManager.createWifiLock("0 Backup wifi lock");
        _wifiLock.acquire();
      }
    }

    private void _keepOnStop() {
      if ((_wifiLock != null) && (_wifiLock.isHeld())) {
        _wifiLock.release();
      }
      if ((_wakeLock != null) && (_wakeLock.isHeld())) {
        _wakeLock.release();
      }
    }
  }
}

该清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="Odroid.test"
  android:versionCode="1"
  android:versionName="1.0"
>
  <uses-sdk android:minSdkVersion="4" />

  <application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
  >
    <activity
      android:name=".Test"
      android:label="@string/app_name"
    >
      <intent-filter>
        <action
          android:name="android.intent.action.MAIN" />
        <category
          android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

  </application>
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.WAKE_LOCK" />

</manifest>

你知道吗?

推荐答案

有对Android的bug跟踪系统许多错误做了WiFi睡眠/省电模式,甚至试图纠正这种应用程序可用。因此,它很可能是你没有做错什么。

There are numerous bugs on the Android bug tracker to do with wifi sleep/power saving mode and even apps available that attempt to rectify this. So it is quite likely that you are not doing anything wrong.

<一个href="http://$c$c.google.com/p/android/issues/detail?id=9781">http://$c$c.google.com/p/android/issues/detail?id=9781
<一href="http://$c$c.google.com/p/android/issues/detail?id=1698">http://$c$c.google.com/p/android/issues/detail?id=1698

另外,请查阅wififixer这是一个开源项目,它可以帮助你code以保持连接活着

Also check out wififixer which is an open source project which may help you with code to keep the connection alive

HTTP://wififixer.word$p$pss.com/

这篇关于无线上网睡觉,即使有锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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