Solve Hbuilder packaged app back key to exit

Solve Hbuilder packaged app back key to exit

Problem Description: Hbuilder packaged app tap the phone if the return key, app will exit, can not return to the previous page.

Processing sub-page hits

Back key to exit, can not return, as follows:

// This is not on the front page, written in the sub-pages (subpages to return, click on the front page is returned to exit)
 // without introducing mui.js, we are h5 method 
document.addEventListener ( ' plusready ' , function () {
     var Webview = plus.webview.currentWebview ();
    plus.key.addEventListener('backbutton', function() {
        webview.canBack(function(e) {
            if(e.canBack) {
                webview.back();
            } else {
                webview.close(); //hide,quit
                //plus.runtime.quit();
            }
        })
    });
});

Home Click twice to exit the program code is as follows:

1  // this on the front page, to determine batter twice to exit.
2  // need to introduce mui.js version, when I was testing was introduced, I do not want to introduce the band mui have deleted you can (or see below that) 
3  mui.plusReady (function () {
 4                  // Home Back key processing
 5                  @ processing logic: 1 seconds, press the return key twice, then quit the application; 
. 6                  var first = null ;
 . 7                  plus.key.addEventListener ( ' BackButton ' , function () {
 . 8                      // first key, Tip "press exit application ' 
. 9                      IF (! first) {
 10                          first = new new a Date () the getTime ();.
 . 11                         mui.toast ( ' press exit the application ' );
 12 is                          the setTimeout (function () {
 13 is                              First = null ;
 14                          }, 1000 );
 15                      } the else {
 16                          IF ( new new . a Date () the getTime () - First < 1000 ) {
 . 17                              plus.runtime.quit ();
 18 is                          }
 . 19                      }
 20 is                  }, to false );
21              });
 22 is   
23 is  // not need mui.js version 
24 document.addEventListener ( ' plusready ' , function (A) {
 25              var First = null ;
 26 is              plus.key.addEventListener ( ' BackButton ' , function () {
 27                      // first button, suggesting 'press to exit the application' 
28                      IF (! first) {
 29                          first = new new a Date () getTime ();.
 30                          console.log ( ' press again to exit the application ' ); //Preferably with a custom toast tips 
31 is                          the setTimeout (function () {
 32                              First = null ;
 33 is                          }, 1000 );
 34 is                      } the else {
 35                          IF ( new new . A Date () the getTime () - First < 1000 ) {
 36                              plus.runtime .quit ();
 37 [                          }
 38 is                      }
 39                  }, to false );
 40          });

 

Complete js code:

1  // full version is, all pages are incorporated this piece of code, and the upper face and the home page corresponding to the code are necessary to introduce
 2   
. 3  // need mui.js version 
. 4 <Script>
 . 5 document.addEventListener ( ' plusready ' , function ( ) {
 . 6          var Webview = plus.webview.currentWebview ();
 . 7          plus.key.addEventListener ( ' BackButton ' , function () {
 . 8              webview.canBack (function (E) {
 . 9                  IF (e.canBack) {
 10                      Webview. Back ();             
 . 11                  } the else {
12 is                      // webview.close (); // hide, quit
 13 is                      // plus.runtime.quit (); 
14                      mui.plusReady (function () {
 15                          // Home key processing returns
 16                          @ processing logic: 1 second , press the return key twice, then quit the application; 
. 17                          var first = null ;
 18 is                          plus.key.addEventListener ( ' BackButton ' , function () {
 . 19                              // first key, prompt "press exit application ' 
20 is                              IF ( ! First) {
 21 is                                  First = new new a Date () the getTime ();.
 22 is                                 mui.toast ( ' press exit the application ' );
 23 is                                  the setTimeout (function () {
 24                                      First = null ;
 25                                  }, 1000 );
 26 is                              } the else {
 27                                  IF ( new new . a Date () the getTime () - First < 1500 ) {
 28                                      plus.runtime.quit ();
 29                                  }
 30                              }
 31 is                         }, false);
32                     });
33                 }
34             })
35         });
36     });
37  
38 //不需要mui.js版本
39 document.addEventListener('plusready', function() {
40         var webview = plus.webview.currentWebview();
41         plus.key.addEventListener('backbutton', function() {
42             webview.canBack(function(e) {
43                 if(e.canBack) {
 44 is                      webview.back ();             
 45                  } the else {
 46 is                      // webview.close (); // hide, quit
 47                      // plus.runtime.quit ();
 48                      // Home key processing returns
 49                      // processing logic: 1 seconds, press the return key twice, then quit the application; 
50                      var first = null ;
 51 is                      plus.key.addEventListener ( ' BackButton ' , function () {
 52 is                          // first key, suggesting 'then press again to exit the application ' 
53 is                          IF (! first) {
 54 is                             = First new new a Date () the getTime ();.
 55                              the console.log ( ' press exit the application ' );
 56 is                              the setTimeout (function () {
 57 is                                  First = null ;
 58                              }, 1000 );
 59                          } the else {
 60                              IF ( new new a Date () the getTime () - First <. 1500 ) {
 61 is                                  plus.runtime.quit ();
 62 is                             }
63                         }
64                     }, false);
65                 }
66             })
67         });
68     });
69 </script>

 

 

Guess you like

Origin www.cnblogs.com/Strangers/p/11927990.html