按下主页按钮时前台服务被杀?

时间:2015-03-23 21:53:05

标签: android android-service android-5.0-lollipop

因此,这不是服务与活动生命周期相关联的典型问题。这个特殊的错误似乎与Android 5有关。我特别使用运行5.0.1的Nexus 5。

发生的事情是,经过一系列事件然后按回家导致Android杀死我的MediaService。

让我解释如何重现此错误以及我的测试应用程序中的一些代码。有两个课程涉及MainActivityMediaService

  1. 通过启动器图标打开测试应用。 MainActivity已启动,并且onStart()我们将绑定到MediaService,如下所示。
  2. private void doBindService ()
    {
      // no need to bind if already bound
      if (!mIsServiceBound)
      {
          Log.v(TAG, "doBindService()");
    
          Intent intent = new Intent(this, MediaService.class);
          //intent.setComponent(mServiceName);
    
          // start Service
          startService(intent);
    
          // connect to service
          if (14 > android.os.Build.VERSION.SDK_INT)
          {
              mIsServiceBound = bindService(intent, MainActivity.this,
                      Context.BIND_AUTO_CREATE);
          }
          else
          {
              // Context.BIND_IMPORTANT = 0x40
              mIsServiceBound = bindService(intent, MainActivity.this,
                      Context.BIND_AUTO_CREATE | 0x40);
          }
      }
    }
    
    1. 按主页按钮退出测试应用程序。
    2. 按方形按钮显示所有正在运行的任务,然后轻扫测试应用。 MediaService仍然按预期正常运行。
    3. 按下属于Test app foreground MediaService的通知,跳回Test应用程序。这是用于将我们带到Test应用程序的通知。
    4. public PendingIntent getPendingIntent (Context context)
      {
        Intent showPlayer = new Intent(context, MainActivity.class);
        showPlayer.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        return PendingIntent.getActivity(
                context, 0, showPlayer, PendingIntent.FLAG_CANCEL_CURRENT);
      }
      
      1. 按主页按钮关闭测试应用。在onStop(),我们调用unbindService()。紧接着,MediaService被杀了!这是我在日志中看到的内容。
      2.   

        I / ActivityManager(790):杀戮   12049:com.test.MediaService:RemoteMediaService / u0a91(adj 0):删除   任务

        该服务正在另一个进程上运行。

        <service android:name=".MediaService" android:process=":RemoteMediaService" />
        

0 个答案:

没有答案
相关问题