简体   繁体   中英

Android - How to put an ongoing event on status bar but not on notification screen?

In my app I added an ongoing notification. The notification has an icon on status bar, and more details in the notification area.

This image is from Google. This is the notification area:

此图片来自Google。这是通知区域

I need that the icon on status bar will stay there always, but the user will be able to dismiss the notification in the notification screen so the user will stay only with the icon on status bar. How can I do it?

My service code:

   @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;
    }

try to define resultPendingIntent like below

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

EDIT 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM