前台服务示例

编程入门 行业动态 更新时间:2024-10-17 17:17:50
本文介绍了前台服务示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新来的机器人。任何人都可以发布的通知与Android的前台服务示例链接或code。我用Google搜索,但没有得到前台服务的任何实例。

I am new to android. Can anyone post the link or code for "android foreground service example with notification". I googled , but didn't get any example of foreground service.

推荐答案

创建一个通知,可能使用 Notification.Builder 或 NotificationCompat.Builder ,并传递给 startForeground()上的服务:

Create a Notification, perhaps using Notification.Builder or NotificationCompat.Builder, and pass that to startForeground() on the service:

public class Downloader extends IntentService { private static int FOREGROUND_ID=1338; public Downloader() { super("Downloader"); } @Override public void onHandleIntent(Intent i) { startForeground(FOREGROUND_ID, buildForegroundNotification(filename)); // do useful work here stopForeground(true); } private Notification buildForegroundNotification(String filename) { NotificationCompat.Builder b=new NotificationCompat.Builder(this); b.setOngoing(true); b.setContentTitle(getString(R.string.downloading)) .setContentText(filename) .setSmallIcon(android.R.drawable.stat_sys_download) .setTicker(getString(R.string.downloading)); return(b.build()); } }

(从​​此示例项目一个精简的服务)

更多推荐

前台服务示例

本文发布于:2023-11-27 07:54:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   前台

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!