When I test my android application on my phone, the application don't want to quit and make a bug on my phone. My little app take 70MB on my phone and still alive all the day...
How can I close it?
Do I have to put a Listner for the button "return" or there is some methods made from the SDK???
Thanks !
EDIT:
My application still runing, even if I press "HOME" ... this is not normal, is it?
Application on mobile aren't meant to quit, because it's against the UX of mobile user.
Have a look at this discussion. Android: Is quitting an application frowned upon?
you can override onKeyDown function like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
finish();
}
return super.onKeyDown(keyCode, event);
}
Related
I have an android application which kind of like a home screen of the build-in screen on android phone. I can list all the available applications and run them. One thing I don't know how to do is when user run an application from my apps, my application can trigger the hardware menu button to show the current application menus.
Is this possible ? If not, what about doing it if the device was rooted.
In activity use override onKeyDown like this :
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
doSomething();
return true;
}
return super.onKeyDown(keycode, e);
}
In order to open up the menu from other application apart from the caller application, the device have to be rooted before the code to work.
I am making an app in which if power key is pressed twice,The app will perform some task.Please provide me some help.I have made it but its not working.
I am including my class files here,Please correct me if I am going into wrong direction.
Broadcaster
Listener
I don't know how to insert code here it is giving some error so I included links here.I am sorry for that.Please help me.
Android recommends avoiding double clicks and using the long-click besides the normal click.
For the long click use this :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyLongPress(keyCode, event);
}
You also need to add this to the manifest
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
If this doesnt work properly onKeyDown
But I have never actually used this.
My personal opinion though is that you shouldn't hook the power key if you are planning to publish your app because unless it is doing something really an app shouldn't prevent the user from closing the screen
Solved the problem by receiving broadcast on ACTION_SCREEN_ON/OFF.
When the power button is pressed screen turns on/off depending on the fact whether screen is on or off at the time when pressing the power button.
I used this facility and put the logic in code such that if the user presses power button more than 5 times an sms will be sent to the trusted contacts.
I want to handle device home button click in my android application.When googled it is said that this
Link works.But I have some doubts.
Is it supported by all android versions?If not which of them are supported?
Is there any consequence exist because of using onAttachedToWindow() method?
Is there any way to handle Home button click(Except this)?
Thanks in Advance
This only works in previous version. But from os version 4.0 it is not working (although in my emulators this doesnt work after api level 11 but I am pointing ics according to many other links).
Found this on other posts.
On older Android version this is working. But Android changed this, because they say "Home Button should stay Home Button" and they don't want that anybody override the Home Button. And because of this reason your code is not working anymore.
If you want to do something when the home button is pressed, then do this in the onPause method.
yeah its work please try this code
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
};
There's a situation I have that's related to the Activity stack but I'm new enough to Android dev to not now how to respond to it.
I have an application that works like a stopwatch. When you start it, an ongoing notification goes in the notification tray and remains ongoing until you stop the stopwatch. I've noticed that if my stopwatch is running in the foreground, and I touch the notification, it creates a new instance of my stopwatch's activity and slides the old one off screen. This creates problems with the back button (you press back and the new instance goes away to reveal the old instance). It makes sense why this happens, but I don't want it to happen like this. What can I do to prevent multiple instances of my application from running?
What can I do to prevent multiple instances of my application from running?
In your Intent you use with the PendingIntent for the Notification, add setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); or setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, whichever behavior fits your needs better.
You could also disable the back button. Not as elegant as CommonsWare solution but nothing wrong with some options!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
return true;
}
return super.onKeyDown(keyCode, event);
}
I have a service running (Socket), this is how i start the service.
Intent s = new Intent(this, Socket.class);
startService(s);
in every activity i check for the user to select the home button, as soon as the home button is clicked i need to destroy the socket, so i have the below code on every activity in my app:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_HOME)
{
Intent s = new Intent(this, Socket.class);
stopService(s);
}
return true;
}
but this doesn't seem to stop my service?
Am i missing something? I need to destroy my service as soon as the home button is clicked.
Instead of hooking into keypress events and such, perhaps working with the built-in events like onPause, onStart, onDestroy, etc is more suited to your needs?
Another question on StackOVF recently had a brilliant reply with a flowchart that can help you figure out where to start/stop any other stuff you're using:
http://developer.android.com/images/activity_lifecycle.png
creds to monoceres for posting that in this topic:
App crashes after receving phone call