简体   繁体   English

应用程序在后台关闭

[英]App being shut down in background

Update: I've changed some things and better clarified my question here. 更新: 我已经更改了一些内容,并在这里更好地阐明了我的问题。

This question can be closed. 这个问题可以解决。


Quick note: I'm coding this in C# with Xamarin but any solution for regular Java and Android should apply so don't worry if your response isn't specific to C# or Xamarin. 快速说明:我使用Xamarin在C#中对此进行编码,但是适用于常规Java和Android的任何解决方案均应适用,因此如果您的响应不是特定于C#或Xamarin,请不要担心。

I'm having an issue where my android application is being shut down on me when the screen is off and it usually does so in less than a minute. 我遇到一个问题,当屏幕关闭时,我的android应用程序正在关闭,并且通常在不到一分钟的时间内就关闭了。 It keeps running when the application is open so perhaps it has something to do with the way android is handling my application when it's not in use. 当应用程序打开时,它将保持运行状态,因此它可能与android在不使用时处理我的应用程序的方式有关。 It doesn't happen when I'm debugging the application in an IDE though, since that's probably keeping the application alive, so I'm not getting any help from that. 但是,当我在IDE中调试应用程序时并不会发生这种情况,因为这可能会使应用程序保持活动状态,因此我没有得到任何帮助。

The application consists of a few activities and a service. 该应用程序包含一些活动和一项服务。 The service sends and receives data using a tcp connection. 该服务使用tcp连接发送和接收数据。 When the application is opened, it starts with the Login Activity. 打开应用程序后,它将以“登录活动”开始。 Then when you login, it finishes that activity and opens the Main Activity. 然后,当您登录时,它完成该活动并打开“主要活动”。 The Main Activity starts the Service in OnResume with StartService (new Intent (this, typeof(MainService))); Main Activity使用StartService (new Intent (this, typeof(MainService))); MainService StartService (new Intent (this, typeof(MainService)));在OnResume中启动服务StartService (new Intent (this, typeof(MainService))); .

The Service is a foreground service. 该服务是前台服务。

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;
} 

The part of the app that runs when it is being shut down are the Main Activity and the Service. 关闭应用程序时运行的部分是主要活动和服务。

When I try to reopen the application, it seems like it tries to reopen the Main Activity but then fails and closes. 当我尝试重新打开该应用程序时,似乎它尝试重新打开Main Activity,但随后失败并关闭。 Then when I try to reopen the application a second time it opens up the application back to the Login Activity. 然后,当我第二次尝试重新打开该应用程序时,它将打开该应用程序,使其回到“登录活动”。

The "service running" notification continues to remain up in the notification bar the whole time though despite it losing the tcp connection when the app is shut down. 尽管在关闭应用程序时失去了tcp连接,但“服务正在运行”通知始终始终保持在通知栏中。 This isn't good because the service needs to be working correctly for long periods of time. 这不是很好,因为该服务需要长时间正常工作。

Make sure you are starting you service via the startService and not just bindService. 确保您通过startService而不是bindService来启动服务。 If you need a bound service call startService as well so that it runs longer than just the thing bound to it. 如果还需要绑定服务,请调用startService,以使其运行时间比绑定到它的东西还要长。 You already mentioned that you are running it as a foreground service, this is good. 您已经提到过将其作为前台服务运行,这很好。 Finally, if none of that solves the issue for you, run the service in it's own process by adding a ":" in the manifest declaration for the service. 最后,如果这些都不为您解决问题,请在服务的清单声明中添加“:”,以自己的方式运行服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM