当应用程序不在前台时,Ionic / Cordova触发应用程序动作。

时间:2021-07-17 15:52:42

Original Question (Listen to hardware buttons press)

I am wondering if it is possible to listen to the hardware buttons of a phone using Ionic / Cordova.
The important thing is that this should also work when the user is not currently using the app like somehow through a background service?
Basically I want to take a screenshot when the user presses the volume down and the power button at the same time in a hybrid app using Ionic. The app then offers to edit that screenshot or save it directly.

我想知道是否可以使用Ionic / Cordova来监听电话的硬件按钮。重要的是,当用户不像通过后台服务那样使用应用程序时,这也应该起作用。基本上,我想在用户按下音量和电源键的同时,在一个使用Ionic的混合应用程序中进行屏幕截图。然后,应用程序会提供编辑该截图或直接保存它。

I have done some research and found the cordova events to listen to those buttons. I also read that the pause event should be fired when the power button is pressed. Anyhow, I don't think listening to those events would work outside of that app?

我做了一些研究,发现了cordova事件来听这些按钮。我还读到,当按下电源按钮时,暂停事件应该被触发。不管怎样,我不认为听这些事件会在那个应用程序之外工作?

Original Question Conclusion

After some more research I conclude that this is not really possible using Cordova / Ionic. Even the native implementations of this problem seem to be more of a hack than a clean solution. The reason for this is, that the app currently on top should have the control of handling the various button events. You could interfere with another app and you always need to run a background service.
Anyhow, all the solutions I saw seem too dirty for me.

在进行了更多的研究之后,我得出这样的结论:使用Cordova / Ionic是不可能的。即使是这个问题的本地实现,看起来也比干净的解决方案更有挑战性。这样做的原因是,目前在顶部的应用程序应该能够控制处理各种按钮事件。你可能会干扰另一个应用程序,你总是需要运行后台服务。不管怎样,我看到的所有解决方案对我来说都太脏了。

Updated Question / Reevaluation of the problem

Basically I want to take a screenshot when the user presses the volume down and the power button at the same time in a hybrid app using Ionic.

基本上,我想在用户按下音量和电源键的同时,在一个使用Ionic的混合应用程序中进行屏幕截图。

If I can't solve the current problem I have to rethink it. So:
Actually, I just want the user to have the ability to take a screenshot in a way that offers a good usability experience.

如果我不能解决目前的问题,我必须重新考虑。所以:实际上,我只是希望用户能够以一种提供良好可用性体验的方式进行屏幕截图。

There are various ways I can do that as an alternative to pressing two hardware buttons simultaneously.

我可以用多种方法来代替同时按两个硬件按钮。

Option 1: User presses a button "Take screenshot" -> the app goes in the background, a screenshot is taken from the underlying app, the app comes back to the foreground.
AFAIK this wouldn't even work in iOS, only Android offers a "draw on top of other applications", so in iOS the screenshot would simply be taken from the home screen in all cases...?

选项1:用户按下“截图”->应用程序在后台运行,屏幕截图从底层app中取出,app回到前台。AFAIK在iOS中甚至都不工作,只有Android提供了“在其他应用程序之上的绘图”,所以在iOS中,屏幕截图在所有情况下都可以从主屏幕上删除……

Option 2: User presses a button "Take screenshot" -> the app goes in the background and a notification is pushed.

选项2:用户按下“截图”->应用程序进入后台,推送通知。

  • This notification could say "Tap to take a screenshot". The notification panel (which is always drawn on top of other applications) disappears, the screenshot is taken and the app pops up. This could be done using Local Notifications and listening to the on clear, cancel etc. events.
  • 这个通知可能会说“点击截图”。通知面板(它总是在其他应用程序的顶部)消失,屏幕截图被拿走,应用程序弹出。这可以通过使用本地通知并监听clear、cancel等事件来完成。
  • The notification could even have custom buttons using OneSignal (see Action Buttons). One button could say "Add screenshot", another one could say "Done", so the user could even take multiple screenshots at once.
  • 该通知甚至可以使用OneSignal(见操作按钮)定制按钮。一个按钮会说“添加截屏”,另一个按钮可以表示“完成”,因此用户甚至可以同时进行多个屏幕截图。

Option 3: A button could be added to the shortcuts that triggers the take a screenshot action. I don't know if that is even possible and if that works on all devices.

