在哪里创建音乐播放器服务wrt MediaBrowserServiceCompat

rei*_*ley 5 java android mediabrowserservicecompat

我想创建一个利用MediaBrowserService功能的MusicPlayer应用程序.在通过MediaBrowserServiceCompat的文档时,我意识到它是Service的子类,这意味着它在Application的主UI线程上运行.

但是,由于音乐播放器是一项长期运行的任务,我认为最好将其作为一个IntentService而不是作为服务来实现.

所以我想问:

  1. 我应该在哪里实现我的MusicPlayer服务?

  2. 我应该在MediaBrowserServiceCompat实施中实施吗?但它不会在UI线程上过重吗?

  3. 或者我应该将其实现为IntentService&从MediaBrowserServiceCompat调用它?但它似乎有点复杂.

这是我的初始代码结构

请建议.

谢谢

jam*_*mes 0

错误的 :

我意识到它是 Service 的子类,这意味着它在应用程序的主 UI 线程上运行。

医生明确地说:

服务是一个应用程序组件,可以在后台执行长时间运行的操作

它还说,如果您需要某种长时间任务,您应该实现为Foreground Service

Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent =
        PendingIntent.getActivity(this, 0, notificationIntent, 0);

Notification notification =
          new Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
    .setContentTitle(getText(R.string.notification_title))
    .setContentText(getText(R.string.notification_message))
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pendingIntent)
    .setTicker(getText(R.string.ticker_text))
    .build();

startForeground(ONGOING_NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)