首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >前台服务收到待定意图后销毁

前台服务收到待定意图后销毁
EN

Stack Overflow用户
提问于 2019-07-08 05:13:15
回答 1查看 622关注 0票数 1

我正在尝试为android编写一个音乐播放器,并使用前台服务来运行音乐播放器。我从UI控件向服务发送一个pendingIntent来播放歌曲。收到intent后,会立即调用onStartCommand和onDestroy。我不知道如何停止onDestroy通话。

我尝试将挂起的意图更改为startService/ContextCompat.startForegroundService,但问题仍然存在。

服务:

代码语言:javascript
复制
import android.app.Service;
public class PlayerService extends Service{
private TransportControls transportControls;
@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (mediaSessionManager == null) {
      initMediaSession();
    }
    handleIntent(intent);

    return START_STICKY;
  }

private initMediaSession(){
     ...
     transportControls = mediaSession.getController().getTransportControls();

     mediaSession.setCallback(new MediaSessionCompat.Callback() {
      // Implement callbacks
      @Override
      public void onPlay() {
        play();
      }

     ...
  }

  handleIntent(Intent intent){
     // Initial checks
     if(/* action in intent is play*/) transportControls.play();
  }


  private void play(SongInfo songInfo){
     ...
      buildNotificaiton(//play action);
  }

  private void buildNotification(){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
        this, "default").setContentIntent(intent)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setStyle(new MediaStyle()
            .setMediaSession(mediaSession.getSessionToken())
            .setShowActionsInCompactView(0, 1, 2))
        .setContentText(StringUtils.parseArtists(songInfo.artists()))
        .setContentTitle(songInfo.displayName())
        .setContentInfo(songInfo.album())
        .setSound(null);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
        Context.NOTIFICATION_SERVICE);
    if (notificationManager == null) {
      return;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      NotificationChannel channel = new NotificationChannel("default",
          "Bhatki media notification",
          NotificationManager.IMPORTANCE_HIGH);
      channel.setDescription(
          "Notification displayed when music is being played. This notification is "
              + "required for the music to play.");
      notificationManager.createNotificationChannel(channel);
    }

    // Execution is reaching this line.
    startForeground(NOTIFICATION_ID, notificationBuilder.build());
  }
}

在活动中:

代码语言:javascript
复制
Intent intent = new Intent();
    intent.setClass(context, PlayerService.class);
    intent.setAction(action);
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

// one approach
    ContextCompat.startForegroundService(context, intent);

// Different approach
//    PendingIntent pendingIntent = PendingIntent
//        .getService(serviceContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//    try {
//      pendingIntent.send(serviceContext, 0, intent);
//    } catch (CanceledException e) {
//      Log.d(TAG, "sendIntent: failed");

我还在清单中添加了android前台服务权限:

代码语言:javascript
复制
 <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

我期望通知和服务能够持续存在。不知道为什么要调用OnDestroy。

EN

回答 1

Stack Overflow用户

发布于 2019-07-08 05:28:26

Once the service has been created, the service must call its startForeground() method within five seconds.

针对安卓9(API28级)或更高版本并使用前台服务的应用程序必须申请FOREGROUND_SERVICE权限

foreground service

编辑:我不能从你的代码中判断出问题所在,但根据我的经验,通知块应该在override fun onCreate()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56926158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档