Is there any way to programmatically pause an Android app in Phonegap? I would like to mimic the behavior that occurs when you hit the HOME button. I've already had to overwrite the back button handler using this, and while in most cases I want it to do my action, when in a particular state the user would expect the app to minimize, and I want to replicate this behavior.
Keep in mind, on Android this is not the same as closing the app. That is quite easy to do with device.exitApp(); but I would like it to remember its state and keep running in the background. Especially if there's still an asynchronous job being done in the background.
Is there a feature in Phonegap to achieve this?
Possible duplicate of Manually pause an application in Android Phonegap, but I couldn't find some of the tools the OP mentioned there such as navigator, so I was nervious to totally edit and rewrite their post
The simple answer appears to be: no.
However, for anyone else that comes down this path, its not impossible. It's just that there isn't a feature of Phonegap to do it for you.
The Android equivalent of "sleeping an app" is actually just opening another intent. Specifically, opening the "Home" intent would sleep the running app and bring you back to the home screen. But as far as I can tell from asking around and scoping the docs, Phonegap doesn't have a direct way of opening intents.
What you (supposedly) can do is one of two things:
This plugin is supposed to be promising
Call the Java code that does it yourself using the means described here
Mind you, as of right now I've decided to not go any further with this, so I make no promises about either of those means, having not attempted them myself.
I invite anyone else who decides to pursue this further to update their experience here.
Related
Currently I am building an app and in IOS the users get the choice, what to do with the content. The most important activities are 'Copy' and 'WhatsApp'. Options are given through the activity viewcontroller as seen in the example image below:
This is build in Xamarin so the activity view controller is called through an IOS dependency. I am not very familiar with Android and I was wondering if android has something comparable?
(And what is it called, I could not find any hits on google with android and activity view)
If it exists and I know how it is called, I can use it for android in the Android dependency.
In Android, you can "talk" to other apps even if you don't know them, simply by telling the OS, "Hey, I have this data type, that I want to share, can you show the user apps and actions that can handle it so he/she can decide?"
They are called Intents (get it? :p)
Anyway, there's basically the official documentation about how to do this. So I suggest you refer back to it (and other readers of the future) for things may change between releases.
The short answer is: fire an intent indicating what you want to share, and let Android do its thing.
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.
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?
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.
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.
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...?
Option 2: User presses a button "Take screenshot" -> the app goes in the background and a notification is pushed.
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.
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.
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.
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.
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.
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
It allows you to carry out task in the background.
Please note this from the readme file.
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.
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
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:
navigator.screenshot.save(function(error,res){
if(error){
console.error(error);
}else{
console.log('ok',res.filePath);
}
});
I want to be able to tap the statusbar and the contents in the displayed app to be scrolled up to the top.
Is it technically possible that an app intercept my tap and send the appropriate command to the active app? I have noticed for example that AntTek quick settings shows a drop-down window when swiping down from statusbar. While using the app I did also notice that even by just tapping the statusbar (before beginning to move the finger down), the app seems to already interact with the touch as it dims the screen brightness in preparation to display it's "window" (sorry I use the MS Windows term), so clearly a statusbar tap CAN be sensed by an app.
Starting from this, I wonder if such an app could then send a message to the active program telling it to scroll up.
Is that possible? And if yes, the message must be customized to a particular app (let's say the browser as the most important) or is it standardized so the apps speak the same language between themselves?
I am not a programmer so answers with codesamples might be less helpful than a plain english explanation. Finding out that is possible would lead rather to pursuing a programmer to implement the idea rather than starting to develop it myself.
Thanks :-)
There is an XPosed-module which seems to do exactly what you want.
To use XPosed-modules, you'll need to root your phone and install the XPosed-framework.
The XPosed-module is called "Statusbar Scroll to Top" and its repository can be found here:
http://repo.xposed.info/module/com.mohammadag.statusbarscrolltotop
This will work for almost all app-lists, but for example won't work for browser-content.
If you want to scroll to the top of the page in a browser, then you'll probably do best to get a browser which can do that on its own. (For example Habit Browser has it built-in and respective plugins are available for Firefox.)
Yes it is possible. HiroMacro and Frep can do this, but it requires root. https://play.google.com/store/apps/details?id=com.prohiro.macro&hl=en
(how do they simulate mouse and keyboard interactions on other applications? i have no clue :/ anyone?)
Is it technically possible that an app intercept my tap and send the appropriate command to the active app?
No. One app cannot send fake input to another app, for security reasons.
An android app comprises of several activities. Each activity display a GUI that allows the user to perform a specific task. To take the user from one activity to another, app must use an Intent to define our app’s intent to do something.
An intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action.
Interacting one app to other app in android
google docs link
Is there an Android standard or coding convention / best practice that says whether or not an app should Exit (not running in background) or minimize (running in background) when the user "backs-out" of an app?
For example, you are on the home page of an app. What is the best-practice for handling the back press?
The obvious answer is to let android handle it's own back button press, which in turn exits the app. But is it okay to override and minimize?
Maybe this will help
"App follows Android Design guidelines and uses common UI patterns and icons:
App does not redefine the expected function of a system icon (such as the Back button)".
Android Core Standard Policies
That depends on the kind of application you are making in my opinion.
If for example you have an application that needs to save something to a database or whatever when the user clicks the back button, I would agree you just minimize the app. But even then, after the app has done saving, it should exit.
But if possible I would suggest you exit the application, this will also let the phone free up memory/CPU which can be used by other applications etc. It will also save battery.
And of course, this is the expected behaviour for an Android user: If you go back, you close the application. You don't want to confuse your users ;-)
Alright so I have an app that I would like to have utilize other apps. For example I have an app that does quite a number of things except for a directory look up since there is already an app that does that for my school. I know I can launch the application with intents, but that also brings them away from the navigation menu for my application. Is there anyway that I could run an app inside a view layout. I am not hopeful for this but I figured I would chance asking it anyway.
This is technically possible by using widgets. You can implement an AppWidgetHost, and other applications can create App Widgets to use inside your own app. This is how the launcher screen in Android works.
This, of course, will only work if other applications in question implement widgets. So, the general answer to your question would be no, it is not possible to host arbitrary applications or Views/Activities from other applications inside your own.
This not the Android design philosophy. You should send an Intent to the directory app, which I hope is designed to look up a result and then return it to you. The mechanism is startActivityForResult() in your app, and setResult() in the directory app.