I have my phone to do some work with network, so I need to keep turn on wi-fi always and my phone shouldn't sleep. But sometimes wi-fi connection lost and I need to reconnect manuale, so I decided to use AlarmManager to reconnect each hour to my own wi-fi network. But after 5-7 days my phone lost wi-fi. It's always "off" and I can't turn it on. I need to do software reset! Why it can happen? I have noticed that this problem appeared after I add some code to reconnect each hour to wi-fi network. So here it is. I have some permissions in my manifest.xml
我有我的手机与网络做一些工作,所以我需要一直打开Wi-Fi,我的手机不应该睡觉。但有时wi-fi连接丢失,我需要重新连接manuale,所以我决定使用AlarmManager将每小时重新连接到我自己的Wi-Fi网络。但是5-7天后我的手机丢失了Wi-Fi。它总是“关闭”,我不能打开它。我需要做软件重置!为什么会这样?我注意到在添加一些代码以将每小时重新连接到wi-fi网络后出现此问题。所以在这里。我在manifest.xml中有一些权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Also I have AlarmManager to plan my reconnection
我还有AlarmManager来计划我的重新连接
alarmIntent = new Intent(MainActivity.this, TimeReboot.class);
alarmIntent.putExtra("type", 3);
alarmIntent.setAction("actionReconnect");
PendingIntent rpendingIntent = PendingIntent.getBroadcast(MainActivity.this, 3, alarmIntent, 0);
Calendar rcalendar = Calendar.getInstance();
rcalendar.setTimeInMillis(System.currentTimeMillis());
rcalendar.set(Calendar.HOUR_OF_DAY, 6);
rcalendar.set(Calendar.MINUTE, 25);
manager.cancel(rpendingIntent);
manager.setRepeating(AlarmManager.RTC_WAKEUP, rcalendar.getTimeInMillis(),
1000 * 60 * 60, rpendingIntent);
And here some code for reconnect in onRecieve method of my BroadcastReceiver class
这里有一些代码用于重新连接我的BroadcastReceiver类的onRecieve方法
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", desiredNetworkSSID);
wifiConfig.preSharedKey = String.format("\"%s\"", modemwifipass);
WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
Also I use have this code in my onCreate method of main activity
我也在我的onCreate主要活动方法中使用了这段代码
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()) {
wifi.setWifiEnabled(true);
}
else
{
WifiInfo newInfo = wifi.getConnectionInfo();
}
// wi-fi always ON
lock = wifi.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "LockTag");
if (lock != null) {
lock.acquire();
}
// shouldn't sleep!
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
if (wl != null) {
wl.acquire();
}
I have no idea where can be my problem?? Please any ideas??
我不知道我的问题在哪里?请问有什么想法?
1 个解决方案
#1
0
I noticed that using WIFI_MODE_FULL_HIGH_PERF
you don't need to keep the screen on, but it will consume more power.
我注意到使用WIFI_MODE_FULL_HIGH_PERF您不需要保持屏幕开启,但它会消耗更多电量。
However, if you really need to keep the screen on, I also noticed that SCREEN_DIM_WAKE_LOCK
is a deprecated constant and they recommend using FLAG_KEEP_SCREEN_ON
(but this will keep the screen bright).
但是,如果你真的需要保持屏幕,我还注意到SCREEN_DIM_WAKE_LOCK是一个不推荐使用的常量,他们建议使用FLAG_KEEP_SCREEN_ON(但这会使屏幕保持亮)。
Besides this, try to check in the logs for your "LockTag"
when that forced disable happens (from the createWifiLock):
除此之外,尝试在强制禁用发生时(从createWifiLock)检查日志中的“LockTag”:
tag: a tag for the WifiLock to identify it in debugging messages. This string is never shown to the user under normal conditions, but should be descriptive enough to identify your application and the specific WifiLock within it, if it holds multiple WifiLocks.
tag:WifiLock的标记,用于在调试消息中标识它。在正常情况下,此字符串永远不会显示给用户,但如果它具有多个WifiLock,则应足以描述您的应用程序及其中的特定WifiLock。
The fact that the wi-fi is forcefully disabled makes me wonder if, by default, the system disables the wi-fi when it's locked for a prolonged use... The fact that you need to reset the software is disturbing. That may be a bug in Android.
wi-fi被强制禁用这一事实让我想知道,默认情况下,系统是否会在长时间使用时禁用wi-fi ......您需要重置软件这一事实令人不安。这可能是Android中的一个错误。
#1
0
I noticed that using WIFI_MODE_FULL_HIGH_PERF
you don't need to keep the screen on, but it will consume more power.
我注意到使用WIFI_MODE_FULL_HIGH_PERF您不需要保持屏幕开启,但它会消耗更多电量。
However, if you really need to keep the screen on, I also noticed that SCREEN_DIM_WAKE_LOCK
is a deprecated constant and they recommend using FLAG_KEEP_SCREEN_ON
(but this will keep the screen bright).
但是,如果你真的需要保持屏幕,我还注意到SCREEN_DIM_WAKE_LOCK是一个不推荐使用的常量,他们建议使用FLAG_KEEP_SCREEN_ON(但这会使屏幕保持亮)。
Besides this, try to check in the logs for your "LockTag"
when that forced disable happens (from the createWifiLock):
除此之外,尝试在强制禁用发生时(从createWifiLock)检查日志中的“LockTag”:
tag: a tag for the WifiLock to identify it in debugging messages. This string is never shown to the user under normal conditions, but should be descriptive enough to identify your application and the specific WifiLock within it, if it holds multiple WifiLocks.
tag:WifiLock的标记,用于在调试消息中标识它。在正常情况下,此字符串永远不会显示给用户,但如果它具有多个WifiLock,则应足以描述您的应用程序及其中的特定WifiLock。
The fact that the wi-fi is forcefully disabled makes me wonder if, by default, the system disables the wi-fi when it's locked for a prolonged use... The fact that you need to reset the software is disturbing. That may be a bug in Android.
wi-fi被强制禁用这一事实让我想知道,默认情况下,系统是否会在长时间使用时禁用wi-fi ......您需要重置软件这一事实令人不安。这可能是Android中的一个错误。