1. Download our Official Android App: Forums for Android!

Apps Identifying back button press

Discussion in 'Android Development' started by satellitemundo, Mar 13, 2011.

  1. satellitemundo

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    29
    Joined:
    Aug 18, 2010

    Aug 18, 2010
    29
    0
    15
    Hi all,

    Identifying a back button pressed event is veru simple. But I want to do it in different way.
    Say my application is installed on a device. Currently user is doing some stuff in Alarm Clock. But now when I press back button or any other button it should no back to the home screen or anywhere but it should open my application. WidgetLocker is the application which is doing the same thing. It uses power button to perform the task. Whenever you press power button from anywhere in the phone it throws to its own customized lock screen.

    I want to perform the similar task using some buttons. Can anyone guide me?
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    Then it sounds like you should use a Service to get key event, and start your app from that service.
     
  3. JamTheMan

    JamTheMan Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    136
    Joined:
    Dec 16, 2010

    Or maybe a Broadcast Receiver?
     
  4. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    If there is a broadcast for buttons, than that of course is the best way. Did not know there were any for buttons.
     
  5. JamTheMan

    JamTheMan Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    136
    Joined:
    Dec 16, 2010

    Was also only a wild guess from my side...

    Looks like there are no broadcast actions for specific buttons, only activity intents...

    Dunno what to try then, except from a Service listening in on the intents somehow. More on which intents to possibly use here:

    Intent | Android Developers
     
  6. satellitemundo

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    29
    Joined:
    Aug 18, 2010

    Aug 18, 2010
    29
    0
    15
    Yes, Services or Broadcast receiver is the only way.
    But there is no broadcast receiver for back button.

    Can any one suggest how to bind key events with services.I want to run particular service which will start on press of back button.

    What to do in this case. and what Intent I should use is the question.
     
  7. JamTheMan

    JamTheMan Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    136
    Joined:
    Dec 16, 2010

    For you service to be able to do anything as the back button is pressed, you'll need to have it running all the time. Only a broadcast receiver can "wake up" on an event like that.

    So you'll need the service running and listening in on the back button somehow. And then trigger whatever reaction you want...
     
  8. satellitemundo

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    29
    Joined:
    Aug 18, 2010

    Aug 18, 2010
    29
    0
    15
    Exactly.. But I think there is no broadcast receiver which can listen that Back Button is pressed.There are receivers for Power button, Camera Button,Search Button etc..

    How to do this with services I have no idea.
    Can anyone mention few steps to start the same with services.
    And first of all is this possible?
     
  9. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    Use the debug mode in eclipse with usb debug on your phone.
    If you touch the back button it will send an event and it should be visible in the debugger because it shows evrything the phone is doing.

    Hope this will work for you
     
  10. satellitemundo

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    29
    Joined:
    Aug 18, 2010

    Aug 18, 2010
    29
    0
    15
    Nice idea.But this we can do in emulator also.
    Anything more ?
     
  11. pietjuhhh1990

    pietjuhhh1990 Well-Known Member
    Rank:
    None
    Points:
    38
    Posts:
    235
    Joined:
    Feb 21, 2011

    Feb 21, 2011
    235
    22
    38
    App/web development
    Netherlands
    keyCode == KeyEvent.KEYCODE_BACK
     
  12. satellitemundo

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    29
    Joined:
    Aug 18, 2010

    Aug 18, 2010
    29
    0
    15
    Yea very true.
    But this should be in services. How to trigger a service when app is running in background or in close state.So I need to bind this key events with services.
    And the problem is there is no broadcast receiver android provides for back button.
     

Share This Page

Loading...