Always show service in notification bar

后端 未结 4 2062
名媛妹妹
名媛妹妹 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:46

    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());
    
    
    }
    

提交回复
热议问题