Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Android 8+ crash at boot_completed

Hi,

I am currently developing an Android application and I have a mysterious problem on Android 8 to 9. When I restart my phone, I receive an alert message to warn me of the failure of the application (mainly by the service started during Boot_Completed), but the services of the application are still functional. By my stack trace, I can say that the problem comes from the broadcast receiver that launches any other service.

Unfortunately, I was unable to find a fix to that issue.

Below, you can see my broadcast receiver.

[BroadcastReceiver( Name = "com.parroinfo.SERVICE_STARTUP", Enabled = true, Exported = true, DirectBootAware = true)]
    [IntentFilter(new[] {Intent.ActionBootCompleted, Intent.ActionLockedBootCompleted}, Priority = (int)IntentFilterPriority.HighPriority)]
    class ServiceStartup : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
            {
             if (AndroidUtils.isVersionBelow(BuildVersionCodes.O))
             {
                        context.StartService(new Intent(context, -type-));
                     }
                     else
                     {
                        context.StartForegroundService(new Intent(context, -type-));
                     }
        }
    }

Below, you can see my declaration on my manifest.

<receiver android:exported="true" android:name="com.parroinfo.SERVICE_STARTUP">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Below, you can see the stack trace of the problem.

java.lang.RuntimeException: 
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3388)
  at android.app.ActivityThread.access$1200 (ActivityThread.java:199)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1661)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:193)
  at android.app.ActivityThread.main (ActivityThread.java:6669)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:493)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: 
  at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1577)
  at android.app.ContextImpl.startService (ContextImpl.java:1532)
  at android.content.ContextWrapper.startService (ContextWrapper.java:664)
  at com.parroinfo.SERVICE_STARTUP.n_onReceive (Native Method)
  at com.parroinfo.SERVICE_STARTUP.onReceive (SERVICE_STARTUP.java:29)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:3379)

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @patriack
    I have tested your BroadcastReceiver. It works fine. I think this issue is caused by your service. Please check your service code. Or please share the code so that we could help you to solve it.

  • patriackpatriack Member ✭✭

    @BillyLiu

    hi, has service, I use the firebaseMessagingService.

        [Service]
        [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
        public class NotificationMessagingService : FirebaseMessagingService
        {
            public void OnMessageReceived(RemoteNotificationPush remoteMessage)
            {
                AndroidUtils.notify(AndroidUtils.createNotification(remoteMessage.title, remoteMessage.body,
                                                            Resource.Drawable.ic_app_notification, true, true, true));
    
                AndroidUtils.vibrate(100);
                AndroidUtils.wakeUpScreen();
            }
    
            public override void HandleIntent(Intent p0)
            {
                RemoteNotificationPush remoteMessage = RemoteNotificationPush.create(p0);
                //S'assure d'être bien le destinataire de l'intent
                if (remoteMessage.collapseKey == AndroidUtils.context.PackageName)
                    this.OnMessageReceived(remoteMessage);
            }
        }
    
Sign In or Register to comment.