我正在尝试检查WIFI_SLEEP_POLICY
是否设置为WIFI_SLEEP_POLICY_NEVER
以外的任何内容。它在某些设备和模拟器中运行良好,但在其他设备和模拟器上不起作用。例如,它适用于Nexus S
仿真器API 10
,但Nexus One
仿真器不能API 8
。当它不起作用时,我收到此消息
12-10 15:57:19.625: W/System.err(477): android.provider.Settings$SettingNotFoundException: wifi_sleep_policy
12-10 15:57:19.625: W/System.err(477): at android.provider.Settings$System.getInt(Settings.java:807)
这是代码
WifiManager wm = (WifiManager) m_activity.getSystemService(Context.WIFI_SERVICE);
ContentResolver cr = m_activity.getContentResolver();
int policyNever = android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER;
String WIFI_SLEEP_POLICY = android.provider.Settings.System.WIFI_SLEEP_POLICY;
try {
if(android.provider.Settings.System.getInt(cr, WIFI_SLEEP_POLICY) != policyNever)
{
if (Build.VERSION.SDK_INT>=17){
showWifiSleepWarning(restartAction);
return false;
}
else
android.provider.Settings.System.putInt(cr, WIFI_SLEEP_POLICY, policyNever);
}
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:0)
有趣的是,当getInt返回错误时,putInt工作正常。因此,如果我尝试更改它而不先检查其状态,它就可以工作。当然,对于API 17及更高版本,它正好相反(如文档所述),因此新代码处理这两种情况。
WifiManager wm = (WifiManager) m_activity.getSystemService(Context.WIFI_SERVICE);
if(!wm.isWifiEnabled())
{
return true;
}
ContentResolver cr = m_activity.getContentResolver();
int policyNever = android.provider.Settings.System.WIFI_SLEEP_POLICY_NEVER;
try
{
android.provider.Settings.System.putInt(cr, WIFI_SLEEP_POLICY, policyNever);
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(android.provider.Settings.System.getInt(cr, WIFI_SLEEP_POLICY) != policyNever)
{
//if (Build.VERSION.SDK_INT>=17)
showWifiSleepWarning(restartAction);
return false;
}
}
catch (Exception e)
{
e.printStackTrace();
}