2

I'm building an android service for audio playback (it's a flutter app using native code for playback), but when launching the service it doesn't seem to run onCreate() and `onStartCommand()'.

I've tested it with putting some print or log statements in those functions, but they never run. I've also made sure to add the service into the AndroidManifest.xml

Here is how I launch the service:

public class MainActivity extends FlutterActivity implements MethodCallHandler {
  public void onMethodCall(MethodCall call, Result result) {
    switch (call.method) {
      [...]
      case "startService":
        Intent serviceIntent = new Intent(getFlutterView().getContext(), AudioService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          this.startForegroundService(serviceIntent);
        } else {
          this.startService(serviceIntent);
        }
        break;
        [...]
    }
}

FlutterActivity is a class that extends Activity

Here is the service class:

public class AudioService extends Service {
  public MediaPlayer audioPlayer;

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

  @Override
  public void onCreate() {
    super.onCreate();

    Log.i("Audio", "onCreate()");
  }

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

    Log.i("Audio", "Starting service...");

    // create notification
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(
            this,
            0,
            notificationIntent,
            0
    );

    Notification audioNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Foreground service is running")
            .setContentText("This notification does nothing")
            .setSmallIcon(R.drawable.app_icon)
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1, audioNotification);

    audioPlayer = new MediaPlayer();

    Log.i("Audio", "Service started successfuly");
    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    super.onDestroy();

    // destroy the player
    stopAudio();
  }
  [...]
}

And the service declaration in AndroidManifest:

<service 
      android:name=".AudioService"
      android:process="net.tailosive.app.AudioService"
      android:enabled="true"
      android:exported="true"/>

I don't see what I'm doing wrong here. A thing worth mentioning is that the installed package name is net.tailosive.app, but the the package name included in java files, directories and manifest is com.example.tailosive. Could this be an issue?

Karol Wasowski
  • 465
  • 1
  • 6
  • 18
  • You could try placing the `startForeground` in the `onCreate` even before `super.onCreate`. At least this is what saved me once, when using a Foreground Service. – Maximilian Speicher Aug 16 '19 at 11:20

1 Answers1

0

Also what's the purpose of using START_STICKY while it's a Foreground Service and it's guaranteed to be running as long as the ongoing notification displays?

Mehran Rasa
  • 99
  • 1
  • 5
  • These things helped, but what seemed to be the issue was, that I didn't add the FOREGROUND_SERVICE permission to AndroidManifest.xml. Thanks for your suggestion! – Karol Wasowski Aug 17 '19 at 14:43