In HTML5, how can I keep an Android device’s screen on? - android

We are developing an App in HTML5 using jQuery.
Is there a way/method, in JavaScript or another technology, to keep the screen "on"?

I think there is another way doing it without any java code. You can add a video of 1 second with infinite repeat and hidden and it will do the trick. I have read it somewhere but i don't remember where.
Maybe you could use one of the blank videos below:
https://github.com/esc0rtd3w/blank-intro-videos
https://github.com/kud/blank-video
IT WORKS!

Google's WebVR polyfill has a way to do this on Chrome. It basically creates a tiny video (with a data URL, so no extra downloads needed) and loops it.
It seems like a hack, so I wouldn't be surprised if it's not reliable in the future.
They have code that achieves the same thing on iOS too, by triggering a location update.
Here's the relevant code for Android:
var Util={};
Util.base64 = function(mimeType, base64) {
return 'data:' + mimeType + ';base64,' + base64;
};
var video = document.createElement('video');
video.setAttribute('loop', '');
function addSourceToVideo(element, type, dataURI) {
var source = document.createElement('source');
source.src = dataURI;
source.type = 'video/' + type;
element.appendChild(source);
}
addSourceToVideo(video,'webm', Util.base64('video/webm', 'GkXfo0AgQoaBAUL3gQFC8oEEQvOBCEKCQAR3ZWJtQoeBAkKFgQIYU4BnQI0VSalmQCgq17FAAw9CQE2AQAZ3aGFtbXlXQUAGd2hhbW15RIlACECPQAAAAAAAFlSua0AxrkAu14EBY8WBAZyBACK1nEADdW5khkAFVl9WUDglhohAA1ZQOIOBAeBABrCBCLqBCB9DtnVAIueBAKNAHIEAAIAwAQCdASoIAAgAAUAmJaQAA3AA/vz0AAA='));
addSourceToVideo(video, 'mp4', Util.base64('video/mp4', 'AAAAHGZ0eXBpc29tAAACAGlzb21pc28ybXA0MQAAAAhmcmVlAAAAG21kYXQAAAGzABAHAAABthADAowdbb9/AAAC6W1vb3YAAABsbXZoZAAAAAB8JbCAfCWwgAAAA+gAAAAAAAEAAAEAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAIVdHJhawAAAFx0a2hkAAAAD3wlsIB8JbCAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAQAAAAAAIAAAACAAAAAABsW1kaWEAAAAgbWRoZAAAAAB8JbCAfCWwgAAAA+gAAAAAVcQAAAAAAC1oZGxyAAAAAAAAAAB2aWRlAAAAAAAAAAAAAAAAVmlkZW9IYW5kbGVyAAAAAVxtaW5mAAAAFHZtaGQAAAABAAAAAAAAAAAAAAAkZGluZgAAABxkcmVmAAAAAAAAAAEAAAAMdXJsIAAAAAEAAAEcc3RibAAAALhzdHNkAAAAAAAAAAEAAACobXA0dgAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAIAAgASAAAAEgAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABj//wAAAFJlc2RzAAAAAANEAAEABDwgEQAAAAADDUAAAAAABS0AAAGwAQAAAbWJEwAAAQAAAAEgAMSNiB9FAEQBFGMAAAGyTGF2YzUyLjg3LjQGAQIAAAAYc3R0cwAAAAAAAAABAAAAAQAAAAAAAAAcc3RzYwAAAAAAAAABAAAAAQAAAAEAAAABAAAAFHN0c3oAAAAAAAAAEwAAAAEAAAAUc3RjbwAAAAAAAAABAAAALAAAAGB1ZHRhAAAAWG1ldGEAAAAAAAAAIWhkbHIAAAAAAAAAAG1kaXJhcHBsAAAAAAAAAAAAAAAAK2lsc3QAAAAjqXRvbwAAABtkYXRhAAAAAQAAAABMYXZmNTIuNzguMw=='));
video.play();

