代码之家  ›  专栏  ›  技术社区  ›  matio

活动停止后IntentService停止

  •  0
  • matio  · 技术社区  · 8 年前

    activity 它约束了 connection 到a IntnetService .该服务只是起到了 MediaPlayer onDestroy 服务内部。然而,在按下main activity中的back之后,我原本希望该服务能够像IntentService假设的那样在后台以单独的线程运行,但在调试之后,我意识到了这一点 onDestory 活动来电 IntentService

    Activity

    public class MainActivity extends AppCompatActivity{
    
        private void startAudio() {
            Intent intent = new Intent(Intent.ACTION_SYNC, null, this, PlayerService.class);
            bound = bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    
        }
    }
    

    这是我的 Service :

    public class PlayerService extends IntentService{
        @Override
        public void onDestroy() {
            mediaPlayer.pause();
            mediaPlayer.reset();
            mediaPlayer.release();
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Shinoo Goyal    8 年前

    bindService用于绑定到服务,当服务没有剩余的客户端/连接时,服务会自动停止。在这里,随着活动被破坏,服务也会被破坏

        2
  •  1
  •   Abhishek Tyagi    8 年前

    由于您的服务没有任何被处理的意图,因此它不再存在。这里您需要的是一个粘性服务,即使它没有任何操作要执行,它也会继续。

    推荐文章