【问题标题】:Resume (Foreground Started) Activity without updating ImageIcons恢复(前台启动)活动而不更新 ImageIcons
【发布时间】:2015-11-25 05:02:44
【问题描述】:

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

这是一段代码:

    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. 活动进入前台。它还在播放。
  4. 您点击通知。活动回来(onCreate,onStart,onResume)。然后它将暂停图像更改为播放图像。

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

我希望有人可以帮助我。

【问题讨论】:

  • 您能否将android:launchMode="singleTask" 添加到AndroidManifest.xml 文件中的主要活动中?
  • 是的。它停止了变化。但它不接受暂停命令。
  • @mdtuyen 成功了。我发现了一个糟糕的测试。但是您的解决方案再次解决了创建活动的问题。谢谢你。请添加它作为答案,然后我可以标记它。再次感谢。

标签: android android-intent android-activity android-service android-notifications


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多