The future answer - because it's experimental now - is to use the new API called: Screen Wake Lock API
I think you can mix it with previous workaround answers
// Create a reference for the Wake Lock.
let wakeLock = null;
// create an async function to request a wake lock
try {
wakeLock = await navigator.wakeLock.request('screen');
statusElem.textContent = 'Wake Lock is active!';
} catch (err) {
// try other solutions here ...
}
You can follow the support of this feature here

There is no way to ONLY write javascript or other web code to keep the screen on, without writing at least a little java code.
To explain why I am so certain, if you are developing a web app through html5 you MUST use a WebView as the main "screen" of your application to host your html,javascript code. So your "web code" does not directly run in the application but uses a View as its holder. As you can guess you can't just lock the screen from some code that is not even running in the native part.
I can provide a very easy and simple way to keep the screen on if you are not an expert in android programming. In the first activity, that uses the WebView I guess, add in onCreate after super:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Simplest method is to go to the Settings, Developer Options, and to select "Stay awake while charging". No need to code anything!
This supposes that OP wants to keep the screen on while connected to an external power source, which is reasonable, otherwise no clever coding will prevent the screen from going off soon when the internal battery drains empty.
The external power source can be as simple as a portable usb battery. When that eventually gets empty, the device will go to sleep as usual, but will remain functional on its internal battery.
I found this solution here:
posting about keeping the screen on

Keeping the device on does not depend on the OS but on the browser. Chrome is in the process of implementing Wake Lock API, but it is still experimental.
Until fully available, there is a way to mock the api by playing a base64 video in the background. This locks the sleep mode with all browsers.
You can find a webcomponent implementation here https://github.com/madeInLagny/mil-no-sleep

Related

How can we read device status in react native? (eg: phone in call , sleep mode, etc...)

I just need to read the status of the phone eg: in phone call /sleep mode etc ... is there any way to read all the status ?
Its better to use react-native-device-info library which provides so much APIs to detect details of the mobile.
But to detect if user is in a call, you have to use another library such as react-native-check-phone-call-status. I haven't used this library but worth to give a try.

Playing sounds sequentially on android's google chrome (given the new restrictions on playing sound)

I have a small app that plays sequential sounds (a teaching app playing the sillables of a word)
This could be accomplished by firing an event right after each sound stopped playing. Something like:
var sounds = new Array(new Audio("1.mp3"), new Audio("2.mp3"));
var i = -1;
playSnd();
function playSnd() {
i++;
if (i == sounds.length) return;
sounds[i].addEventListener('ended', playSnd);
sounds[i].play();
}
(source)
However, now android chrome has implemented some new restrictions on how to play sound: Sound events must all be fired by a user action.
So, when I run code very similar to the above, the first sound plays, and then I get
Uncaught (in promise) DOMException: play() can only be initiated by a user gesture.
How can a sequence of sounds, determined at run time, be played on Android's Chrome?
To start with, Google Chrome on Android has been having the limitation of not allowing applications to play HTML audio(s) without an explicit action by the user. However, it is different than how stock browser(s), in most cases, handles it.
The reason, as Chromium Org puts it, is that, Autoplay is not honored on android as it will cost data usage.
You may find more details on the same here.
Apart from the fact that this results in wastage of bandwidth, this also makes some sense, since mobile devices are used in public and in houses, where unsolicited sound from random Web sites could be a nuisance.
However, in the later versions, this idea was over ruled and Chrome on Android started allowing autoplay of HTML audios and videos on it. Again after a set of reviews and discussions, this feature was reverted to what it was, making it mandatory for a user action to invoke HTML audios and videos on the Chrome for Android.
Here is something that I found more on the same. As it says, the reason stated was that "We're going to gather some data about how users react to autoplaying videos in order to decide whether to keep this restriction". And hence the playing option without a user action was reverted back.
You can also find more about the blocking of _autoplay of audio(s) and video(s) here on Forbes and The Verge.
However, this is something that I can suggest you to try which will help you achieve what you intend to. All you have to do is copy this code and paste in your Chrome for Android. This helps you reset the flag which is default set to not allowing to play HTML audios and videos without user interaction:
chrome://flags/#disable-gesture-requirement-for-media-playback
OR
about:flags/#disable-gesture-requirement-for-media-playback
If the above procedure doesn't help/work for you, you can do this:
Go into chrome://flags OR about:flags (this will direct you to chrome://flags) and Enable the "Disable gesture requirement for media playback" option (which is actually the same as the above URL specified).

