Java 错误:";Context.startForegroundService()然后没有调用Service.startForeground();

Java 错误:";Context.startForegroundService()然后没有调用Service.startForeground();,java,android,android-service,foreground-service,Java,Android,Android Service,Foreground Service,我在做一个android项目。我想在用户在应用程序中或终止应用程序时向用户发送通知。因此,我使用服务和广播接收器类 当我尝试发送通知时,它在两个方面都成功(应用程序正在运行或已终止)。但是,当我在应用程序死机后重新打开应用程序时,应用程序崩溃(Android 8.1及更高版本)。我得到这个错误: “Context.startForegroundService()没有调用 Service.startForeground() 我在AndroidManifest中添加了对前台服务的权限。 作为研究的结

我在做一个android项目。我想在用户在应用程序中或终止应用程序时向用户发送通知。因此,我使用服务和广播接收器类

当我尝试发送通知时,它在两个方面都成功(应用程序正在运行或已终止)。但是,当我在应用程序死机后重新打开应用程序时,应用程序崩溃(Android 8.1及更高版本)。我得到这个错误:

“Context.startForegroundService()没有调用 Service.startForeground()

我在AndroidManifest中添加了对前台服务的权限。 作为研究的结果,我尝试了很多方法,但都没有找到解决办法。我跟着,但不起作用。如果你能让我看看我能做什么,我将不胜感激。你可以在下面找到我的课程

我的基类:

Intent GoService;
private PushService mPushService;
Context context;
public Context getCtx() {
    return  context ;
}
…
context = this;
mPushService = new PushService(getCtx());
GoService = new Intent(getCtx(),mPushService.getClass());
if (!isServiceRunning (mPushService.getClass())) {
    startService(GoService);
}
…
private boolean isServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            Log.i ("isServiceRunning?", true+"");
            return true;
        }
    }
    Log.i ("isServiceRunning?", false+"");
    return false;
}

@Override
protected void onDestroy() {
    stopService(GoService);
    Log.i("MAINACT", "onDestroy!");
    super.onDestroy();
}
我的接受者类别:

public class PushServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        context.startForegroundService(new Intent(context, PushService.class));
    } else {
        context.startService(new Intent(context, PushService.class));
    }
}

最后,我解决了这个问题。谢谢你告诉我我能做什么。首先,我在我的服务类中添加startForeground和CustomStartForeground。我隐藏了这个通知。现在,应用程序每次都成功运行

我的服务级别:

   @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startCustomForeground();
        else
            startForeground(1, new Notification());
    }

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        stopForeground (true); //For hide notification
   ...

private void startCustomForeground(){
        String NOTIFICATION_CHANNEL_ID = "Try";
        String channelName = "BackgroundService";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }

您的
服务
s
startForeground
call在哪里?@Pawel,Receiver类拥有start方法。这是另一种方法,您必须从服务本身内部调用才能满足前台联系人的要求。但是,我想在不通知的情况下使用。我是如何用实现此方法的?您不知道,您的用例是将此限制放在首位的原因-以防止服务在用户不知情的情况下启动。请注意,停止前台后,系统将可以自由销毁您的服务。此外,一些AOSP android实现在后台服务运行时会显示警告通知。@Pawel感谢您的关注。我会考虑你的说法。Pawel有没有办法阻止AOSP显示警告信息?
   @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            startCustomForeground();
        else
            startForeground(1, new Notification());
    }

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        stopForeground (true); //For hide notification
   ...

private void startCustomForeground(){
        String NOTIFICATION_CHANNEL_ID = "Try";
        String channelName = "BackgroundService";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }