locked
Windows Store App - Executing a single task when launched from a secondary tile

    Question

  • I have a Windows Store App that creates a secondary tile. When the tile is clicked, I want the app to perform, what is effectively, an off-line function (that is, I don't want to go back into the app, but want the app to do something and then exit). So in this case I want to launch the e-mail client, for example. Here's what I have so far:

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
    
            if (args.detail.arguments !== "") {
                // Activation arguments are present that 
                // were declared when the secondary tile was pinned to Start.
                args.setPromise(WinJS.UI.processAll().done(function () {
                    var emailaddress = args.detail.arguments;
                    var promise = Email.SendNewMail.sendEmail(emailaddress);
                    //promise.complete();
                    return;
                }));
    
            } else if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
    ...

    Email function here:

        sendEmail: function sendEmail(addess, subject, body) {
            var mailto = new Windows.Foundation.Uri("mailto:?to=" + addess + "&subject=" + subject + "&body=" + body);
            return Windows.System.Launcher.launchUriAsync(mailto);
        }

    The error I currently get is "Object has disconnected from its client" - which I assume is caused by the program exiting before the promise completes. How can this kind of behaviour be achieved in WInRT?

    Monday, February 11, 2013 10:11 PM

Answers

  • Hi PCMichaels,

    The behavior you are asking for (silently taking an action and then exiting) is inappropriate and unsupported for a Windows Store app. It is very likely to confuse your users. The tile is intended to navigate into your app, not as a general purpose action. At most, your app should take the action and land on a page letting the user know what has happened. It can also give the user an opportunity to continue within your app.

    Note that the Certification Requirement 3.6 says:

    Your app must neither programmatically close nor offer UI affordances to close it

    --Rob

    • Marked as answer by pcmichaels Thursday, February 14, 2013 8:04 AM
    Wednesday, February 13, 2013 9:13 AM
    Owner

All replies


  • Hi pcmichaels,

    If you directly execute the email sending code after your windows store app is launched via seconary tiles, you have to add code to ensure that it is cancelable or you can use "try ... catch..." to protect the code block that might occur error. It depends how your email sending task is implemented.

    Another possible approach is that you can tried registering a background task which is dedicated to email sending tasks. Background tasks can keep executing even if your app is suspended. And whenever your app is launched via secondary tile, you might consider start a background task to send the email and remove it the next time (another email is to be sent). Or if you do not need to send the email immediately, you can even consider using a single timer trigger based background task which constantly checks some storage data (such as html 5 local storage or windows store specific temp folder or localSettings) for a email sending list. And when the tile is clicked by user, you simply add a new record into the list.


    #Supporting your app with background tasks (Windows Store apps using JavaScript and HTML) (Windows)
    http://msdn.microsoft.com/en-us/library/windows/apps/Hh977046.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, February 13, 2013 5:30 AM
    Moderator
  • Hi PCMichaels,

    The behavior you are asking for (silently taking an action and then exiting) is inappropriate and unsupported for a Windows Store app. It is very likely to confuse your users. The tile is intended to navigate into your app, not as a general purpose action. At most, your app should take the action and land on a page letting the user know what has happened. It can also give the user an opportunity to continue within your app.

    Note that the Certification Requirement 3.6 says:

    Your app must neither programmatically close nor offer UI affordances to close it

    --Rob

    • Marked as answer by pcmichaels Thursday, February 14, 2013 8:04 AM
    Wednesday, February 13, 2013 9:13 AM
    Owner
  • Hi Rob,

    Can I confirm that you are basically saying that Windows Store does not allow any app that creates a secondary tile which, when clicked, performs an automated task?  However, based on Steven's answer above, it is acceptable to create a background task to do the same, which affords no user action whatsoever?

    ...Paul

    Wednesday, February 13, 2013 9:52 AM
  • Hi Paul,

    Having an app perform an automated task from a secondary tile is likely to surprise the user and is not good design. It may be allowed: check the Certification Requirements to see what they say. Having the app close itself after doing so is pretty clearly a violation of 3.6.

    I don't see how a background task will help here, since your goal as I understand it is to react to user input. Creating a background task in response to the tile task may prevent the app from crashing when it exits inappropriately, but it doesn't address the root issue that the app shouldn't exit there.

    --Rob

    Thursday, February 14, 2013 3:15 AM
    Owner
  • Thank you Rob for point out this.

    @Paul,

    My original reply hasn't noticed that you want to programmtically close/terminate the app after start/execute the email sending task/code. If that's the case, I agree with Rob that this is not recommended and might violate the cert requirements of windows store app. We should let user to suspend or exit app unless there occurs unrecoverable error.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 14, 2013 3:59 AM
    Moderator
  • Okay - thanks to you both for your help.
    Thursday, February 14, 2013 8:03 AM