前台服务示例 [英] Foreground service example
本文介绍了前台服务示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的机器人。任何人都可以发布的通知与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());
}
}
(从此示例项目一个精简的服务)
这篇关于前台服务示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文