gpt4 book ai didi

android - 恢复(前台启动) Activity 而不更新 ImageIcons

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:44 24 4
gpt4 key购买 nike

我有一个音乐播放器服务,当它进入前台时我会向用户显示一条通知。但是当用户点击通知返回 App 时,它会再次更新图标(图标图像在 onCreate 中设置)。我知道它正在更新图标,因为它正在重新创建 Activity 。

这是一段代码:

    public void startForeground(String songName){

Intent showTaskIntent = new Intent(AudioPlayer.this, PlayerActivity.class);
showTaskIntent.setAction(Intent.ACTION_MAIN);
showTaskIntent.addCategory(Intent.CATEGORY_LAUNCHER);
showTaskIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent contentIntent = PendingIntent.getActivity(
this,
0,
showTaskIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle(getString(R.string.app_name))
.setContentText(songName)
.setSmallIcon(R.drawable.logo)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.build();

startForeground(NOTIFICATION_ID, notification);

我试过很多不同的标志,但它总是更新。堆栈中的其他相关案例和解决方案对我没有帮助。

--

主要问题 - 解释:

  1. 例如,当您打开音乐播放器时,您会看到播放按钮。
  2. 你点击播放,它就会播放。播放图像更改为暂停图像。
  3. Activity 进入前台。它还在播放。
  4. 您点击通知。 Activity 返回(onCreate、onStart、onResume)。然后它将暂停图像更改为播放图像。

    • 如果我点击应用程序图标(系统图标不是通知),它会调用 onRestart。我想这就是我需要的。

我希望有人能帮助我。

最佳答案

android:launchMode="singleTask" 添加到 AndroidManifest.xml 中的主要 Activity

关于android - 恢复(前台启动) Activity 而不更新 ImageIcons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33908788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com