<分区>
我是安卓新手。任何人都可以发布“带通知的 android 前台服务示例”的链接或代码。我用谷歌搜索,但没有找到任何前台服务示例。
<分区>
我是安卓新手。任何人都可以发布“带通知的 android 前台服务示例”的链接或代码。我用谷歌搜索,但没有找到任何前台服务示例。
最佳答案
创建一个 Notification
,可能使用 Notification.Builder
或 NotificationCompat.Builder
,并将其传递给 startForeground()服务上的
:
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());
}
}
(来自 this sample project 的精简服务)
关于android - 前台服务示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21915287/