I'd like to implement an alarm clock/reminder function in a Lwuit application, where the user may set an alarm time, and at that time a reminder dialog pops up with an alarm sound. However, I'm unsure about the approach. So help on any of the below questions would be greatly appreciated:
How to keep a timer running constantly, no matter what Form is displayed, e.g. without interfering with lwuit and EDT?
What's the right way to implement the trigger mechanism that will make the reminder Dialog box pop up? This may happen at any time, no matter what Form is currently displayed and shouldn't interfere with the ongoing user operation, so that when the reminder dialog is closed the user can continue whereever he was when the reminder popped up.
And finally, when the application is closed, or not running in the foreground, how can I keep a timer running? With the new and exiting CodenameOne coming up, I'd obviously like the solution to work on both J2ME, iOS and Android.
Sorry, if the answers are obvious, but I haven't been able to find a solid working solution.
1) You can use java.util.Timer for a generic timer, its callbacks will occur off the EDT so in order to use LWUIT you would just need to callback into the EDT using Display.getInstance().callSerially() and have your code within the Runnable callback.
2) In order to not block input and display in an unobtrusive way you can use the glasspane to just show a translucent element on top of the screen. It will not block input in any way. There is a sample of that in the LWUIT Chat demo application.
3) You can't. Background process execution isn't really supported on any device, all devices have a special "use case based" solution for background execution which requires you to write your applications in a very specific way. Codename One will add support for push notification which will allow you to push an alarm from your server. Seams counter intuitive at first but if you consider the amount of battery life that will be taken up by background processes it becomes pretty obvious why this was done.
Related
I am building a word game and I want to hide the board when application is suspended?
the code looks fine however it givs a strange behaviour!!,
when I suspend the app nothing will happen but when i resume the application then the board will hide!!
local onSystem = function( event )
if event.type == "applicationSuspend" then
print("suspend")
board_group.alpha = 0
end
end
Runtime:addEventListener( "system", onSystem )
Note: you might wonder how do I know how the application looks when suspended?
the answer is: by pressing the home button twice.
example
SpellTower in normal state
https://dzwonsemrish7.cloudfront.net/items/430k0c0b0y0b413d0b42/Image%202012.11.12%208:08:24%20AM.png?v=4822f549
SpellTower after pressing the home button twice
https://dzwonsemrish7.cloudfront.net/items/280a1y0r2U3W321y1B2z/Image%202012.11.12%208:08:31%20AM.png?v=09c37567
you can see how they are hiding the letters, this is exactly what I want to do for my game, the only difference is i am using Corona SDK
When you do board_group.alpha = 0 you only has set a variable, the result will only take effect after a screen update.
But since the application is suspended... it won't update! So, changing any graphics on applicationSuspend don't work.
I believe the reason is because the application is not considered as suspended. In normal objective c programming it means that applicationWillResignActive is called when the user double clicks on the home button. So what you want to do is to add that code for this part.
Here is a flow of events:
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Corona seems to have these events:
"applicationStart" occurs when the application is launched and all code
in main.lua is executed.
"applicationExit" occurs when the user quits the application.
"applicationSuspend" occurs when the device needs to suspend the application such as during a phone call or if the phone goes to sleep
from inactivity. In the simulator, this corresponds to the simulator
running in the background. During suspension, no events (not even
enterFrame events) are sent to the application while suspended, so if
you have code that depends on time, you should account for the time
lost to an application being suspended.
"applicationResume" occurs when the application resumes after a suspend. On the phone, this occurs if the application was suspended
because of a phone call. On the simulator, this occurs when the simulator was in the background and now is the foreground application.
So my guess is that you have to implement it outside of the corona API.
According to the corona documents you can implement them in the delegate:
You can intercept UIApplicationDelegate events via your implementation
of the CoronaDelegate protocol.
This protocol conforms to the UIApplicationDelegate protocol. Corona's
internal delegate will call your protocol's method if it is
implemented.
Please keep in mind the following:
Methods that Apple has deprecated will be ignored.
In most cases, your class' version will be invoked after Corona's corresponding version of the UIApplicationDelegate method. There is one situation in which your version will be called before.
In situations where the app is about to suspend or go to the background, your method will be called before Corona's version, e.g.
applicationWillResignActive: and applicationDidEnterBackground:.
http://docs.coronalabs.com/native/enterprise/ios/CoronaDelegate.html
But this is just a guess. Hope it helps!
Edit:
I was thinking, something really simple you could do is catch it outside and present a "pause" screen, then just hide it when the application enters foreground.
So if you can't do that (for now), one other option is to save application state when the application is about to terminate, and then set UIApplicationExitsOnSuspend = true in your plist file. This will cause the application to exit instead of suspending, which will avoid any screenshots, effectively "hiding" the board, etc. The downfall is, the app will have to read the session state when it launches again... this is only useful if your application can be designed to actually exit without losing your state, and is quite honestly, a little extreme. That said, it may be the only way to effectively do what you're trying to do.
Other ideas would be to see if you can add a large black layer to the screen, even though the application is suspending; perhaps this will somehow trigger an internal screen update by natively setting setNeedsDisplay. Also, instead of modifying the alpha, you might consider temporarily removing all of your layers and see if that has a similar effect.
I am doing the iOS timer application, but when I press the home button the timer will stop.
I would like to know if close the application, the program can keep working?
And go back the application can keep running.
I see some article How to write a background service in iphone application? said can't run in background.
Can some one help me?
Thank you.
If it's a timer you are doing, you can always keep the time when it started, through CACurrentMediaTime(), and if your application goes in the background, when it comes to the foreground again, in applicationDidBecomeActive:(UIApplication *)application, take another CACurrentMediaTime() poll, and display the difference between the numbers.
I believe this is how the Apple's bundled app works. No need to keep processing that number if the application is not used.
Sorry, I am new to iPhone development and my google searches have failed me, so I have had to resort to posting a potentially idiotic question on SO.
I would like to write an app that, when suspended, performs an action when the user presses the home or power button (presumably to unlock the phone). I read the section in the iOS App Programming Guide's App States and Multitasking and the "Processing Queued Notifications at Wakeup Time" discusses handling queued events upon waking up. However, this isn't what I'm after.
I'd like to know if it is possible to:
From the phone sleeping state (I couldn't find a document for phone states, so I am talking about the case where the user presses the power button to turn off the screen), I would like my app to be ready to respond to the event where the user presses the power or home button (to unlock the phone)
I would like my app to respond to the event that occurs when the user unlocks the phone. I found an answer here that's close.
I don't want any funkiness when calls come in, get ignored, etc. :)
From what I can tell, it's a very gray area in the API around locking and unlocking, and I'd like to verify whether or not I'm wasting my time trying to do this.
It looks like I can use the accelerometer to detect when the phone is locked, but I also assume that I won't be able to count on this behavior in all future versions of iOS.
EDIT - I think I can handle the locking and unlocking requirements by assuming that the application has to be running at the time the phone is locked and unlocked, but I still cannot figure out if it is possible to determine #1 above, which is that the power button has been pressed and the unlock screen is displayed. Likewise, I'd like to know if the power button has been pressed again and is no longer displayed (i.e. screen is off).
All of the behaviors for your app being suspended/backgrounded are in the document you referenced. The AppDelegate will receive these messages and pass them on to whatever view you want to listen for them.
-applicationWillTerminate
-applicationWillEnterBackground
-applicationDidBecomeActive
-applicationWillResignActive
You can set up a notification observer in your view if you would like the view to be notified of any of these events. Then just override them or set up a custom method to do any work that needs to be done.
While the Apple docs might be a little foggy at first, all the information is in there to let you know which state your app will go to and how it will be handled. It is up to you to figure out what your app needs to do for each of these events. Hope this helps.
Today I went to job interview, I encountered three questions now.they hope i provide some Solutions or
Thinking.
1.when user press Home button,app don't go back the main interface or delay 5 seconds to go back the
main interface.
2. when the screen is black ,how to auto activation screen(how to catch power button press event) .
3.how direct launch my app when iPhone is start-up
My english is very bad, i hope everyone can understand what i said above.Thank you very much
None of these are possible on an iOS device, unless it's jailbroken.
Q1 and Q2: not possible
Q3: The closest you can get to desired behaviour is kiosk (store demo) mode of operation.
See more here:
Lock-down iPhone/iPod/iPad so it can only run one app
It's not exactly what you're looking for - it limits the device to use only certain app - but to my knowledge the only way to auto-start an app without jailbraking the device.
I know that this question is old, but there are easy work arounds for every question on here that work for at least iOS 7+, although there is no way to do question 2 without using private APIs. You can successfully answer questions 1 and 3 with public API answers (although they are admittedly hacky)!!
1. When user presses home button, how do you delay 5 seconds before returning to the main screen?
Oscar Gomez's answer was spot on. While you cannot delay the UI from returning to the home screen without blocking the core run loop (which will get you rejected from the app store), you can use some background process techniques if needed to get your extra 5 seconds, just not with UI.
2. How do you catch the Power Button presses?
This is for sure using a private API, and while you can PROBABLY get into the App Store, you probably won't last long once Apple gets wind of you doing this. It is also a hack. You cannot listen directly to the power button, but you CAN listen to the screen going on or off, or both at once. Here is a small code snippet that I have in an Enterprise App:
Inside UIAppDelegate
static int const DisplayOnOffObserver = 54321876;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, &PLDisplayOnOff, CFSTR("com.apple.springboard.hasBlankedScreen"), NULL, 0);
...
return YES;
}
static void PLDisplayOnOff(CFNotificationCenterRef center, void* observer, CFStringRef name, const void* object, CFDictionaryRef userInfo) {
...
DO SOME MAGIC
...
}
- (void)applicationWillTerminate:(UIApplication *)application {
...
CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), &DisplayOnOffObserver);
...
}
Obviously you have to have the application ALWAYS running in the background (which can get tricky) or you have to only care to do it within the time that you ask for to run in the background (which isn't hard)
First step is listening to the darwin notification for the screen being toggled on/off.
Second step is implementing the callback.
Third step is stopping listening for the call back.
NOTE: DisplayOnOffObserver is a random number (not very random in my case)
3. How can you directly launch your app when the iPhone starts up?
Assuming that by directly launch, you mean launch in the background, there is actually an Apple supported way of doing this. You first have to enable Background Services for location. Second, you have to start listening for significant location changes. I don't know how well this is documented, but as soon as the iPhone boots, it tries to get it's location. When it does this, it goes from not having a location to having one (which is a significant change). Your app will launch in the background which includes calling the application:didFinishLaunchingWithOptions: method, where you can ask for more time and start other processes to permanently run in the background until the user kills your application manually. If you do permanently run in the background using this method, you do stand a chance of being rejected by Apple.
Hope any of this helps someone! If anyone needs more information, just leave a comment and I will update my answer.
When user press Home button,app don't go back the main interface or delay 5 seconds to go back the
You can't, unless you don't want your app in the appstore... The only thing you CAN do is ask for more time to save your data before your application is terminated, but the UI experience will still be the same - the iphone will go back to the main interface.
when the screen is black ,how to auto activation screen(how to catch power button press event) .
Not possible.
3. How direct launch my app when iPhone is start-up
I don't think you can do this even, with private APIs, and of course with your app not accepted in the appstore.
I have a clock program that needs to stay alive all night. I use the idletimer disabled function, but getting a text, missed call, or other notification overrides the idletimer setting back to enabled and allows the phone to sleep. I am thinking that I either need to disable the popups somehow while my app is running, or do something else to keep the phone awake (and running my app) through the popups.
Ideas?
Developing with Iphone 3GS and 3.1.2
How about a timer that periodically disables the idletimer?
You can't do this, at least not with public APIs for App Store inclusion. An incoming phone call will terminate you app and you cannot prevent this.
If you aren't worried about App Store rules, then write a daemon and run your app as a background task.
Although a good idea, the timer does not work. As long as the text message popup is not dismissed manually the phone will go to sleep no matter what idleTimerDisabled state is. Does this make sense, and is there a way to prevent sleep even when a popup is on the screen.