选项3:一个按钮可以添加到触发截图动作的快捷方式中。我不知道这是否可行,是否适用于所有设备。

I would say the best solution would be to go with Option 2: Custom Action Buttons on a Notification. Notifications should be a must have on all devices and should always be drawn on top of the current app. So the user just swipes down the notifications and presses a button to take a screenshot.

我想说最好的解决方案是使用选项2:在通知上定制操作按钮。通知应该是所有设备上必须的,并且应该总是在当前应用程序的顶部被绘制,这样用户就可以删除通知并按下一个按钮来截图。

I don't really have any prior knowledge to this topic so I would be glad if somebody could confirm or improve my thinking process.

我对这个话题没有任何事先的了解,所以如果有人能证实或改进我的思考过程,我会很高兴。

1 个解决方案

#1


1  

Edit:

编辑:

I have done a bit of research into this, mainly just to satisfy my own curiosity. I came across this plugin

我做了一些研究,主要是为了满足我自己的好奇心。我偶然发现了这个插件。

https://github.com/katzer/cordova-plugin-background-mode/blob/master/README.md

https://github.com/katzer/cordova-plugin-background-mode/blob/master/README.md

It allows you to carry out task in the background.

它允许你在后台执行任务。

Please note this from the readme file.

请从readme文件中注意到这一点。

Store Compliance

Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.

大多数移动操作系统不支持无限后台任务,因此不兼容公共商店供应商。成功的征服不是成功的。

Use the plugin by your own risk!

使用插件的风险!

This probably isn't a big deal on Android but there is an open issue with regards to Apple app store submissions being rejected. Read through the issue to see how others over one this.

这在Android上可能不是什么大问题,但苹果应用商店提交的申请被拒绝是一个公开的问题。通读这个问题,看看其他人是如何解决这个问题的。

https://github.com/katzer/cordova-plugin-background-mode/issues/122

https://github.com/katzer/cordova-plugin-background-mode/issues/122

Original Answer:

最初的回答:

If you want to take a screenshot there is this plugin:

如果你想截图,有一个插件:

https://github.com/gitawego/cordova-screenshot/blob/master/README.md

https://github.com/gitawego/cordova-screenshot/blob/master/README.md

Maybe you could listen for some other events whilst using the pause listener. If they are activated then using the screenshot plugin referenced above call:

也许您可以在使用pause侦听器时侦听其他事件。如果它们被激活,那么使用上面提到的截图插件:

navigator.screenshot.save(function(error,res){
    if(error){
        console.error(error);
    }else{
        console.log('ok',res.filePath);
    }
});

#1


1  

Edit:

编辑:

I have done a bit of research into this, mainly just to satisfy my own curiosity. I came across this plugin

我做了一些研究,主要是为了满足我自己的好奇心。我偶然发现了这个插件。

https://github.com/katzer/cordova-plugin-background-mode/blob/master/README.md

https://github.com/katzer/cordova-plugin-background-mode/blob/master/README.md

It allows you to carry out task in the background.

它允许你在后台执行任务。

Please note this from the readme file.

请从readme文件中注意到这一点。

Store Compliance

Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.

大多数移动操作系统不支持无限后台任务,因此不兼容公共商店供应商。成功的征服不是成功的。

Use the plugin by your own risk!

使用插件的风险!

This probably isn't a big deal on Android but there is an open issue with regards to Apple app store submissions being rejected. Read through the issue to see how others over one this.

这在Android上可能不是什么大问题,但苹果应用商店提交的申请被拒绝是一个公开的问题。通读这个问题,看看其他人是如何解决这个问题的。

https://github.com/katzer/cordova-plugin-background-mode/issues/122

https://github.com/katzer/cordova-plugin-background-mode/issues/122

Original Answer:

最初的回答:

If you want to take a screenshot there is this plugin:

如果你想截图,有一个插件:

https://github.com/gitawego/cordova-screenshot/blob/master/README.md

https://github.com/gitawego/cordova-screenshot/blob/master/README.md

Maybe you could listen for some other events whilst using the pause listener. If they are activated then using the screenshot plugin referenced above call:

也许您可以在使用pause侦听器时侦听其他事件。如果它们被激活,那么使用上面提到的截图插件:

navigator.screenshot.save(function(error,res){
    if(error){
        console.error(error);
    }else{
        console.log('ok',res.filePath);
    }
});