android - Service restarted when Application Killed -


i have read question didnt me...android background service restarting when application killed. answer mentioned here not helping me @ all. please consider this

i want create service background music , want play if app closed. write following code...

package elsner.com.mediaplayer;  import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.os.handler; import android.os.ibinder; import android.support.annotation.nullable; import android.util.log;  /**  * created nteam on 6/2/16.  */ public class background extends service {      mediaplayer mplayer;       @nullable     @override     public ibinder onbind(intent intent) {         return null;     }       @override     public void oncreate() {         super.oncreate();          log.i("niral", "service create");       }      @override     public int onstartcommand(intent intent, int flags, int startid) {         log.i("niral", "service start");         final handler h = new handler();         final long delayinmilliseconds = (6000*10);          new thread(new runnable() {              @override             public void run() {                 mplayer = mediaplayer.create(getapplicationcontext(), r.raw.aayat);                 mplayer.start();                  h.postdelayed(new runnable() {                     public void run() {                          mplayer.stop();                     }                 }, delayinmilliseconds);             }         }).start();            return start_not_sticky;     }   } 

all works fine until kill app background. kill app background service restarted , music file play top.

i kill application swiping away app in recent apps.

please me.

this re-post answer here: https://stackoverflow.com/a/39169603/6761960. i'll re-post convenience.

the app , service live on same process, means when app killed service. changing return value of onstartcommand doesn't affect process. tells service either start/stop when tell or when it's finished doing needs to.

to change service it's killed separately , assuming it's started service rather bound service due use of onstartcommand, specify process name in manifest service.

from process , threads developer guide:

the manifest entry each type of component element— <activity>, <service>, <receiver>, , <provider>— supports android:process attribute can specify process in component should run. can set attribute each component runs in own process or components share process while others not. can set android:process components of different applications run in same process—provided applications share same linux user id , signed same certificates.

android might decide shut down process @ point, when memory low , required other processes more serving user. application components running in process that's killed consequently destroyed. process started again components when there's again work them do.

from <service> in manifest file:

android:process

the name of process service run. normally, components of application run in default process created application. has same name application package. element's process attribute can set different default components. component can override default own process attribute, allowing spread application across multiple processes.

if name assigned attribute begins colon (':'), new process, private application, created when it's needed , service runs in process. if process name begins lowercase character, service run in global process of name, provided has permission so. allows components in different applications share process, reducing resource usage.

using method, service , app have 2 different process resulting in them being killed separate reasons. however, doesn't guarantee still won't killed. should design, expect, , willing have service killed.

also, due fact app music app , use of service user actively engaged in, creating service acceptable reason give foreground priority using startforeground. but, again, doesn't mean won't ever killed.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -