locked
Get "Platform::ChangedStateException" when running SmsBackgroundTaskSample

    Question

  • I've tried the msdn sample "SmsBackgroundTaskSample", the code is as follows:

    void SampleSmsBackgroundTask::Run(IBackgroundTaskInstance^ taskInstance)
    {
        // Associate a cancellation handler with the background task.
        taskInstance->Canceled += ref new BackgroundTaskCanceledEventHandler(this, &SmsBackgroundTask::SampleSmsBackgroundTask::OnCanceled);
        BackgroundTaskDeferral^ deferral = taskInstance->GetDeferral();
    
        // Do the background task activity.
        DisplayToast(taskInstance);
    
        // Provide status to application via local settings storage
        auto settings = ApplicationData::Current->LocalSettings;
        auto key = taskInstance->Task->TaskId.ToString();
        settings->Values->Insert(key, "Completed");
        deferral->Complete();
    }
    
    
    void SampleSmsBackgroundTask::DisplayToast(IBackgroundTaskInstance^ taskInstance)
    {
        SmsReceivedEventDetails^ smsDetails = (SmsReceivedEventDetails^) taskInstance->TriggerDetails;
        create_task(SmsDevice::FromIdAsync(smsDetails->DeviceId)).then([smsDetails, this](SmsDevice^ smsdevice) -> task<ISmsMessage^>
        {
            return create_task(smsdevice->MessageStore->GetMessageAsync(smsDetails->MessageIndex));
        }).then([this] (ISmsMessage^ iSmsMessage)
        {
            // ISmsMessage is cast into ISmsBinaryMessage to pass into SmsTextMessage::FromBinaryMessage static function
            ISmsBinaryMessage^ smsbinarymessageinterface = (ISmsBinaryMessage^) iSmsMessage;
            auto smsbinarymessage = (SmsBinaryMessage^) smsbinarymessageinterface;
            auto smstextmessage = SmsTextMessage::FromBinaryMessage(smsbinarymessage);
    
            XmlDocument^ toastXml = ToastNotificationManager::GetTemplateContent(ToastTemplateType::ToastText02);
    
            XmlNodeList^ stringElements = toastXml->GetElementsByTagName("text");
    
            stringElements->Item(0)->AppendChild(toastXml->CreateTextNode(smstextmessage->From));
    
            stringElements->Item(1)->AppendChild(toastXml->CreateTextNode(smstextmessage->Body));
    
            ToastNotification^ notification = ref new ToastNotification(toastXml);
            ToastNotificationManager::CreateToastNotifier()->Show(notification);
        }).then([this, taskInstance] (concurrency::task<void> catcherrors)
        {
            // Catch exception thrown when accessing the sms device and reading messages
            try
            {
                catcherrors.get();
            }
            catch (Platform::Exception^ ex)
            {
                // Indicate that the background task is canceled.
                auto settings = ApplicationData::Current->LocalSettings;
                auto key = taskInstance->Task->TaskId.ToString();
                settings->Values->Insert(key, "Error");
            }
        }).wait();
    }

    when goes into DisplayToast, throw a exception "Platform::ChangedStateException" just the first sentence.
    SmsReceivedEventDetails^ smsDetails = (SmsReceivedEventDetails^) taskInstance->TriggerDetails;

    I don't understand why, can anyone help?

    Best Regards!

    Monday, October 21, 2013 3:16 AM

All replies