<p>说道服务不被杀死说白了分两种情况,一种是永远在执行,不执行ondestory,还有一种方法就是在销毁后重新执行oncreae方法,重新启动,相比而言,第一种基本上时候够呛了,现在的清理软件的优先级非常的高,所以,,,你懂得,你基本上无法创建一个永远不死的,但是可以重生啊!</p>
<p>下边上干货:</p>
<p><br> </p>
<p>1,通过service中的onstartCommand方法,他其中有一个参数是flag,他有四种标志,分别为:</p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">START_STICKY:重启服务,如果没有传递给他一个intent,那么intent为null,</span><br> </p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">START_NOT_STICKY:如果不进行传递intent,将不会启动,所以不会收到null的intent.</span><br> </span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">START_REDELIVER_INTENT:重启后将重新的intent传递过来。</span><br> </span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">START_STICKY_COMPATIBILITY是<span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">START_STICKY的兼容版本,不保证一定会重启,所以建议使用第一个标志,</span><br> </span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">在使用的时候将onstartCommand方法分局不同的需求返回不同的标志即可,首先第一个标志。</span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br> </span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">2,通过在service中在自己的ondestory方法中,启动oncreate方法即可,这样也是非常简单的</span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br> </span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">3,通过更改优先级的方式,在清单文件中,将优先级更改为1000,应该就差不多了,在清理内存的时候,优先级越高的越不容易被杀死。</span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br> </span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">4,通过发送广播,在服务中,当服务销毁的时候发送条广播,广播接收器接收到广播的时候,重新启动服务</span></span></span></span></p>
<p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line- |
|