Android的下降TCP连接时,屏幕关闭 [英] Android dropping tcp connection when screen is off

查看:406
本文介绍了Android的下降TCP连接时,屏幕关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序启动并保持TCP连接前台服务中去。活动使用 StartService()来启动服务。此外该服务已启动在它自己的过程。

My Android app starts and keeps a TCP connection going within a Foreground service. The activity uses StartService() to start the service. Also the service is started in it's own process.

下面是服务 OnStartCommand

// Code is in C# using Xamarin but regular Java/Android answers are acceptable
public override StartCommandResult OnStartCommand (Intent intent, StartCommandFlags flags, int startId)
{
    base.OnStartCommand (intent, flags, startId);

    ...

    var ongoingNotification = new Notification (Resource.Drawable.icon, "Service running");
    var pendingIntent = PendingIntent.GetActivity (this, 0, new Intent (this, typeof(MainActivity)), 0);
    ongoingNotification.SetLatestEventInfo (this, "Service", "The service is running.", pendingIntent);
    StartForeground ((int)NotificationFlags.ForegroundService, ongoingNotification);
    return StartCommandResult.RedeliverIntent;
}

的连接是正常时,手机屏幕上,从我的应用程序的活动是否是开放与否。不过,我关闭屏幕小于30秒后,我总是输TCP连接。所以我得到的连接回到​​我的应用程序将自动重新连接,但是当屏幕处于关闭状态,不断地断开连接。我打开它,它的精致,即使从我的应用程序的活动不开。

The connection is fine when the phone's screen is on, whether or not an activity from my app is open or not. However less than 30 seconds after I turn the screen off I always lose tcp connection. My app reconnects automatically so I get the connection back, but it will continually disconnect when the screen is off. I turn it back on and it's fine, even if an activity from my app isn't open.

它可以连接到Android生命周期,但我只是不明白如何。根据调试信息写到手机(当从IDE调试器附着的问题不会发生)上的文本文件,该服务似乎它应该但连接只是是不是被稳定处于工作状态。

It could be connected to the Android Lifecycle but I just don't see how. Based on debug messages I write to a text file on the phone (the issue doesn't happen when a debugger from an IDE is attached), the service seems to be operating as it's supposed to but the connection just isn't being stable.

我也该有和没有的选择没有改变开发人员选项不保留活动进行测试。所以,它不应该有任何与活动的生命周期至少

I've also tested this with and without the "Don't Keep Activities" in Developer options selected to no change. So it shouldn't have anything to do with the Activity Lifecycle at least.

为什么Android的下降我的TCP连接,但只有当屏幕处于关闭状态?

Why is Android dropping my tcp connection, but only when the screen is off?

推荐答案

我通过收购部分的激活锁定我的服务运行时。

I solved the problem by acquiring a Partial WakeLock while my service is running.

private PowerManager.WakeLock mWakeLock;

public override void OnCreate ()
{
    PowerManager pm = (PowerManager) GetSystemService(Context.PowerService);
    mWakeLock = pm.NewWakeLock (WakeLockFlags.Partial, "PartialWakeLockTag");
    mWakeLock.Acquire();
}

public override void OnDestroy ()
{
    mWakeLock.Release();
}

如果我实现在未来的东西更省电,我会更新这个答案。

If I implement something more power efficient in the future, I'll update this answer.

这篇关于Android的下降TCP连接时,屏幕关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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