Android service stops working after application is not used for a while

I have a service for my application that works fine, but when the user does not use the phone there for 20 minutes, the service just stops working. is there something that I intend to do, like maintaining state or something else, I got lost at this stage. I don’t understand why the service continues to work, I look in the application> running services on my phone, and she is still talking about its launch. Any suggestions?

+4
source share
4 answers

I ran into the same problem several times ago. Then I started using the Android Alarm Service. This solved my problem. Here is the link http://android-er.blogspot.com/2010/10/simple-example-of-alarm-service-using.html

+5
source

If you want to keep running in the background (e.g. loading something), you need to capture wakelock (http://developer.android.com/reference/android/os/PowerManager.html)

Also note the following if you are downloading in the background: http://developer.android.com/reference/android/net/ConnectivityManager.html#getBackgroundDataSetting ()

+4
source

Instead, you can use the Broadcast receiver to use as a service. And it can be called the same as the Service with the alarm manager. Recipient:

public class CallListReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Call List receiver called", Toast.LENGTH_LONG) .show(); } } 

You can continuously call using:

 public void startAlert(Context context) { Intent call_intent = new Intent(this, CallListReceiver.class); PendingIntent pendingCallIntent = PendingIntent.getBroadcast(context, 0, call_intent, 0); AlarmManager call_alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); call_alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (7 * 1000), pendingCallIntent); } 
+4
source

This is the behavior of Android, which A Service has a maximum service life of 20 minutes. After 20 minutes, it will be automatically killed by the Android OS. But I think there is a solution. I have not tested it yet. Solution: In your onStartCommand (), put this code at the end of this method return START_STICKY;

+3
source

Source: https://habr.com/ru/post/1340477/


All Articles