简体   繁体   中英

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.

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. 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. 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.

The application consists of a few activities and a service. The service sends and receives data using a tcp connection. 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))); .

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. 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. 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. If you need a bound service call startService as well so that it runs longer than just the thing bound to it. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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