Clock stops when lockscreen, ActivityStarter workaround with com.android.deskclock

Hi all…my first appinventor2 app
Purpouse:a timer
Details(if interested,or skip to The Problem):at work i have 10 minutes break.i have to badge out and badge in.
The badge machine shows HHours:MMinutes but no SSeconds
Since the badgemachine collect only the minutes between in and out,i want to maxime my break
example:i badge out at 10:00:whateverseconds, so i have to badge in < 10:10:59
so,depending on which SSeconds a badge,i can have from 10MM:01SS to 10MM:59SS to break.
Making syncronisation between my smartphone internal clock and badgmachineclock,i can do some math and find exactly how long my break is.
Then i set a timer accordingly and start it.It beeps ie 10seconds before i have to badgein.Easy!

The Problem: the clock sensor component i use for the countdown to decrement the inizitial variables MM and SS STOPS WHEN I LOCK THE SCREEN (even without closing the app),making my app totally useless.
so the Questions:
1)confirm no way forappinventor2 to work in background, as reported in tons of forum etc?
2)if 1)is confirmed,i tought bout a workaround using ActivityStarter and the internal clock/alarm/timer of my smartphone. i figured the package is com.android.deskclock but i have no idea how to pass the information(my starting MM and SS after myapp math) to the Timer and start it
Any idea and suggestions and examples with blocks? ie: MM=10 SS=21 set Timerstarting value MM:SS,start timer.

my smartphone is Huawei p8 lite Emui 8.0.0 Android 8.0.0 build 8.0.0.400(c432)
thanks a lot
Claudio

No way to run in the background using App Inventor.

What a developer can do is use a Stay Awake routine . Please try one of the methods below instead of using ActivityStarter (which will not work to do what you hope for).

Either use a Stay Awake block routine using a Notifier

or you use Taifun’s Tools Extension: https://puravidaapps.com/tools.php

Be aware using either an extension or a block routine drains your device battery rapidly . These work around methods are not running the app in the Background, they force the app screen to stay open.

1 Like

Hi,thanks for quick answer
I tryed right now both method you suggested.
Both work fine when my smartphone is connected via usb to the pc…they prevent the screen to autofade and to go into Lockscreen…and even if i lock the phone with the the phisical key,wait a while and then unlock it, the timer works fine.
BUT when compiled to apk and installed on my phone, same prob as always…of course screen doesnt fade to lock BUT if i lock with phisical key,wait a while and then unlock it, have wrong values as always.

i also tryed to manage battery use from my phone,in settings/battery i can manual manage app to work in background…(i use this optionfor same gps app that seem to loose gps after a while,ie Wikiloc)…
but no result!
I suppose the phisical lock ovveride everything,so having my screen on is pointless
no idea why different behavior from phone connected to pc and phone no connected…maybe there is something to do with battery save option but i cant figure how.
Any other suggestion will be appreciate :slight_smile:
-sorry for my poor english anyway-
cheers
***EDIT after some other test
the discriminating is whenever the phone is or not connected to usb recharger,regarding apk or usb emulation(actually works not for the emulated but for the usb connection!!)
so the problems is my phone,whom phisical key to lockscreen acts different if in charge or not…
and now…how to resolve?? :slight_smile:
thank you

the only solution is to not lock the device
Taifun

1 Like

... :frowning:
or to connect a powerbank to the phone..! (it works,really!)

i quote from Power management restrictions  |  App quality  |  Android Developers
" As described in Power management, the system can impose power restrictions on apps for a number of reasons. The following table outlines the current restrictions. These restrictions do not apply while the device is charging."

So i wonder..since im doing my own app for myself only (as maybe lot of appinventor2 users do) and i dont want to publish or distribute my app..there is no way to "trick" the android core to let it thinks it's on charge?

Why? the com.android.deskclock dont' accept parameters? maybe another third part timer would do...
thanks

Here is what App Inventor 2 can do with intents:
http://ai2.appinventor.mit.edu/reference/other/activitystarter.html

Google imposes constraints as to what developers can do to modify certain Android behavior. I expect the Android deskclock widget can not be modified and or if it can, it cannot be used to revive a ‘sleeping’ App Inventor app, especially if the device is manually LOCKED. Remember, App Inventor is not running as a Service (in the background). You possibly can experiment using the Android Alarm information using an intent to control it. How? I do not know. When, if you find a solution to ‘Why? the com.android.deskclock dont’ accept parameters’ , please share it with the community.

Here are some resources to help you learn to use the AI2 tools that might help you figure out a solution. A very good way to learn App Inventor is to read the free Inventor’s Manual here in the AI2 free online eBook http://www.appinventor.org/book2 … the links are at the bottom of the Web page. The book ‘teaches’ users how to program with AI2 blocks.

There is a free programming course here http://www.appinventor.org/content/CourseInABox/Intro and the aia files for the projects in the book are here: http://www.appinventor.org/bookFiles

How to do a lot of basic things with App Inventor are described here: http://www.appinventor.org/content/howDoYou/eventHandling .

