2016-12-29 38 views
2

我想在用户离开应用程序时执行方法。我什么都试过:当用户离开应用程序时,Ionic 2执行方法

ionViewWillUnload() { 
    console.log("Wlill unload"); 
    this.leaveRoom(); 
} 

onDestroy() { 
    console.log("DESTROY"); 
    this.leaveRoom(); 
} 

ionViewWillLeave() { 
    this.leaveRoom(); 
} 

偏偏当用户关闭应用程序或当用户刷新页面,他们不会执行。

有什么想法?

回答

10
  1. 进口平台:

    import { Platform } from 'ionic-angular';

  2. 添加平台的constuctor:

    constructor(public navCtrl: NavController, platform: Platform)

  3. 订阅平台pauseresume

     platform.ready().then(() => { 
         this.platform.pause.subscribe(() => { 
          console.log('[INFO] App paused'); 
         }); 
    
         this.platform.resume.subscribe(() => { 
          console.log('[INFO] App resumed'); 
         }); 
        }); 
    
+0

upVote/downVote,mark解决/标记未解决,现在你自己回答,接下来是什么?而你只是使用我提到的方法。 – avilac

+0

我把它标记为已解决,因为虽然它有效,但它没有。您需要订阅平台暂停和恢复。而不是编辑所有的答案,我创建了一个新的 – TheUnreal

+0

当应用程序关闭时,“暂停”事件不会在IOS上触发。只有当它放在后台。 –

0

我想你在寻找可以在这里找到:

http://cordova.apache.org/docs/en/6.x/cordova/events/events.html#endcallbutton

特别是本次活动:

function onEndCallKeyDown() { 
    // Handle the end call button 
} 

为了使您必须首先声明这个监听事件工作:

document.addEventListener("endcallbutton", onEndCallKeyDown, false); 

的onPause事件:

document.addEventListener("pause", onPause, false); 

function onPause() { 
    // Handle the pause event 
} 

的onResume事件

document.addEventListener("resume", onResume, false); 

function onResume() { 
    // Handle the resume event 
} 

希望它能帮助!

+0

此事件仅支持'blackberry10'。 – TheUnreal

+0

我编辑了答案,现在它指向实际的科尔多瓦版本。 – avilac

+0

@TheUnreal在这里您可以获得为新版本更新的受支持平台列表:http://cordova.apache.org/docs/en/6.x/guide/support/index.html – avilac

相关问题