2017-08-07 42 views
0

我可以知道如何创建一个警告对话框,询问用户在用户按下硬件后退按钮时是否确认退出cordova中的inappbrowser?我曾尝试使用ref.addEventListener('exit',onBackKeyDown);去做但不成功。请帮助我并提供任何解决方案。非常感谢你。退出科尔多瓦inappbrowser之前的警报对话框

回答

0

使用“_blank”而不是“_self”。如果在现有视图中打开外部源,则“退出”事件不会触发。

var ref = window.open('http://google.com', '_blank', 'location=no'); 

    ref.addEventListener('exit', function(event){ Exit(); }); 

function Exit(){ 
       navigator.notification.confirm(
       'Do you want to exit app?', 
       function(i){ 
        if(i==2) 
        { 
         navigator.app.exitApp(); //This will Close the App 
        } 
       },    
       'App Name',    
       'Cancel,Exit'   
      ); 
} 

希望这有助于你

+0

我想有对话框之前离开谷歌。我只是尝试你的代码...退出谷歌网站后出来的对话框。 – ping94

0

要创建一个警告对话框,询问用户是否确认退出inappbrowser科尔多瓦当用户按下硬件后退按钮,您需要:

一)修改位于your_project/platforms/android/src/org/apache/cordova/inappbrowser /中的InAppBrowserDialog.java。
B)添加import android.content.DialogInterface;添加所需的导入。
C)修改onBackPressed()函数 FROM:

public void onBackPressed() { 
if (this.inAppBrowser == null) { 
    this.dismiss(); 
} else { 
    // better to go through the in inAppBrowser 
    // because it does a clean up 
    if (this.inAppBrowser.hardwareBack() && this.inAppBrowser.canGoBack()) { 
     this.inAppBrowser.goBack(); 
    } else { 
     this.inAppBrowser.closeDialog(); 
    } 
} 

}

TO:

public void onBackPressed() { 
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context) 
    .setTitle("Exit") 
    .setMessage("You are about to exit, are you sure?") 
    .setPositiveButton("Exit", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int which){ 
      if (inAppBrowser == null) { 
       dismiss(); 
      } 
      else { 
       // better to go through the in inAppBrowser 
       // because it does a clean up 
       if (inAppBrowser.hardwareBack() && inAppBrowser.canGoBack()) { 
        inAppBrowser.goBack(); 
       } else { 
        inAppBrowser.closeDialog(); 
       } 
      } 
     } 
    }) 
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog,int which){ 
      dialog.cancel(); 
     } 
    }); 
    alertDialogBuilder.create(); 
    alertDialogBuilder.show(); 

}