最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - navigator.app.exitApp() is not working - Stack Overflow

programmeradmin4浏览0评论

I am developing Windows phone 8 PhoneGap app. Using navigator.app.exitApp() I am quiting the app from home screen in Windows phone 7. But when I tried the same in Windows phone 8, I am getting the error Unable to get property 'exitApp' of undefined or null reference. I would like to know why it is undefined in Windows phone 8 and not in Window phone 7 PhoneGap app. Also, I would like to know, is there any way to quit the app programmatically in Windows phone 8 PhoneGap app?.

I am developing Windows phone 8 PhoneGap app. Using navigator.app.exitApp() I am quiting the app from home screen in Windows phone 7. But when I tried the same in Windows phone 8, I am getting the error Unable to get property 'exitApp' of undefined or null reference. I would like to know why it is undefined in Windows phone 8 and not in Window phone 7 PhoneGap app. Also, I would like to know, is there any way to quit the app programmatically in Windows phone 8 PhoneGap app?.

Share Improve this question asked Jul 30, 2013 at 15:16 user2189143user2189143
Add a ment  | 

7 Answers 7

Reset to default 10

You can create a simple plugin. Add file ExitApp.css to your platforms/wp8/Plugins folder with:

using System.Windows;

namespace WPCordovaClassLib.Cordova.Commands
{
  class ExitApp : BaseCommand
  {
    public void execute(string options)
    {
        Application.Current.Terminate();                        
    }
  }
}

edit your platforms/wp8/config.xml and add to the widget tag:

<feature name="ExitApp">
  <param name="wp-package" value="ExitApp" />
</feature>`

then from you javascript call:

cordova.exec(null, null, "ExitApp", "execute", []);

You can use it in bination with backbutton event to close the app when the user clicks on backbutton in the main page:

function goBack(e){
  if(isInMyMainPage()) cordova.exec(null, null, "ExitApp", "execute", []);
}
document.addEventListener("backbutton", goBack, false)

I develop small application for Windows Phone 8.1 and the code below works for me:

window.close();

Navigator.app.exit() will not exit the application it will crash the application.

In Windows Phone 8, it is handled so it will just throw an exception.

You will have to write the below code in page_BackKeyPress event in CordovaView.xaml.cs

Application.Current.Terminate();

It will exit your application on pressing hardware backbutton.

Similar question here: How to exit an application in window phone 8 with phonegap 2.3 includes a fix that doesn't require any native hacking.

In version 3.6.3, navigator.app.exitApp() does work.

Here is where it is called in CordovaView.cs

void CordovaBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    string mandStr = e.Value;

    string mandName = mandStr.Split('/').FirstOrDefault();

    if (browserDecorators.ContainsKey(mandName))
    {
        browserDecorators[mandName].HandleCommand(mandStr);
        return;
    }

    CordovaCommandCall mandCallParams = CordovaCommandCall.Parse(mandStr);

    if (mandCallParams == null)
    {
        // ERROR
        Debug.WriteLine("ScriptNotify :: " + mandStr);
    }
    else if (mandCallParams.Service == "CoreEvents")
    {
        switch (mandCallParams.Action.ToLower())
        {
            case "overridebackbutton":
                string arg0 = JsonHelper.Deserialize<string[]>(mandCallParams.Args)[0];
                this.OverrideBackButton = (arg0 != null && arg0.Length > 0 && arg0.ToLower() == "true");
                break;
            case "__exitapp":
                Debug.WriteLine("Received exitApp mand from javascript, app will now exit.");
                CordovaBrowser.InvokeScript("eval", new string[] { "cordova.fireDocumentEvent('pause');" });
                CordovaBrowser.InvokeScript("eval", new string[] { "setTimeout(function(){ cordova.fireDocumentEvent('exit'); cordova.exec(null,null,'CoreEvents','__finalexit',[]); },0);" });
                break;
            case "__finalexit":
                IsExiting = true;
                // hide the browser to prevent white flashes, since about:blank seems to always be white
                CordovaBrowser.Opacity = 0d; 
                CordovaBrowser.Navigate(new Uri("about:blank", UriKind.Absolute));
                break;
        }
    }
    else
    {
        if (configHandler.IsPluginAllowed(mandCallParams.Service))
        {
            mandCallParams.Namespace = configHandler.GetNamespaceForCommand(mandCallParams.Service);
            nativeExecution.ProcessCommand(mandCallParams);
        }
        else
        {
            Debug.WriteLine("Error::Plugin not allowed in config.xml. " + mandCallParams.Service);
        }
    }
}

navigator.app.exitApp(); has been available in Apache Cordova WP8 projects since 3.4.0

<div onclick="navigator.app.exitApp()">Exodus</div>

Similar question here: https://groups.google./forum/#!msg/phonegap/9v2kOwXj6sQ/O8SVpd-qjicJ

But it basically says that windows phone 8 apps shouldn't be programatically exited.

发布评论

评论列表(0)

  1. 暂无评论