[Q] Listener problem - Java for Android App Development

Hi,
I want my app to start, after the back button was pressed 3x.
So I got this class which is executed in the Launcher.
Code:
public class KeyManager extends Activity {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(KeyEvent == "Back key"){
Log.e("KM", "PRESSED!!!!!"+keyCode);
}
//return super.onKeyDown(keyCode, event);
return false;
}
}
This code works, if it s implemented in the launcher class, but it does not work, if the app is in the background. How or which key listener do I need for listening to background keys or more if the app is working only in the background?

Of course it doesn't work when being background. These events aren't sent to the activitiy if it's not in the foreground, and as far as I know even services can't react to them.

Well, ok so that is the problem. But I think it should be possible to solve that as some apps also react if for example the browser is being opened etc. Also, Samsung user might know, by clicking 2x the home button the app S Voice opens. And it should not be a system app, but just a usual one.
Maybe something like a global key listener is available? Unfortunately I couldnt find anything referring to that, yet.

Deyaz said:
Well, ok so that is the problem. But I think it should be possible to solve that as some apps also react if for example the browser is being opened etc. Also, Samsung user might know, by clicking 2x the home button the app S Voice opens. And it should not be a system app, but just a usual one.
Maybe something like a global key listener is available? Unfortunately I couldnt find anything referring to that, yet.
Click to expand...
Click to collapse
The Samsung S voice example is a really bad example to use because that's just code implemented in TouchWiz's launcher, I'm guessing its a timer that listens for 2 presses of the home button within a set time, if 2 presses are registered in the given time-frame, S Voice is launched via intent, if not then the default action for the home button is taken.

Related

[Q] What is the proper way to close an activity/application?

I started to develope an application.
When I wanted to close the application I clicked on the home screen,
but the app is still playing in the background.
Than I tried to click on the "back" key, but nothing happend, so I've
created a key event listener for the 'back" key, and inside that
method I wrote finish();
Now my app is closing but I think it's still running in the
background, because when I go to "manage applications" I can still see
it in the "running" tab, and if I press it, I see I can click on the
"force close" to actually close the app.
What do I need to do in order to really close the app without leaving
it open (one way or another)?
Thanks, Yaniv
You does not need a Exit Button, because Android will close things up as needed.
Perhaps you should read this: click
Otherwise you can exit your app with System.exit(0) at the end of your
onDestroy() methode.
Yes, I do know that Android is closing unused apps, but I do want to put also an exit option.
And thanks, the System.exit(0) worked for me
How does that compare to having an Activity-extending class call finish() from the main UI thread?
Actually... now that I think about it... if you're going to call it, do you have to call finish() as the last thing you do before returning from onResume(), etc? Or is it literally a black void from which the calling thread is guaranteed to never return? If you look at the Javadocs, they merely say that calling it indicates that the Activity should be closed, with the result propagated back to the parent (if any).
I've gotten burned by Java enough times over the past 10 years to know it's dangerous to read more into Javadocs than they literally say, regardless of how commonsense and reasonable it might seem. Going by the Javadocs' literal wording, it would be perfectly reasonable for a call to finish() to simply mark the Activity as eligible for aggressive closure and recovery... then return from the call and have a trainwreck after it executes code in the following finally{} block, or between the point where you called finish() and the return from the end of the current method. It would be outrageous, make lots of developers mad, and cause huge problems... but technically not inconceivable... which is why traditionally, I end up following every call to finish() with throw new RuntimeException("WTF?!?");, just to be safe and guarantee that execution will never, ever continue beyond that point.

How to get Android to always open root Activity

Ok, so I have been struggling for a couple days with this, and I hope someone can help me.
I want Android to always run my splash screen activity, because I load a bunch of data from web services that I need throughout the rest of the app. This works fine with the flag android:clearTaskOnLaunch="true" set when you launch the app from the home screen.
Where it does not work so well is the long press of the home button and then selecting the app. In that case it starts the app in whatever random spot the user left it last. The problem is that if the app has been killed due to memory, it still shows in that recent apps list and when a user restarts the activity from the long press, all my data is gone so the app force closes.
So, my ugly workaround is to remove the app from the recently run list. I would much rather it showed up there but that it started my app the same way as if it was clicked on the home screen.
Anybody know how to do this? The ebay app pretty much does this, so I know it can be done.
-frank
Instead of reading your data in OnCreate, you could do it in OnResume. See the flow chart here:
http://developer.android.com/reference/android/app/Activity.html
as you can see, OnResume is always called whether the app is created initially, resumed from pause, or reloaded.
kaediil said:
Ok, so I have been struggling for a couple days with this, and I hope someone can help me.
I want Android to always run my splash screen activity, because I load a bunch of data from web services that I need throughout the rest of the app. This works fine with the flag android:clearTaskOnLaunch="true" set when you launch the app from the home screen.
Where it does not work so well is the long press of the home button and then selecting the app. In that case it starts the app in whatever random spot the user left it last. The problem is that if the app has been killed due to memory, it still shows in that recent apps list and when a user restarts the activity from the long press, all my data is gone so the app force closes.
So, my ugly workaround is to remove the app from the recently run list. I would much rather it showed up there but that it started my app the same way as if it was clicked on the home screen.
Anybody know how to do this? The ebay app pretty much does this, so I know it can be done.
-frank
Click to expand...
Click to collapse
In the onResume for the activity that is crashing, you could test your data to see if it's still there, if it's null you can run the code to repopulate it again.

[Q] Task not resuming to last activity when launched from home screen

