Оно использует плагины audio_service и just_audio для работы в фоновом режиме и для воспроизведения звука.
В настоящее время я пытаюсь добиться того, чтобы при перезагрузке телефона на устройствах Android приложение автоматически запускалось в фоновом режиме с помощью audio_service.
На данный момент я можно запустить приложение только при перезагрузке, но на переднем плане. Есть мысли?
Вот часть кода:
Код: Выделить всё
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
val i = Intent(context, com.ryanheise.audioservice.AudioServiceActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
context.startActivity(i)
);
Этот код взят из собственного кода audio_service Android.
Код: Выделить всё
public class AudioServiceActivity extends FlutterActivity {
@Override
public FlutterEngine provideFlutterEngine(@NonNull Context context) {
return AudioServicePlugin.getFlutterEngine(context);
}
}
Код: Выделить всё
public static synchronized FlutterEngine getFlutterEngine(Context context) {
FlutterEngine flutterEngine = FlutterEngineCache.getInstance().get(flutterEngineId);
if (flutterEngine == null) {
// XXX: The constructor triggers onAttachedToEngine so this variable doesn't help us.
// Maybe need a boolean flag to tell us we're currently loading the main flutter engine.
flutterEngine = new FlutterEngine(context.getApplicationContext());
String initialRoute = null;
if (context instanceof FlutterActivity) {
final FlutterActivity activity = (FlutterActivity)context;
initialRoute = activity.getInitialRoute();
if (initialRoute == null) {
if (activity.shouldHandleDeeplinking()) {
Uri data = activity.getIntent().getData();
if (data != null) {
initialRoute = data.getPath();
if (data.getQuery() != null && !data.getQuery().isEmpty()) {
initialRoute += "?" + data.getQuery();
}
}
}
}
}
if (initialRoute == null) {
initialRoute = "/";
}
Log.d("GASPI", "Initial route: " + initialRoute);
flutterEngine.getNavigationChannel().setInitialRoute(initialRoute);
flutterEngine.getDartExecutor().executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault());
FlutterEngineCache.getInstance().put(flutterEngineId, flutterEngine);
}
return flutterEngine;
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... ng-an-andr
Мобильная версия