如何创建持久性 Android 服务

How to create a persistent Android Service

Android documentationfor运行前台的一个Service中,提供了如下示例代码:

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);

但是,此代码不起作用。首先,constructor used for Notification is deprecated. Second, the method setLatestEventInfo(Context, String, String, PendingIntent) is no longer included in the Notificationclass。当我消除这些并以正确的方式创建通知时,会发生如下所示的错误:

Caused by: java.lang.NullPointerException: class name is null
  at android.content.ComponentName.<init>(ComponentName.java:114)
  at android.app.Service.startForeground(Service.java:654)
  at com.vcapra1.motionsensors.MainActivity.startService(MainActivity.java:53)

编辑: 这是我使用的代码,来自@提供的 link RusheelJain:

// prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(ctx, MotionMonitorService.class);
// use System.currentTimeMillis() to have a unique ID for the pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, (int) System.currentTimeMillis(), intent, 0);

// build notification
// the addAction re-use the same intent to keep the example short
Notification notification = new Notification.Builder(ctx)
  .setContentTitle("Monitoring Motion")
  .setContentText("No events yet...")
  .setSmallIcon(R.mipmap.ic_launcher)
  .setContentIntent(pendingIntent).build();

NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(NOTIFICATION_SERVICE);

// notificationManager.notify(1, notification);
startForeground(1, notification);

当我使用 notificationManager.notify(1, notification); 行时,会创建一个通知,但它不是持久的,也不会启动服务(当然,这不是故意的)。但是,当我使用 startForeground(1, notification); 行时,应用程序崩溃并显示上述堆栈跟踪。

所以我的最后一个问题是:即使应用程序关闭也会保持 运行 的服务的正确方法是什么? 我检查了几个来源它们都包含我在 Android 文档中找到的方法。

您必须使用 Notifiation.Builder 而不是通知的 class 构造函数。

这里是一个用动作启动前台服务的例子:

private Notification getForegroundNotification() {      
    Intent showTaskIntent = MyActivity.createStartIntent(this, mContext);

    PendingIntent contentIntent = PendingIntent.getActivity(
            getApplicationContext(),
            0,
            showTaskIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);


    Notification.Builder builder = new Notification.Builder(this);

    builder.setContentTitle(getString(R.string.title))
        //.setContentText(getString(R.string.notification_text))
        .setSmallIcon(R.drawable.notification_white)
        .setContentIntent(contentIntent);



    Intent shutdownActionIntent = new Intent(this, MyService.class);
    shutdownActionIntent.setAction(SHUTDOWN_NOTIFICATION_ACTION);

    PendingIntent shutdownPendingIntent = PendingIntent.getService(
            getApplicationContext(),
            1,
            shutdownActionIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    builder.addAction(android.R.drawable.ic_lock_power_off, getString(R.string.shutdown_action), shutdownPendingIntent);

    if(isRunningJellybeanOrLater()) {
        builder.setPriority(Notification.PRIORITY_MAX);
        return builder.build();
    } else {
        return builder.getNotification();
    }
} 

这是你开始的方式:

void handleStartAction() {      
        Notification notification = getForegroundNotification();
        startForeground(FOREGROUND_NOTIFICATION_ID, notification);
    }

使用来自服务生命周期方法(例如,onCreate()onStartCommand())的有效 Notification 或由这些生命周期方法之一调用的方法调用 startForeground() (例如,IntentServiceonHandleIntent())。

例如,此服务使用前台 Notification 而下载正在 onHandleIntent():

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.foredown;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.app.NotificationCompat;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Downloader extends IntentService {
  public static final String ACTION_COMPLETE=
      "com.commonsware.android.downloader.action.COMPLETE";
  private static int NOTIFY_ID=1337;
  private static int FOREGROUND_ID=1338;

  public Downloader() {
    super("Downloader");
  }

  @Override
  public void onHandleIntent(Intent i) {
    try {
      String filename=i.getData().getLastPathSegment();

      startForeground(FOREGROUND_ID,
                      buildForegroundNotification(filename));

      File root=
          Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

      root.mkdirs();

      File output=new File(root, filename);

      if (output.exists()) {
        output.delete();
      }

      URL url=new URL(i.getData().toString());
      HttpURLConnection c=(HttpURLConnection)url.openConnection();
      FileOutputStream fos=new FileOutputStream(output.getPath());
      BufferedOutputStream out=new BufferedOutputStream(fos);

      try {
        InputStream in=c.getInputStream();
        byte[] buffer=new byte[8192];
        int len=0;

        while ((len=in.read(buffer)) >= 0) {
          out.write(buffer, 0, len);
        }

        out.flush();
      }
      finally {
        fos.getFD().sync();
        out.close();
        c.disconnect();
      }

      stopForeground(true);
      raiseNotification(i, output, null);
    }
    catch (IOException e2) {
      stopForeground(true);
      raiseNotification(i, null, e2);
    }
  }

  private void raiseNotification(Intent inbound, File output,
                                 Exception e) {
    NotificationCompat.Builder b=new NotificationCompat.Builder(this);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
     .setWhen(System.currentTimeMillis());

    if (e == null) {
      b.setContentTitle(getString(R.string.download_complete))
       .setContentText(getString(R.string.fun))
       .setSmallIcon(android.R.drawable.stat_sys_download_done)
       .setTicker(getString(R.string.download_complete));

      Intent outbound=new Intent(Intent.ACTION_VIEW);

      outbound.setDataAndType(Uri.fromFile(output), inbound.getType());

      b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
    }
    else {
      b.setContentTitle(getString(R.string.exception))
       .setContentText(e.getMessage())
       .setSmallIcon(android.R.drawable.stat_notify_error)
       .setTicker(getString(R.string.exception));
    }

    NotificationManager mgr=
        (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());
  }

  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

您的堆栈跟踪表明您正在尝试从 MainActivity 调用 startForeground(),这不应该是可能的,而且肯定不是合适的模式。