Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

xamarin Android - google activity recognition transition api not working

$
0
0

I wish to use the new Google Activity transition API and after following the tutorial here I am not able to get the desired result. If you wanto to use Transition API you have to update Xamarin.GooglePlayServices.Location nuget to prerelease(70.1501.0).

The method OnReceive in BroadcastReceiver is never called!!. OnSuccess is always called.

This is the code I have for setting the activity transition I wish to detect.

//manifest

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />

//service

[Service]
    public class Service : IntentService
    {
        public IBinder Binder { get; private set; }
        private PendingIntent pendingIntent;

        public override void OnCreate()
        {
            base.OnCreate();
            List<ActivityTransition> transitions = InitActivityTransitions();
            ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);

            var intent = new Intent(this, typeof(SampleReceiver));
            pendingIntent = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.UpdateCurrent);

            var task = ActivityRecognition.GetClient(this).RequestActivityTransitionUpdates(request, pendingIntent);
            task.AddOnSuccessListener(new OnSuccessListener());
            task.AddOnFailureListener(new OnFailureListener());
        }

        [BroadcastReceiver(Enabled = true, Exported = false)]
        public class SampleReceiver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {           
                Toast.MakeText(context, "OnReceive called! hurray", ToastLength.Long).Show();
            }
        }

        private List<ActivityTransition> InitActivityTransitions()
        {
            List<ActivityTransition> transitions = new List<ActivityTransition>();
            transitions.Add(
                 new ActivityTransition.Builder()
                 .SetActivityType(DetectedActivity.Still)
                 .SetActivityTransition(ActivityTransition.ActivityTransitionEnter)
                 .Build());

            transitions.Add(
                new ActivityTransition.Builder()
            .SetActivityType(DetectedActivity.Still)
            .SetActivityTransition(ActivityTransition.ActivityTransitionExit)
            .Build());
            return transitions;
        }

        public override void OnDestroy()
        {
            ActivityRecognition.GetClient(this).RemoveActivityTransitionUpdates(pendingIntent);
            base.OnDestroy();
        }

        protected override void OnHandleIntent(Intent intent)
        {

        }

        public class OnFailureListener : Java.Lang.Object, IOnFailureListener
        {

            public void OnFailure(Java.Lang.Exception e)
            {

            }
        }

        public class OnSuccessListener : Java.Lang.Object, IOnSuccessListener
        {
            public void OnSuccess(Java.Lang.Object result)
            {

            }
        }
    }

Thanks for any help


Viewing all articles
Browse latest Browse all 204402

Trending Articles