Alexander Farber
Alexander Farber

Reputation: 22978

How to use ServiceCompat?

I am trying to create a local private Service, which would constantly run in background, scan for bluetooth (using BLE Wrapper) and post notifications - when a device with certain address (passed from Activity to the Service as string through SharedPreferences) is around.

(Also I'd like my service to start up automatically at boot, so I have added

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

to AndroidManifest.xml).

So here is my class:

import android.support.v4.app.ServiceCompat;

public class ScanService extends ServiceCompat {
    public ScanService() {
        super("ScanService");
    }
}

It does not compile with the following error:

The constructor ServiceCompat(String) is undefined

Then I try:

import android.support.v4.app.ServiceCompat;

public class ScanService extends ServiceCompat {
    public ScanService() {
        super();
    }
}

It does not compile with the following error:

The constructor ServiceCompat() is not visible

Finally I try:

import android.support.v4.app.ServiceCompat;

public class ScanService extends ServiceCompat {
    public ScanService() {
    }
}

It does not compile with the following error:

Implicit super constructor ServiceCompat() is not visible. Must explicitly invoke another constructor

So how should I please extend ServiceCompat properly?

Upvotes: 0

Views: 1198

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

You don't extend ServiceCompat. You extend Service.

If you look at the JavaDocs for ServiceCompat, you will notice that it has only one thing: a START_STICKY constant as a public static int. That's it. Since it is public static, you do not need a subclass to reference that constant. And, since that constant is defined on Service since API Level 5, you probably do not need ServiceCompat at all.

Upvotes: 2

Related Questions