简体   繁体   English

使用 Ionic (angular)。无法访问在显示屏/屏幕中有按钮的移动设备的硬件后退按钮

[英]Using Ionic (angular).Unable to access Hardware back button of mobile devices which have buttons inside the display/screen

I am facing a problem when I trying to access hardware back button so that I can produce an alert message to the user before the app get closed.我在尝试访问硬件后退按钮时遇到问题,以便在应用程序关闭之前向用户生成警报消息。 Initially I was unable to do , so I raised a question on stack overflow ( link of the question ) and with the help of the answers I almost solved my problem until I observed on some particular devices I unable to access the back button.最初我无法做到,所以我提出了一个关于堆栈溢出的问题问题的链接),在答案的帮助下,我几乎解决了我的问题,直到我在某些特定设备上观察到我无法访问后退按钮。 Noticing those devices which has back button inside the display (I think we can call this type of buttons as soft back button ) in those device I unable to produce alert box,to be more precise unable to access the back button.注意到那些在显示器内有后退按钮的设备(我认为我们可以将这种类型的按钮称为软后退按钮),在这些设备中我无法产生警报框,更准确地说无法访问后退按钮。 But If I touch on the screen and than press the button, it is working fine .但是如果我触摸屏幕而不是按下按钮,它工作正常。 basically if someone just launch the app and presses the back button the app gets exit.基本上,如果有人只是启动应用程序并按下后退按钮,应用程序就会退出。

It is very difficult for me to write and produce my issue properly, therefore I am sharing a link of video please watch and try to understand and ask for any clarification我很难正确地编写和制作我的问题,因此我正在分享视频链接,请观看并尝试理解并要求任何澄清

Link-1, The type of devices on which alert is not working Link-1, 警报不工作的设备类型

Link-2 On this type devices working fine Link-2 在这种类型的设备上工作正常

When you are in the app on another screen and press back button that time you go to the back screen.当您在另一个屏幕上的应用程序中并按下后退按钮时,您将返回后屏幕。 and when your screen is home or login, and that time within two seconds you press twice the time back button app is closed.当您的屏幕处于主页或登录状态时,并且在两秒钟内您按两次后退按钮应用程序关闭的时间。

public astTimeBackPress = 0;
public timePeriodToExit = 2000;

constructor(
    public toastController: ToastController,
    private platform: Platform,
    private nav: NavController,
    private router: Router,
  ) { }

handleBackButton() {
    this.platform.backButton.subscribe(() => {
      if (this.loaderOff) {
        document.addEventListener(
          'backbutton',
          () => {},
          false);
      } else {
        if (
          this.router.url === '/tabs/home' ||
          this.router.url === '/signin'
        ) {
          if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
            navigator['app'].exitApp();
          } else {
            this.presentToast('Press again to exit');
            this.lastTimeBackPress = new Date().getTime();
          }
        } else {
          this.nav.back();
        }
      }
    });
}

async presentToast(msg, color = 'dark') {
    const toast = await this.toastController.create({
      color,
      message: msg,
      duration: 3000,
      showCloseButton: true,
      closeButtonText: 'Close',
    });
    toast.present();
  }
public void callAlert(){
    AlertDialog.Builder builder1 = new AlertDialog.Builder(appCompatActivity);
    builder1.setMessage("Do you want to close.");
    builder1.setCancelable(true);

    builder1.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                    finish();
                }
            });

    builder1.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alert11 = builder1.create();
    alert11.show();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        callAlert();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM