Always show service in notification bar

后端 未结 4 2065
名媛妹妹
名媛妹妹 2020-12-14 22:45

I want to add my app to the notification bar so that it always shows, like some apps in the Google Play store.

I want it to be like this screen shot:

4条回答
  •  借酒劲吻你
    2020-12-14 23:33

    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

提交回复
热议问题