0

我有一个MusicPlayerActivity(它的 launchMode 是 Manifest 中的 SingleTop)和MainActivity,都绑定到一个MusicService. 一切似乎都很好,但后来我看到了一个错误:播放第一首歌曲没有问题。但是当我播放第二首歌曲并尝试使用通知按钮来控制它时,就像按下了两次一样。因此,暂停会导致再次播放,下一次会导致跳过一首歌曲。

我尝试了什么: -我认为这是由于使用了 and 的组合startForeground()NotificationManager所以我删除了所有NotificationManager部分并开始使用startForeground,即使是为了更新通知(我希望这不是错误的做法)。但它没有用。

我认为问题在于: 我不确定,但我认为问题在于创建MusicPlayerActivity. 对于第一首歌,它是好的和绑定的,那么onBackPressed()MainActivityMovedToFront。然后当用户从中选择另一首歌曲时,会创建MainActivity一个新的实例并再次与服务绑定。歌曲播放良好,但通知开始变得奇怪。MusicPlayerActivity

这就是我在以下playSong()方法中创建通知的方式Service

void playSong(){
...

            setNotification(songTitle);
}
public void setNotification(String songName) {
        notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);
        notificationView.setTextViewText(R.id.textView1, songName);
        setNotificationArt(notificationView);
    notification.contentView = notificationView;
        Intent notificationIntent = new Intent(getApplicationContext(), MusicPlayerActivity.class);
        notificationIntent.setAction("dummy");
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingNotificationIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
        notificationView.setOnClickPendingIntent(R.id.textView1, pendingNotificationIntent);
        notificationView.setOnClickPendingIntent(R.id.imgAppIc, pendingNotificationIntent);
        notification.contentIntent = pendingNotificationIntent;
        notification.flags |= Notification.FLAG_NO_CLEAR;
        notification.flags |= Notification.PRIORITY_HIGH;
        notification.flags |= Notification.FLAG_ONGOING_EVENT;//Dont know why using this flag.
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        Intent switchIntent = new Intent("com.dmmet.musicplayer.ACTION_PLAY");
        PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        notificationView.setOnClickPendingIntent(R.id.btnPlay2, pendingSwitchIntent);
        Intent closeIntent = new Intent("com.dmmet.musicplayer.ACTION_CLOSE");
        PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this, 0, closeIntent, 0);

        notificationView.setOnClickPendingIntent(R.id.btnClose, pendingCloseIntent);
        Intent nextIntent = new Intent("com.dmmet.musicplayer.ACTION_NEXT");
        PendingIntent pendingNextIntent = PendingIntent.getBroadcast(this, 0, nextIntent, 0);

        notificationView.setOnClickPendingIntent(R.id.btnNext, pendingNextIntent);
        Intent previousIntent = new Intent("com.dmmet.musicplayer.ACTION_PREVIOUS");
        PendingIntent pendingPreviousIntent = PendingIntent.getBroadcast(this, 0, previousIntent, 0);

        notificationView.setOnClickPendingIntent(R.id.btnPrevious, pendingPreviousIntent);

        startForeground(NOTIFICATION_ID, notification);
    }

这就是我绑定MusicPlayerActivity到服务的方式:

 public ServiceConnection musicConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder)service;

            //get service
            musicService = binder.getService();
            Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
            //pass list
            //musicService.setList(Q1);
            musicBound = true;
            if(musicBound){
                //Toast.makeText(getApplicationContext(), "Ready to play", Toast.LENGTH_LONG).show();
                musicService.queue(Q1,start);
                //musicService.playSongContinuation(Q1.get(start));
            }
            Log.d("ServiceConnection","connected");

        }

        public void onServiceDisconnected(ComponentName className) {
            Log.d("ServiceConnection","disconnected");
            musicBound = false;
        }

    };



    @Override
    public void onStart() {
        super.onStart();
        Intent intent = new Intent(this, MusicService.class);
        bindService(intent, musicConnection, Context.BIND_AUTO_CREATE);
    }
4

0 回答 0