android - 前台服务示例

标签 android android-layout android-intent android-fragments

<分区>

我是安卓新手。任何人都可以发布“带通知的 android 前台服务示例”的链接或代码。我用谷歌搜索,但没有找到任何前台服务示例。

最佳答案

创建一个 Notification,可能使用 Notification.BuilderNotificationCompat.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/

相关文章:

java - 通过 Intent 拍摄多张照片

java - 如何在java中使用GET查询发送HashMap

android - 解释安装新的 Android apk 和升级现有的 Android apk 之间的区别? apk 升级如何工作?

android - 在Android中单击线性布局时将选择单选按钮

android - 如何为包含的布局设置权重?

android - RecyclerView 内的 ScrollView 不会滚动

android - 以编程方式使墙纸适合每个屏幕而不滚动

android - IntentService 和 HandlerThread 有什么区别?

c# - 无法在 SlidingUpPanelLayout Xamarin Android 中使用 ScrollView 布局

android - 如何使用 intent startactivityforresult 方法?