Die Dienste onCreate und onStartCommand werden nicht ausgeführtJava

Java-Forum
Anonymous
 Die Dienste onCreate und onStartCommand werden nicht ausgeführt

Post by Anonymous »

Ich erstelle einen Android-Dienst für die Audiowiedergabe (es ist eine Flatter-App, die nativen Code für die Wiedergabe verwendet), aber beim Starten des Dienstes scheint er onCreate() und onStartCommand() nicht auszuführen.
Ich habe es getestet, indem ich einige print- oder log-Anweisungen in diese Funktionen eingefügt habe, aber sie werden nie ausgeführt. Ich habe auch darauf geachtet, den Dienst in AndroidManifest.xml hinzuzufügen
So starte ich den Dienst:

Code: Select all

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 ist eine Klasse, die Activity erweitert
Hier ist die Serviceklasse:

Code: Select all

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();
}
[...]
}
Und die Dienstdeklaration in AndroidManifest: Ich verstehe nicht, was ich hier falsch mache.
Erwähnenswert ist, dass der Name des installierten Pakets net.tailosive.app lautet, der in Java-Dateien, Verzeichnissen und Manifesten enthaltene Paketname jedoch com.example.tailosive ist. Könnte das ein Problem sein?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post