Also look here http://kio4.com/appinventor/index.htm and here http://www.imagnity.com/tutorial-index/ for more tutorials.

To do what you need to do to make your app run in the background, build your app with a professional compiler that can create a Service app; that means use something other than App Inventor like the professional Android Studio and B4A compilers.

Good luck.

this is what the alarm extension is providing

but as far as I can see, this wil not help with your problem

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

I checked your alarm extension and it works for me...i mean...now i have an app that do some math to set the alarmclock right 10 minutes after the current hour...not a timer indeed,but do the job.
i have just some question,if u mind..
1-i suppose alarm works only with Hours and Minutes..no way to set the seconds?
2-every time i use the alarmextension,it creates a alarm in the alarms list of the phone..no way to delete them from the app?
3-is it possible to set the duration of alarm,how many times to snooze etc as from the alam options of the phone?
3-is there a way to detect the alarm snooze/abort and come back to he app with events?
Thnks

the API does not offer to set seconds, see also AlarmClock  |  Android Developers

see Q3 here App Inventor Extensions: Alarm | Pura Vida Apps

yes, to set the snooze duration would be possible, see also AlarmClock  |  Android Developers

you could do it using the activity starter or I can add it as parameter into the extension in case you like to sponsor that feature

I don't know if I understand the question, but probably this is not possible?

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

According to this:

"ACTION_SET_TIMER
Added in API level 19
public static final String ACTION_SET_TIMER

Activity Action: Set a timer.

Activates an existing timer or creates a new one.
This action requests a timer to be started for a specific length of time. If no length is specified, the implementation should start an activity that is capable of setting a timer (EXTRA_SKIP_UI is ignored in this case). If a length is specified, and EXTRA_SKIP_UI is true, the implementation should remove this timer after it has been dismissed. If an identical, unused timer exists matching both parameters, an implementation may re-use it instead of creating a new one (in this case, the timer should not be removed after dismissal). This action always starts the timer.
Request parameters

EXTRA_LENGTH (optional): The length of the timer being set.
EXTRA_MESSAGE (optional): A custom message for the timer.
EXTRA_SKIP_UI (optional): Whether or not to display an activity for setting this timer. 

Constant Value: "android.intent.action.SET_TIMER"
"
i used the activitystarter.action with "android.intent.action.SET_TIMER"
now i dont know how to use activitystarter.EXTRAS to set the seconds..i dont understand the syntax.
i post the block image..can anyone fill the correct arguments in the text?

AS_Extras

and the Designer..how to fill??

Of course it works thanks to Taifun for his alarm1 extension to override the permissions!!!

as already said, the API does not provide a possibility to set the seconds
Taifun

** ACTION_SET_ALARM does not provide a possibility to set the seconds

** ACTION_SET_TIMER it does..why not??
"ACTION_SET_TIMER
Added in API level 19
public static final String ACTION_SET_TIMER
Activity Action: Set a timer.
Activates an existing timer or creates a new one.
This action requests a timer to be started for a specific length of time.

Use the Alarm Clock to create a timer using:

Intent setTimer = new Intent(AlarmClock.ACTION_SET_TIMER);
setTimer.putExtra(AlarmClock.EXTRA_LENGTH, 60);
setTimer.putExtra(AlarmClock.EXTRA_MESSAGE, "My Amazing timer");
setTimer.putExtra(AlarmClock.EXTRA_SKIP_UI, false);
startActivity(setTimer);

I just need to put this into appinventor2 ActivityStarter component!!
what am i missing?? im confused..

Set Extras like this:

http://ai2.appinventor.mit.edu/reference/other/activitystarter.html

According to the documentation:
https://developer.android.com/reference/android/provider/AlarmClock#EXTRA_LENGTH

The blocks should look like this:

but it doesn’t call the timer

AlarmClock documentation indicates Applications that wish to receive the ACTION_SET_ALARM and ACTION_SET_TIMER Intents should create an activity to handle the Intent that requires the permission com.android.alarm.permission.SET_ALARM. The AS may not work unless the Manifiest sets the permission. I don’t know.

@SteveJG

Yes, but bringing in Taifun’s Alarm extension resolves the SET_ALARM permissions requirement.

I can get the timer to open with activityStarter, but not with any of the extras on board

ok, you like to set a timer and not an alarm...
you probably need to be able to set the time as long value...
this extension is able to do it
Activity Starter Extension by pepemont, which additionally is able to pass boolean and long values

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Tried that, same result a with normal activity Starter:
(variable skip is text block = false)
No error message generated by activity Starter

Calling the activity without the extras opens the clock

Timai2, you got the point...

and obviously including the Taifun alarm extension to bypass the permission settings,
It should work..!!!

the problem is how to pass the seconds (and/or other parameters) via extras syntax !!

btw,how did you configured the ActivityStarter properties in the Designer?? mine are all empty..maybe this is the problem...

I tryed this:
AS_2

and i have this error:

Tips?