Music service is killed even at startup

Is it possible to prevent service damage (startForground) from os? I use it to play sound, but it was killed when I use another application that requires more resources (Angry birds go) for a while. In the logs, I see that the application process is being destroyed by the ActivityManager. I reproduced this also with some other audio applications. Only the application that is not killed is the "Listen to Music" application. It has been replicated to samsung s3 android 4.3.

+5
source share
3 answers

Is it possible to prevent the Service (startForground) from killing the OS?

Not. This is Android that decides which process to stop when. The only way is to make it β€œless attractive to stop” by reducing the amount of memory it consumes.

Save memory. Put the service in your very small process. So the Android server can kill the main process for memory recovery, and your service can continue to work. Maintain a tiny service code by passing settings and settings with the intention of using the service.

Note: the tiny service must be a foreground service, that is, it should show an icon in the status bar.

process manifest

<service android:name="com.gosylvester.bestrides.ServiceLocationRecorder" android:process=":bestRidesService" > </service> </application> 

Go to settings with the intention to use the service. Just restart the service to change the settings.

 @Override public int onStartCommand(Intent intent, int flags, int startId) { boolean isTrackerMarker = SettingMarker.TRACKER_MARKER_DEFAULT; if (intent != null) { // intent is processing = b isMiles = intent.getBooleanExtra( SettingApplication.APPLICATION_MILE, SettingApplication.APPLICATION_MILE_DEFAULT); isRecordAccuracy = intent.getBooleanExtra( SettingRecord.RECORD_ACCURACY, SettingRecord.RECORD_ACCURACY_DEFAULT); locationInterval = intent.getLongExtra(SettingRecord.RECORD_MIlLIS, SettingRecord.RECORD_PRESET_MEDIUM_MILLIS); startMillis = intent.getLongExtra(BUNDLE_START_MILLIS, 0); distance = intent.getDoubleExtra( ServiceLocationRecorder.BUNDLE_TRACKED_DISTANCE, 0); recordDistance = (float) intent.getIntExtra( SettingRecord.RECORD_DISTANCE, SettingRecord.RECORD_PRESET_MEDIUM_DISTANCE); boolean newIsRecording = intent.getBooleanExtra( SettingRecord.RECORDING, isRecording); isTrackerMarker = intent.getBooleanExtra( SettingMarker.TRACKER_MARKER, SettingMarker.TRACKER_MARKER_DEFAULT); startRecording(newIsRecording); } 
+5
source

Have you tried to request focus and control the gain and lost it? I think this application management is far from our control, but at least you can do something when that happens.

At some point in your application loop, try something like this:

 audioManager.requestAudioFocus(new OnAudioFocusChangeListener() { @Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case AudioManager.AUDIOFOCUS_GAIN: // you have the focus, you can start or restarting playing break; case AudioManager.AUDIOFOCUS_LOSS: case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // you lost the focus, you should pause or stop playing break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // you lost the focus, but your app can continue playing in "duck" mode break; default: } } }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); 
+1
source

Take a look at the answer. Foreground service, which is being killed by Android Robin Davis, there is a lot of information:

Just to summarize, here's how it should work. The launch of the services will be regularly cleaned and terminated every 30 minutes or so. Services that want to stay alive longer than this should call Service.startForeground, which places a notification in the notification panel, so that users know that your service is constantly running and potentially suck battery life. Only 3 maintenance processes can designate themselves as foreground services at any given time. If there are more than three front-end services, the android will designate the oldest service as a candidate for cleanup and completion.

So, maybe there are more than 3 foreground services? He also explains possible platform errors when priority is given to foreground services.

0
source

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


All Articles