public async override void OnDestroy() { isRunning = false; ActivityRecognition.GetClient(this).RemoveActivityTransitionUpdates(pendingIntent); if (mGoogleActivityReceiver != null) { LocalBroadcastManager.GetInstance(this).UnregisterReceiver(mGoogleActivityReceiver); mGoogleActivityReceiver = null; } Bundle b = new Bundle(); var duration1 = DateTimeOffset.Now.Subtract(mSensorData.StartDate).TotalSeconds; if (duration1 > min_tracking_time) { mSensorData.Duration = duration1; await mDatabase.WriteAsync(tempRealm => { tempRealm.Add(mSensorData); }); b.PutBoolean("SavingSuccessful", true); } else { b.PutBoolean("SavingSuccessful", false); //using (var h = new Handler(Looper.MainLooper)) h.Post(() => //{ // Toast.MakeText(this.ApplicationContext, "Activity was too short to be saved.", ToastLength.Short).Show(); //}); } Intent onStop_intent = new Intent("StoppingSignal"); onStop_intent.PutExtra("report", b); LocalBroadcastManager.GetInstance(context).SendBroadcast(onStop_intent); mSensorManager.UnregisterListener(this); base.OnDestroy(); }
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { base.OnStartCommand(intent, flags, startId); if (intent.Action.Equals(Constants.ACTION_START_SERVICE)) { RegisterForegroundService(); data_idx = 1; isRunning = true; var startTicks = (int)(DateTime.Now.Ticks >> 23); // retain bits 23 to 55 var mStartDate = DateTimeOffset.Now; mSensorData = new MotionSensorData { StartTicks = startTicks, StartDate = mStartDate }; mSensorManager?.RegisterListener(this, mAccel, SensorDelay.Ui); mSensorManager?.RegisterListener(this, mMagn, SensorDelay.Ui); wklock.Acquire(); List <ActivityTransition> transitions = InitActivityTransitions(); ActivityTransitionRequest request = new ActivityTransitionRequest(transitions); pendingIntent = PendingIntent.GetBroadcast(context, 0, new Intent("GSM_TRANSITIONS_RECEIVER_ACTION"), PendingIntentFlags.UpdateCurrent); mGoogleActivityReceiver = new GoogleActivityReceiver(); LocalBroadcastManager.GetInstance(context).RegisterReceiver(mGoogleActivityReceiver, new IntentFilter("GSM_TRANSITIONS_RECEIVER_ACTION")); var task = ActivityRecognition.GetClient(context).RequestActivityTransitionUpdates(request, pendingIntent); task.AddOnSuccessListener(new OnSuccessListener()); task.AddOnFailureListener(new OnFailureListener()); } else if (intent.Action.Equals(Constants.ACTION_STOP_SERVICE)) { StopForeground(true); StopSelf(); } return(StartCommandResult.Sticky); }