Я тестировал ее, помещая в эти функции несколько операторов печати или журнала, но они никогда не запускаются. Я также обязательно добавил службу в AndroidManifest.xml
Вот как я запускаю службу:
Код: Выделить всё
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;
[...]
}
}
Вот класс обслуживания:
Код: Выделить всё
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();
}
[...]
}
Код: Выделить всё
Следует упомянуть, что имя установленного пакета — net.tailosive.app, но имя пакета, включенное в Java-файлы, каталоги и манифест, — com.example.tailosive. Может ли это быть проблемой?
Подробнее здесь: https://stackoverflow.com/questions/575 ... d-dont-run