keepRunning PhoneGap/Cordova

Anyone can explain me how keepRunning works in the config.xml for Android.
I mean, I don't want to know how to write the instruction but how does it work, how does it affect the execution of the Android app ? Does it create an Service in background ?
If anyone can find the source where we can see how does it work, that will be great
Thanks.
Edit : I try to analyze the generated code, analyze the RAM, services and processus in the setting of Android. And my conclusion is..... that do nothing.
If you try to make a app which track the user with GPS, dont use Cordova. To track the user correctly, you need to make a Service with the START_STICKY option. So, it's in native code. you lost the interest of the CrossPlatform because you have to recode the service for all platforms and in my opinion, the communication between Native Service and Cordova App is not easy.
In conlusion, if you use Cordova, you have to know you can't use the power of all native, you have to make choise :
- easy dev (subjective) and crossplaform (really crossplatform ?)
and
- Native dev with its power and no compatibility problems but you have to make one app for one platform
I'm not a JS/Cordova developer, I'm an Android developer. Once I worked on a Cordova plugin, faced some issues and did some investigations on the subject.
General purpose of keepRunning flag is to indicate if JS timers should be stopped when the app is paused (goes to background). Answering your question: no, it doesn't create any new Service. Existing design is quite plain in this regard.
The keepRunning flag is defined in CordovaActivity.java as follows:
// Keep app running when pause is received. (default = true)
// If true, then the JavaScript and native code continue to run in the background
// when another application (activity) is started.
protected boolean keepRunning = true;
Its main purpose is to disable JS timers when Cordova app is paused, in CordovaWebView.java:
public void handlePause(boolean keepRunning)
{
LOG.d(TAG, "Handle the pause");
// Send pause event to JavaScript
this.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};");
// Forward to plugins
if (this.pluginManager != null) {
this.pluginManager.onPause(keepRunning);
}
// If app doesn't want to run in background
if (!keepRunning) {
// Pause JavaScript timers (including setInterval)
this.pauseTimers();
}
paused = true;
}
Note that plugins are also notified via PluginManager, so in theory they can handle app paused events, to stop (or not) their activity in background, depending on keepRunning flag.
In my case I had an issue/bug when keepRunning was true, but JS timers were stopped anyway. It happened because there is additional functionality related to that flag, in CordovaActivity.java:
/**
* Launch an activity for which you would like a result when it finished. When this activity exits,
* your onActivityResult() method will be called.
*
* #param command The command object
* #param intent The intent to start
* #param requestCode The request code that is passed to callback to identify the activity
*/
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return results
if (command != null) {
this.keepRunning = false;
}
// Start activity
super.startActivityForResult(intent, requestCode);
}
When Cordova app launches another Android activity, main Cordova activity (screen with WebView) goes to background and is therefore paused. In my case it was made via Google Maps plugin which started GM screen over Cordova app.
The code above turns off keepRunning flag, and it means that JS timers are stopped anyway when the called activity appears (in CordovaActivity.onPause method) regardless keepRunning is true or false!
It looks like a kind of trick implemented for some unclear (and not documented) purpose, I do not know its context. In my case it caused a bug, and I just removed keepRunning handling in startActivityForResult, recompiled Cordova and it worked OK.
ADDED: About using a Service for GPS - you are quite right, I agree. As an Android developer with relevant (GPS) experience I can say that a right approach (and possible the only acceptable) is to use a service for that. As far as I know Cordova doesn't provide any functionality for it, so I think it should be made via a plugin. I mean you can write native Android code for GPS functionality (implemented as a Service) and access it from JS code. I believe it is a common solution in Cordova for such cases.
Perfect answer, helped me a lot! I was searching for the solution to the problem for 2 days now.
In my case I'm currently developing a cordova plugin for login purposes. For the login I use an external form which I load in a webview. For two days now I was struggling with the fact that the "Password forgotten" link and every other link on the page I loaded was working, but I wasn't able to submit my form. Only when I hit the back button and through this finished the intent holding the webview, it did submit and proceed.
Turns out that the keepRunning handling was the only problem here. In the end I replaced: `
cordova.startActivityForResult(this, intent, 0);
by:
cordova.setActivityResultCallback(this);
cordova.getActivity().startActivityForResult(intent, 0);
which basically fulfills the whole job Cordova's startActivityForResult would do The only thing that's left out is the whole keepRunninghandling which messed up my plugin in the first place.
Thanks again, Mixaz!

Android ICS/JB, Reboot to Safe Mode Programmatically?

I am making a Utility app for my galaxy nexus. I want to reboot my tablet in safe mode.
I tried to look in PowerManager
PowerManager p = (PowerManager) getSystemService(POWER_SERVICE);
p.reboot(reason);
It seems this will not reboot the device in safe mode. Is it possible to reboot the device programmatically? How?
Basically there are two known ways to enter Safe Mode:
Android detects a problem with a newly installed app and force-closes it while entering into Safe Mode.
A combination of key presses at power application;
I doubt there's yet another way of doing it. If there was, most recoveries and power menus of Custom ROMS would have included that.
The string passed to reboot() is a kernel param, and would have effect only if device's kernel has that option. You can try some options here.
UPDATE:
Safe Mode is toggle is inside PackageManagerService of Android's system server ("package" service):
public void enterSafeMode() {
enforceSystemOrRoot("Only the system can request entering safe mode");
if (!mSystemReady) {
mSafeMode = true;
}
}
and here are some points about using it from any APP :
Process executing this code must be System or have Root previleges
This is an internal service and off-limits to any outside code. Though, some system classes indeed get implementation stubs (IPackageManager) of this service.
The mode change can only be useful when system is yet to be ready.
Let's suppose your app does turn on safe mode some how, due to safe mode being enabled, it won't be around to turn it off. Unless its a system app, built into ROM.
A third way to enter safe mode (available sometime after GingerBread 2.3.5)
With device fully powered up, Press power button, and the LONG press on the power off menu item. An option appears to go into safe mode. Because of this, there may now be a way to programmaticaly enter safe mode. Sure hope so to help troubleshoot. i am going from memory on a lifehacker article which referenced yet another source.

How to test vibration function in android?

I am writing a simple Phonegap application for Android. This program will send notification to notification bar and make the phone vibrate periodically.
I use navigator.notification.vibrate(time_period) to achieve the target. According to this article, both beep and vibration are not supported by android emulator. Hence, I was expecting that there could be entry indicating failure of it in the Catlog, but there is no such entry. The question is how to make sure that a vibration event has happened or failed (without deploying to a device).
AppHarbor looks like one of the ways to debug Phonegap application remotely. I wonder if there is other local ways to test Phonegap application as an HTML5 website in a Chrome browser (navigator.notification call is a standard call)? If yes, then it is probably possible to somehow parse the browser's console automatically to find out if the vibration event has happened.
Can you hide the vibrate() call behind an abstraction which you can replace depending on which platform you are using?
For example
var vibrateFunc = function(time_period) {
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
console.log('vibrating for ' + time_period)
} else {
navigator.notification.vibrate(time_period)
}
}
and then have your app code call vibrateFunc() whenever it wants to vibrate.

Categories

Resources