Always show service in notification bar

此生再无相见时 提交于 2019-11-30 05:08:29

If you want your application to be present on the status bar at all times, you have to write a service and call startForeground(id, notification) in the onStart(...) and onStartCommand(...) methods and respectively call the stopForeground() method in the onDestroy() method of the service.

The id is an integer you can assign to the notification and notification is a Notification object (you can read about it more here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html).

This way as long as your service is running, the status bar notification will show.

Notification notification = new Notification(R.drawable.statusbar_icon,
        "Rolling text on statusbar", System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, YourActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

notification.setLatestEventInfo(this,
        "Notification title", "Notification description", contentIntent);

startForeground(1, notification);

You can put this code in the service's onStart(...) and onStartCommand(...) methods.

Also you can read more on services here: http://developer.android.com/reference/android/app/Service.html

In order to have your notification always present, you'll want to set these two flags:

notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

Note that while setting your Service to be in the foreground will also get you an ongoing event, that is a very inappropriate thing to do unless you truly do need your Service to run in the foreground. A music player is a good example of an app that should do that -- the user has an expectation that their music will play without interruption, even when doing many other things with the device.

Most Services, however, can afford to be temporarily stopped by the system when memory is low, and then restarted automatically when memory is available again. So the correct way to think about it is to separate the two ideas.

  1. If you want your notification to always be visible, use the two flags I mentioned.
  2. If you happen to also need your Service to run in the foreground, you can and should call Service.startForeground(), but don't think of this as a way to get an ongoing notification.

Here's example using NotificationCompact.Builder class which is the recent version to build notification.

private void startNotification() {

     //Sets an ID for the notification

  int mNotificationId = 001;

    // Build Notification , setOngoing keeps the notification always in status bar
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ldb)
                    .setContentTitle("Stop LDB")
                    .setContentText("Click to stop LDB")
                    .setOngoing(true);




    // Gets an instance of the NotificationManager service
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // Build the notification and issues it.
    mNotifyMgr.notify(mNotificationId, mBuilder.build());


}
Mani kandan

Just use below code to always show notification bar.

    Notification.Builder builder = new Notification.Builder(MainActivity.this);
    builder.setSmallIcon(R.mipmap.ic_launcher)
            .setContentText("Call Recorder")
            .setAutoCancel(false);
    Notification notification = builder.getNotification();

    notification.flags |= Notification.FLAG_NO_CLEAR
            | Notification.FLAG_ONGOING_EVENT;

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!