java.lang.NoSuchMethodError:没有虚拟方法startForeground

时间:2019-10-28 20:56:54

标签: android android-studio android-fragments android-service android-notifications

我正在尝试取消前台服务,但收到NoSuchMethodError。它说没有找到startForeground,但看起来像它存在。我确实在清单文件中拥有该服务和android.permission.FOREGROUND_SERVICE权限。

public void startForegroundService(Intent intent) {
    Log.d(TAG, "startForegroundService: " + intent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        getContext().startForegroundService(intent);
    } else {
        getContext().startService(intent);
    }
}

并收到此错误:

java.lang.NoSuchMethodError: No virtual method startForeground(ILandroid/app/Notification;I)V in class Lcom/volnoor/backup/core/sync/BaseForegroundService; or its super classes (declaration of 'com.volnoor.backup.core.sync.BaseForegroundService' appears in /data/app/com.volnoor.backup-aFOxNxXCGe_PmUbYxrrdHw==/base.apk!classes2.dex)
    at com.volnoor.backup.core.sync.BaseForegroundService.onStartCommand(BaseForegroundService.java:27)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3567)
    at android.app.ActivityThread.-wrap20(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1718)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6687)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

DriveService:

public class DriveService extends BaseForegroundService {

    @Override
    protected int getId() {
        return 1;
    }

    @Override
    protected Notification getNotification() {
        return new NotificationCompat.Builder(this, BackupApplication.CHANNEL_FOREGROUND_SYNC)
                .setContentTitle("TODO Title")
                .setContentText("TODO Text")
                .setSmallIcon(R.drawable.ic_google_drive)
                // TODO .setContentIntent(pendingIntent)
                .build();
    }

    @Override
    protected int getServiceType() {
        return FOREGROUND_SERVICE_TYPE_DATA_SYNC;
    }
}

BaseForegroundService:

public abstract class BaseForegroundService extends Service {

    private static final String TAG = "BaseForegroundService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");

        startForeground(getId(), getNotification(), getServiceType());

        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    protected void stopForegroundService() {
        Log.d(TAG, "stopForegroundService");

        stopForeground(true);
        stopSelf();
    }

    protected abstract int getId();

    protected abstract Notification getNotification();

    protected int getServiceType() {
        return FOREGROUND_SERVICE_TYPE_MANIFEST;
    }
}

2 个答案:

答案 0 :(得分:2)

您选择的startForeground()重载仅添加到API 29中。您的设备可能来自较早的API级别。

解决方案:如果Build.VERSION.SDK_INT小于Build.VERSION_CODES.Q,请使用startForeground(int, Notification)

我不知道这是怎么过去的。

答案 1 :(得分:-1)

问题出在startForeground(getId(), getNotification(), getServiceType());方法上(3个参数)。将其更改为startForeground(getId(), getNotification())(2个参数)后,一切工作正常;