【问题标题】:Android - How to put an ongoing event on status bar but not on notification screen?Android - 如何在状态栏而不是通知屏幕上放置正在进行的事件?
【发布时间】:2016-07-22 10:26:42
【问题描述】:

在我的应用中,我添加了一个持续通知。通知在状态栏上有一个图标,在通知区域中有更多详细信息。

这张图片来自谷歌。这是通知区域:

我需要状态栏上的图标始终保持在那里,但用户将能够在通知屏幕中关闭通知,这样用户将只停留在状态栏上的图标上。 我该怎么做?

我的服务代码:

   @SuppressWarnings("static-access")
   @Override
   public int onStartCommand(Intent intent, int flag, int startId)
   {
        super.onStartCommand(intent, START_STICKY, startId);

        ...

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.myNotificationString))
            .setContentText(string1 + "  |  " + string2)
            .setLargeIcon(appIcon)
            .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName()));  // the icon changes every day

        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        Notification notification = mBuilder.build();
        notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(myRequestCode, notification);

        return START_STICKY;
    }

【问题讨论】:

  • 对不起,但对我来说,这个问题还不清楚
  • 我编辑了。简而言之:假设我有一个向用户显示电池状态的应用程序。用户希望在状态栏上看到电池状态,但又不想一直在通知区看到它作为通知。所以我希望用户能够在通知区域关闭通知,但状态栏上的图标不会消失。
  • 喜欢闹钟活动通知吗?您可以删除已设置警报的通知,但如果删除它,您将始终在状态栏中看到应用程序图标。我明白了吗?
  • 我想是的.. 如果我理解你

标签: android notifications


【解决方案1】:

尝试像下面这样定义resultPendingIntent

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_ONGOING_EVENT);

编辑 1

   @SuppressWarnings("static-access")
   @Override
   public int onStartCommand(Intent intent, int flag, int startId)
   {
        super.onStartCommand(intent, START_STICKY, startId);

        ...

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.myNotificationString))
            .setContentText(string1 + "  |  " + string2)
            .setLargeIcon(appIcon)
            .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName()));  // the icon changes every day

        Intent resultIntent = new Intent(this, MainActivity.class);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        Notification notification = mBuilder.build();
        notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;

        mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(myRequestCode, notification);

        return START_STICKY;
    }

【讨论】:

  • 但我有“ notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;” - 还不够吗?无论如何它不起作用,因为用户根本无法关闭通知,它很粘。
  • 好的,我明白了。然后还原该更改并进行此更改notification.flags = Notification.FLAG_ONGOING_EVENT;
  • 对不起,试试这个notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;,所以删除分配中的|
  • 对不起@TamarG,为了更清楚,我编辑了我的答案,请尝试编辑 1 处的代码
  • 我编辑了我的代码,但我仍然无法关闭通知。
猜你喜欢
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
相关资源
最近更新 更多