簡體   English   中英

后退按鈕確認退出應用程序 android + phonegap + jquery

[英]backbutton confirm exit app android + phonegap + jquery

如何在 Phonegap 在線系統中使用 jQuery Mobile 處理 Android 中的物理后退按鈕? 我想顯示確認退出應用程序(是 - 否)。 我嘗試了很多方法,但沒有任何效果。

試試這個:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
}

function onBackKeyDown(e) {
    e.preventDefault();
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
    // Prompt the user with the choice
}

function onConfirm(button) {
    if(button==2){//If User selected No, then we just do nothing
        return;
    }else{
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

當第一次單擊 set exitApp = true 時,第二次單擊后退按鈕將退出應用程序。 但是設置一個 Interval 將 exitApp 的狀態更改為 false。 所以當在 1 秒內點擊兩次后退按鈕時,將退出應用程序。

document.addEventListener('deviceready', function() {
    var exitApp = false, intval = setInterval(function (){exitApp = false;}, 1000);
    document.addEventListener("backbutton", function (e){
        e.preventDefault();
        if (exitApp) {
            clearInterval(intval) 
            (navigator.app && navigator.app.exitApp()) || (device && device.exitApp())
        }
        else {
            exitApp = true
            history.back(1);
        } 
    }, false);
}, false);

//設備就緒函數

document.addEventListener('deviceready', function() {

    document.addEventListener("backbutton", ShowExitDialog, false);

}, false);

// 按下后退按鈕時的對話框

 function ShowExitDialog() {
        navigator.notification.confirm(
                ("Do you want to Exit?"), // message
                alertexit, // callback
                'My APp', // title
                'YES,NO' // buttonName
        );

    }

// 調用退出函數

 function alertexit(button){

        if(button=="1" || button==1)
        {

            device.exitApp();
        }

}

不確定 phonegap 是否發生了變化,但 Suhas 和 Geet 的解決方案幾乎對我有用。 (但絕對給了我讓它工作所需的東西,謝謝!)后退按鈕基本上壞了。

以下是使其對我有用的調整:

加載應用程序后,請執行以下操作:

        document.addEventListener("backbutton", onBackKeyDown, false);//hijack the backbutton

        function onBackKeyDown(e){
            var page = $.mobile.activePage.attr('id');
            xStat.rec("back button clicked from page:" + page);
            if (page == 'menuPage'){//are you on the 'root page' from which phonegap will exit?
                e.preventDefault();
                $.mobile.changePage('#aboutToExitAppPage');
            } else {
                window.history.back();//restore normal back button functionality
            }
        }


//somewhere else in your code for the "aboutToExit app" page
        $('#aboutToExitAppPage').on('pageinit', function(){
            $(this).find('#exitApp').on('click', function(){
                navigator.app.exitApp();//quit the app.
            });
        });

和 HTML

<div data-role="page" id="aboutToExitAppPage">
    <div data-role="header" id="" data-position="inline" data-backbtn="true" >
        <h1 class=>About to exit app</h1>
    </div>
    <div data-role="content" style="width:100%; height:100%; padding:0;">
        <ul id="" data-role="listview" data-inset="true" data-theme="c" data-dividertheme="b" data-role="fieldContain">
            <input id="exitApp" class="" type="button" value="Exit" data-theme="">
            <a href="#menuPage" data-role='button'>Main Menu</a>
        </ul>
    </div>
</div>

試試這個:

// When Device ready
document.addEventListener('deviceready', function() {
    document.addEventListener("clickBackbutton", ExitDialogPrompt, false);
}, false); 

function ExitDialogPrompt() {
    navigator.notification.confirm(
        ("Do you want to Exit?"), // message
        prompt, // callback
        'Your title', // title
        'YES,NO' // button Name
    );
}

function alertexit(button){
    if (button=="0" || button==1) {
       navigator.app.exitApp();
    }
}

如果用戶單擊是,則退出應用程序。 首先通過運行cordova plugin add cordova-plugin-dialogs安裝對話框插件

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    document.addEventListener("backbutton", onBackKeyDown, false); //Listen to the User clicking on the back button
}

function onBackKeyDown(e) {
    e.preventDefault();
    navigator.notification.confirm("Are you sure you want to exit ?", onConfirm, "Confirmation", "Yes,No"); 
    // Prompt the user with the choice
}

function onConfirm(button) {
    if(button==1){//If User selected Yes, then exit app
        navigator.app.exitApp();
    } else {
        return;
    }
}

這對我有用。

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>

用這個

<script type="text/javascript">
    document.addEventListener("deviceready", onDeviceReady, false);

        function onDeviceReady(){
            document.addEventListener("backbutton", function(e) {
                e.preventDefault();

                        if(confirm("Exit App?")) {
                            navigator.app.exitApp();
                        }
            }, false);
        }
    </script>

要么

<script type="text/javascript">
document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady(){
        document.addEventListener("backbutton", function(e) {
            e.preventDefault();

            press = press + 1;
            if(press == 1) {
                alert('Press again to exit');
            }

            if(press == 2) {
                navigator.app.exitApp();
            }
        }, false);
    }
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM