java获取前台按钮状态_前台服务通知按钮奇怪地工作重新启动活动(绑定到服务)...

我有 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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值