public async void DeleteData() { // 1. Build a DataCollector object. DataCollector MyDataCollector = new DataCollector.Builder().SetPackageName(MyContext) .SetDataType(DataType.DtContinuousStepsDelta) .SetDataStreamName("STEPS_DELTA") .SetDataGenerateType(DataCollector.DataTypeRaw) .Build(); // 2. Build the time range for the deletion: start time and end time. DateTime startDate = DateTime.Parse("2020-12-15 09:00:00"); DateTime endDate = DateTime.Parse("2020-12-15 09:05:00"); // 3. Build a parameter object as the conditions for the deletion. DeleteOptions deleteOptions = new DeleteOptions.Builder().AddDataCollector(MyDataCollector) .SetTimeInterval(GetTime(startDate), GetTime(endDate), TimeUnit.Milliseconds) .Build(); // 4. Use the specified condition deletion object to call the data controller to delete the sampling dataset. // 5. Calling the data controller to delete the sampling dataset is an asynchronous Task. var DeleteTask = MyDataController.DeleteAsync(deleteOptions); try { await DeleteTask; if (DeleteTask.IsCompleted) { if (DeleteTask.Exception == null) { Logger("Success delete sample data from HMS core"); Logger(Split); } else { PrintFailureMessage(DeleteTask.Exception, "Delete"); } } } catch (Exception ex) { PrintFailureMessage(ex, "Delete"); } }
//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"); } }