Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

android - Xamarin start foreground service from background with activity recognition client

I have a ActivityRecognitionClient that is giving activity transition updates when user enters or exits a vehicle. That works fine. An Intent service is then starting a foreground service when vehicle entered is detected. However this works only when the application is in foreground and device is unlocked. The exact same code worked on xamarin 4.8 with android 9. Now that I have updated to xamarin 5 and android 11, it won't start the service when locked or application in background. Well, occasionally it does - which confuses me even more.

private void CreateActivityRequest()
        {
            manager = new ActivityRecognitionClient(context);
            var intent = new Intent(context, typeof(DetectedActivitiesIntentService));
            pendingIntent = PendingIntent.GetService(context, 0, intent, PendingIntentFlags.UpdateCurrent);
            var transitions = new List<ActivityTransition>
            {
                new ActivityTransition.Builder()
                      .SetActivityType(DetectedActivity.InVehicle)
                      .SetActivityTransition(ActivityTransition.ActivityTransitionEnter)
                      .Build(),
                new ActivityTransition.Builder()
                      .SetActivityType(DetectedActivity.InVehicle)
                      .SetActivityTransition(ActivityTransition.ActivityTransitionExit)
                      .Build()
            };
            request = new ActivityTransitionRequest(transitions);
        }

        public void StartManager()
        {
            manager.RequestActivityTransitionUpdates(request, pendingIntent);
        }

The IntentService:

protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                if (ActivityTransitionResult.HasResult(intent))
                {
                    var result = ActivityTransitionResult.ExtractResult(intent);
                    var transitions = result.TransitionEvents;
                    foreach (var t in transitions)
                    {
                        // wake lock
                        TurnDeviceLightOn(10);
                        // handle cached events
                        if (((SystemClock.ElapsedRealtime() - (t.ElapsedRealTimeNanos / 1000000)) / 1000) <= 60)
                        {
                            if (t.ActivityType == DetectedActivity.InVehicle)
                            {

                                if (t.TransitionType == 0)
                                {
                                    if (!DataStore.Driving && !DataStore.ServiceStarted)
                                    {
                                        DataStore.Driving = true;
                                        DataStore.Activity = "in vehicle";
                                        DataStore.StartTime = DateTime.Now;

                                        // start tracking service

                                        StartService();
                                    }
                                }
                                else
                                {
                                    if (DataStore.Driving)
                                    {
                                        DataStore.Driving = false;
                                        DataStore.Activity = "not in vehicle";
                                        DataStore.EndTime = DateTime.Now;

                                        CheckExit(5);
                                    }
                                }
                            }
                        }
                    }
                    MessagingCenter.Send(new object(), "drivingStatus", DataStore.Driving);
                    MessagingCenter.Send<object, string>(new object(), "new_activity_data", null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

However, when the device is locked the startService() is not called. I even added a wakelock to prevent this. Any ideas on how to fix or debug? Because debugging is not really possible with emulator and the real device gotta be in driving state..

question from:https://stackoverflow.com/questions/65842179/xamarin-start-foreground-service-from-background-with-activity-recognition-clien

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...