Android apps : Get button event in background

Issue

I create an Android app for a device without screen (not a phone). At startup my apps is launched and works correctly.

I try to catch the physical buttons event (down/up). It works but only one time when my apps starts. Then the events are not fire.

I have an idea : the apps is in the background and doesn’t receive the events.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

      Context context  = getApplicationContext();


      //Start a service
      Intent intent = new Intent(context,MainService.class);
      context.startService(intent);

      //finish();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        //Send event to server
        return true;
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        //Send event to server
        return true;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        //Send event to server
        return true;
    }

}

Do you have an idea ? I suppose I am not the first person with this problem …

Regards,

Solution

Found the solution: a second app was catching and blocking? the events. I have removed the apps and it’s ok now.

Answered By – Jonathan Le Cornichone

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published