My android application has a login screen that is launched as the main activity with intents as follows:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>​
Nothing else is out of the ordinary (that I am aware of, concerning the other activities, etc, no additional intents set on those or anything).
With the emulator, if I login and the second activity is launched, then I press the home button, if I launch the app again from a homescreen shortcut the task returns to the second activity. This is the desired behavior, as I don't want the user to have to login over and over again.
With both an HTC Incredible and a Droid X (only phones I have access to), if I login and the second activity launches, then press the home button, if I relaunch the app from a homescreen shortcut, it always launches the login activity, which is not the desired result. I want it to resume at the second activity where I left off.
I do have a task killer on the two phones I tested with, but they are set to not automatically kill any tasks and I don't believe this is happening because when I launch the app again for the second time and it shows the login screen, if I click "back" from there, it goes back to the activity I was hoping it would resume to, so the activity stack seems intact.
Does anyone have any insight as to why this works as intended with the emulator but not on the phones? Thank you very much.
I posted this on stackoverflow as well, but since I am new to this forum I could not include the link.
Note
I also just discovered that if I resume the app by holding the home screen button down and choosing the app from the recently used list that pops up, that it resumes to the last activity I was on properly. So it seems to just be from the app launcher shortcut or a home screen shortcut.
You can override that by setting android:alwaysRetainTaskState to True in the root activity.
http://developer.android.com/intl/fr/guide/topics/manifest/activity-element.html#always
This attribute must be on the root activity, so if your root activity is only the login activity and not the primary activity you want the user to return to, then you will have to do something a bit more complex. You will either need to make the login forum be a view on the primary activity, or make the login activity store the information and re-launch primary activity (not recommended).
You might also want to look at android:launchMode="SingleTask"
The above will solve the issue you are having, but the best solution by far is to instead use a long running service to handle the connection and login state, and make your activities just use and configure the service (login creates and sets up the service, primary activity acts as a proxy to the service commands, and logout shuts down the service). This will also prevent the login from being lost if memory gets low enough to shut down an activity, but not low enough to require a service shutdown.
I was able to get it working, thank you RoboPhred. The funny thing though, is nothing extra was required.
I think what was happening was that the old shortcut I had on my home screen was just that, an old shortcut. It was most likely botching something up. Once I uninstalled the app and created a new icon on the home screen, all is working fine.
Most likely this is why it was fine in the emulator as it fully installs the new apk.
So other devs be aware that old shortcuts may have some iffy intents depending on old builds etc and when in doubt fully uninstall the app and create new shortcuts.
I just wanted to thank you once again for at least replying with a potential fix, as it got me testing it again.
Thank you very much.

Toggling a image

I'm working on an app where there is a requirement to "mark" an item, specifically a question on a quiz app.
There is an image button on the top of the screen and when pressed, will put a "star" by the question. The user wants this to toggle, that is, every time the imagebutton is pressed, the star will appear or disappear.
Any "easy" way to do this? LOL Obviously I know how to show and hide it. But the code on the actual button press could be a pain. Not to mention, once set to View.VISIBLE, it's visible on ALL pages of the quiz.
My thought is to store the state of the question# and status of button in a database table: Y for on, N for off. Then query that status on every page. What I prefer would be something like this:
Code:
switch(V.getid()) {
case starButton: // button pressed!
if(star.VISIBLE) // it's there already, get rid of it
star.setVisibility(View.INVISIBLE);
else
star.setVisibility(View.VISIBLE); // not there, show it
}
There isn't a "star.VISIBLE" There is "star.setEnabled(true/false)", but that just "turns off" the button and grays it out a little. Any ideas? I want to keep this code short and sweet because I have to put it in onFling() too!
TIA,
Roots
if (star.getVisibility() == View.VISIBLE)
......
OMG...ok, I need to retire from programming ROFL So simple, so easy and yet I am so dumb to not see that solution.
Man, I was all around it wasn't I? LOL
Thanks. <hides under desk in shame>

[Q] Screen on - New Activity created

Hi!
I've found a strange Activity behavior while programming a game for Android (currently with API level 8)
Following Situation:
The Main Activity only consinsts of one View on which the Game Thread draws all the sprites and stuff. In the manifest I checked android:multiprocess="false" and android:launchMode="singleInstance" for the Main Activity, so that Android only creates one instance of this activity, so you always come back to your left game after pressing the home button and starting the app again. This works great, but if I turn off the screen WHILE in game, and the activity is active, and turning it back on, Android creates another instance of the game (the Main Activity), so it starts over again.
So my question is, what could I do to prevent this and have the same behavior as if switched back to Home Screen before turning off the screen?
(The testing device is a HTC Desire HD with Android Revolution HD 5.1.7, if this is necessary information for helpers )
I hope someone here can help me!
I think you will have to listen for the screen off intent and handle this situation very explicitly. Maybe make the pause screen come up and then when it turns back on have your activity check for instances of itself and then deal with it.
Do you use static activities?
From something awesome
Thanks for your answer
The ScreenOff/On Intents method would be easy, but how can I recover the old Activity in the new one?
The Activity should be static, cause of the manifest properties, or not?
Another idea: In the onPause() Method queck if it was Screen off (with the corresponding intent) and 'call' the home button
Is this possible? And how could I realize this?
There is a way to use adb to send the home key press. And you can run adb on the phone with getRuntime().exec("adb keyevent xx")
Where xx is the home button constant
From something awesome
Thanks for your help!
But I made it now a much easier way...
One single 'moveTaskToBack()' in the onPause and everything works perfect!
Nice. Its always best to assume that android will do nothing to manage your app and to just do everything very explicitly
From something awesome

Categories

Resources