【问题标题】:Notification closed when Service finished服务完成后通知关闭
【发布时间】:2015-11-03 14:05:11
【问题描述】:

我正在创建一个通知并在执行意图服务期间对其进行更新。服务完成后出现问题,通知也被取消。

intentNotification = new Intent(this, ScanProcessActivity.class);
    intentNotification.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mBuilder = new NotificationCompat.Builder(getApplicationContext());

    mBuilder.setSmallIcon(R.mipmap.ic_app)
            .setAutoCancel(true)
            .setOngoing(true)
            .setContentTitle("Title")
            .setContentText("text")
            .setWhen(System.currentTimeMillis());

Intent Service 完成后,我可以做些什么来保留通知?

提前致谢。

【问题讨论】:

  • 你使用 NotificationManager 来显示通知吗?
  • 是的,我正在使用通知管理器来显示通知。
  • setAutoCancel(boolean autoCancel) 在 API 级别 11 中添加 使该通知在用户触摸时自动关闭。

标签: android service android-notifications android-intentservice


【解决方案1】:

我终于找到了解决方案。

主要问题是我使用的是:

startForeground(NOTIFICATION_ID, mBuilder.build());

这会导致意外行为,例如线程完成时取消通知。

解决方案是将 startForeground() 调用更改为:

mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多