Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement App Application to display Notification in the background by Android

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve Android App application back to the background display notice, I hope you will learn something after reading this article, let's discuss it together!

Demand background

When I first received this demand, the first thing I thought of was that after the user retreated to the home page like the bank app, the notice bar needed to show "XXX is running in the background", and the notification could not be cleared, only the user re-entered the app and then disappeared. Then I came up with a solution to implement the front desk service (foregroundService), so I rolled up my sleeves and did it.

1. Create a ForegroundService inheritance Service

2. A series of methods such as rewriting onCreate

3. Create a notification and enable the service according to different versions

Start the service according to different versions

4. Monitor the life cycle of Application, display the foreground service in onActivityStopped, and cancel the foreground service in onActivityResumed.

Done, run the code to see the effect.

Oh, yeah.

Not at all, the problem encountered:

1. Not all onActivityStopped applications are switched to the background-where Baidu "how to monitor applications is switched to the background"

2. During onActivityResumed, if stopService operates quickly to the background and then to the front desk, it will receive a lot of crash messages.

Crash message

If you encounter a problem, let's solve the problem. Let's do it.

1. This problem is easy to solve. On Baidu, add a refCount variable, + + in the onActivityStarted method, and-- in the onActivityStopped method, and then judge in onActivityStopped that when refCount equals 0, the application goes back to the background.

2. The message of the crash of this problem means that the Service.startForeground () method is not called after the startForegroundService is called. The reason for this problem is that the foreground service has been stop before the start has been repeatedly entered and exited within a short period of time.

What are we going to do? The first thing that comes to mind is to delay a few seconds before stopService, and there is still a lot of crashes after writing the running results.

So: ask yourself from the bottom of your heart, why use the front desk service? Why use front desk service? Are there any other plans?

The answer must be yes, why do we have to use the front desk service? Can't we just use the notice? okay, just use the notice.

Therefore, a notification management class ForegroundPushManager is used to handle the display and closure of notifications.

This completes the function of retreating the application to the background to display notifications.

After reading this article, I believe you have a certain understanding of "how to implement App applications in Android to display notifications in the background". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 254

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report