在android中刷新网络堆栈的最佳方法是什么?
在我的应用程序内长时间不活动后,所有获取请求都开始失败,并出现连接超时错误。我注意到当手机通过 3G 网络连接时会发生这种情况,而不是在连接到 wifi 时发生。 目前,为了使其正常工作,我切换了飞行模式状态两次,并且网络连接被重置。这是执行此操作的最佳方法吗?请赐教!
void toggleAirplaneMode(){
//Toggling airplane mode to restart network
Context context = getApplicationContext();
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
// toggle airplane mode
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
context.sendBroadcast(intent);
Log.d("Inetify", "Toggling Airplane Mode");
}
After a long period of inactivity within my app, all the get requests start failing with a connection timeout error. I notice this was happening when the phone is connected over the 3G network, and not when it is connected to wifi..
At present to get it to work, i toggle the airplane mode state twice and the network connections get reset.Is this the best way to go about doing this ? Please enlighten!
void toggleAirplaneMode(){
//Toggling airplane mode to restart network
Context context = getApplicationContext();
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
// toggle airplane mode
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
context.sendBroadcast(intent);
Log.d("Inetify", "Toggling Airplane Mode");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论