我有 MusicPlayerActivity
(它的launchMode在Manifest中是SingleTop)和 MainActivity
,都绑定到 MusicService
. 一切似乎都很好,但后来我看到了一个错误:播放第一首歌没有问题 . 但是,当我播放第二首歌曲,并尝试使用通知的按钮来控制它时,表现就好像按了两次 . 因此,暂停将导致再次播放,接下来将导致跳过一首歌曲 .
What I tried: - 我认为这是由于使用了 startForeground()
和 NotificationManager
的组合,所以我删除了所有 NotificationManager
部分并开始使用 startForeground
,即使是更新通知(我希望这不是错误的做法) . 但它没有用 .
What I think the problem is: 我不确定,但我认为问题在于创建 MusicPlayerActivity
的新实例 . 对于第一首歌,它没关系,然后 onBackPressed()
, MainActivity
是 MovedToFront
. 然后,当用户从 MainActivity
中选择另一首歌曲时,将创建 MusicPlayerActivity
的新实例,并再次使用该服务 bound . 歌曲很好但通知开始很奇怪 .
This is how I create notification in playSong() method of 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);
}
This is how I bind MusicPlayerActivity to the Service:
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);
}