コード例 #1
0
        //Delete activity record
        public async void DeleteActivityRecord()
        {
            Logger(Split + "Delete ActivityRecord");


            // Build the time range of the request object: start time and end time
            Calendar Cal = Calendar.Instance;
            Date     Now = new Date();

            Cal.Time = Now;
            long EndTime = Cal.TimeInMillis;

            Cal.Add(CalendarField.HourOfDay, -2);
            long StartTime = Cal.TimeInMillis;


            // Build the request body for reading activity records
            ActivityRecordReadOptions readRequest =
                new ActivityRecordReadOptions.Builder().SetTimeInterval(StartTime, EndTime, TimeUnit.Milliseconds)
                .ReadActivityRecordsFromAllApps()
                .Read(DataType.DtContinuousStepsDelta)
                .Build();


            // Call the read method of the ActivityRecordsController to obtain activity records
            // from the Health platform based on the conditions in the request body
            var GetTask = MyActivityRecordsController.GetActivityRecordAsync(readRequest);

            try
            {
                await GetTask;

                if (GetTask.IsCompleted)
                {
                    if (GetTask.Exception == null && GetTask.Result != null)
                    {
                        ActivityRecordReply result = GetTask.Result;
                        Logger("Reading ActivityRecord  response status " + result.Status.StatusCode);
                        IList <ActivityRecord> activityRecordList = result.ActivityRecords;
                        // Get ActivityRecord and corresponding activity data in the result
                        foreach (ActivityRecord activityRecord in activityRecordList)
                        {
                            DeleteOptions deleteOptions = new DeleteOptions.Builder().AddActivityRecord(activityRecord)
                                                          .SetTimeInterval(activityRecord.GetStartTime(TimeUnit.Milliseconds),
                                                                           activityRecord.GetEndTime(TimeUnit.Milliseconds), TimeUnit.Milliseconds)
                                                          .Build();
                            Logger("Begin Delete ActivityRecord is :" + activityRecord.Id);

                            // Delete ActivityRecord
                            var DeleteTask = MyDataController.DeleteAsync(deleteOptions);
                            try
                            {
                                await DeleteTask;

                                if (DeleteTask.IsCompleted)
                                {
                                    if (DeleteTask.Exception == null)
                                    {
                                        Logger("Delete ActivityRecord is Success:" + activityRecord.Id);
                                    }
                                    else
                                    {
                                        PrintFailureMessage(DeleteTask.Exception, "Delete");
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                PrintFailureMessage(ex, "Delete");
                            }
                        }
                    }
                    else
                    {
                        PrintFailureMessage(GetTask.Exception, "GetActivityForDelete");
                    }
                }
            }
            catch (Exception ex)
            {
                PrintFailureMessage(ex, "GetActivityForDelete");
            }
        }
コード例 #2
0
        // Read historical activity records
        public async void GetActivityRecord()
        {
            Logger(Split + "Get ActivityRecord");

            // Build the time range of the request object: start time and end time
            Calendar Cal = Calendar.Instance;
            Date     Now = new Date();

            Cal.Time = Now;
            long EndTime = Cal.TimeInMillis;

            Cal.Add(CalendarField.HourOfDay, -3);
            long StartTime = Cal.TimeInMillis;


            // Build the request body for reading activity records
            ActivityRecordReadOptions readRequest =
                new ActivityRecordReadOptions.Builder().SetTimeInterval(StartTime, EndTime, TimeUnit.Milliseconds)
                .ReadActivityRecordsFromAllApps()
                .Read(DataType.DtContinuousStepsDelta)
                .Build();

            CheckConnect();

            // Call the read method of the ActivityRecordsController to obtain activity records
            // from the Health platform based on the conditions in the request body
            var GetTask = MyActivityRecordsController.GetActivityRecordAsync(readRequest);

            try
            {
                await GetTask;

                if (GetTask.IsCompleted)
                {
                    if (GetTask.Exception == null && GetTask.Result != null)
                    {
                        ActivityRecordReply result = GetTask.Result;
                        Logger("Get ActivityRecord was successful!");
                        // Print ActivityRecord and corresponding activity data in the result
                        IList <ActivityRecord> activityRecordList = result.ActivityRecords;
                        foreach (ActivityRecord activityRecord in activityRecordList)
                        {
                            if (activityRecord == null)
                            {
                                continue;
                            }
                            DumpActivityRecord(activityRecord);
                            foreach (SampleSet sampleSet in result.GetSampleSet(activityRecord))
                            {
                                DumpSampleSet(sampleSet);
                            }
                        }
                    }
                    else
                    {
                        PrintFailureMessage(GetTask.Exception, "GetActivityRecord");
                    }
                }
            }
            catch (System.Exception ex)
            {
                PrintFailureMessage(ex, "GetActivityRecord");
            }
        }