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?