Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android SDK 34 when calling method stopSelf() Crash #490

Open
summeryi opened this issue Nov 1, 2024 · 0 comments
Open

Android SDK 34 when calling method stopSelf() Crash #490

summeryi opened this issue Nov 1, 2024 · 0 comments

Comments

@summeryi
Copy link

summeryi commented Nov 1, 2024

Using a OnePlus phone, when installing and launching the app for the first time, there is no problem opening the service. However, when the service is triggered to close, the app crashes
Restart the app and repeat the above steps to avoid crashing

//=================================================================//
Flutter:3.22.3
compileSdkVersion 34
Device:OnePlus Ace 2V
Android:14

//=================================================================//
Log:
Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException
Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{716554a u0 com.sunnygrand.sleepbutler/id.flutter.flutter_background_service.BackgroundService}

Fatal Exception: android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{716554a u0 com.sunnygrand.sleepbutler/id.flutter.flutter_background_service.BackgroundService}
at android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2331)
at android.app.ActivityThread.throwRemoteServiceException(ActivityThread.java:2302)
at android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2628)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:255)
at android.os.Looper.loop(Looper.java:364)
at android.app.ActivityThread.main(ActivityThread.java:8979)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1053)

Caused by android.app.StackTrace
Last startServiceCommon() call for this service was made here

Caused by android.app.StackTrace: Last startServiceCommon() call for this service was made here
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1992)
at android.app.ContextImpl.startForegroundService(ContextImpl.java:1936)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:840)
at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java)
at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:700)
at id.flutter.flutter_background_service.FlutterBackgroundServicePlugin.start(FlutterBackgroundServicePlugin.java:83)
at id.flutter.flutter_background_service.FlutterBackgroundServicePlugin.onMethodCall(FlutterBackgroundServicePlugin.java:135)
at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267)
at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0(DartMessenger.java:319)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:255)
at android.os.Looper.loop(Looper.java:364)
at android.app.ActivityThread.main(ActivityThread.java:8979)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:572)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1053)

//=================================================================//
Code:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

 <service
        android:name="id.flutter.flutter_background_service.BackgroundService"
        android:foregroundServiceType="microphone|mediaPlayback" />

@pragma('vm:entry-point')
void onStart(ServiceInstance bgService) async {
Log.d('SleepTextRoute=> Service onStart set');
bgService.on(SleepTextRoute.ROUTE_STAOP).listen((event) async {
Log.d('SleepTextRoute=>Service STOP_BYHAND');
await bgService.stopSelf();
});
Log.d("SleepTextRoute=>Service finish set");
}

Future startService() async {
Log.d('SleepTextRoute=> begin set');
AndroidNotificationChannel channel = AndroidNotificationChannel(
'my_sleep_notification', // id
S.current.notice_sleep_service_title, // title
description: 'This channel is used for important notifications.', // description
importance: Importance.low, // importance must be at low or higher level
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
if (Platform.isIOS) {
await flutterLocalNotificationsPlugin.initialize(
const InitializationSettings(
iOS: DarwinInitializationSettings(),
android: AndroidInitializationSettings(R.loginLogo),
),
);
}
await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation()?.createNotificationChannel(channel);
await _bgService.configure(
androidConfiguration: AndroidConfiguration(
onStart: onStart,
// auto start service
autoStart: true,
isForegroundMode: true,
notificationChannelId: 'my_sleep_notification',
initialNotificationTitle: S.current.notice_sleep_service_title,
initialNotificationContent: S.current.notice_sleep_service_tip,
foregroundServiceNotificationId: 888,
foregroundServiceTypes: [AndroidForegroundType.microphone]
),
iosConfiguration: IosConfiguration(
autoStart: true,
onForeground: onStart,
onBackground: onIosBackground,
),
);
Log.d('SleepTextRoute=> start set');

_bgService.startService();
Log.d('SleepTextRoute=> finish set');

}

@summeryi summeryi changed the title Android 34 when calling method stopSelf() Crash Android SDK 34 when calling method stopSelf() Crash Nov 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant