Long Press on Home

Search This thread

geekasso

Senior Member
Jan 4, 2011
274
126
USA
Samsung Galaxy S22 Ultra
Hello, I have never done this for Android, though I do develop web apps. I'd like to give it a shot with the Android OS and my first project would be to modify behavior of long pressing a Home Button on our Note II's. I found some information on the web by using a function to hook into the KeyLongPress and Key ID.

What I would like to know, if where do I begin to start adding my custom functions? Do I create a separate file or put the function in an existing file?
How do I make this apply only during the lockscreen or home screen?
Using the example below, I imagine I need to create a folder called com.example.demo, but not sure where and what else would be needed.

Code:
package com.example.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;

public class TestVolumeActivity extends Activity {
    boolean flag = false;

    boolean flag2 = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_splash_screen, menu);
        return true;
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            Log.d("Test", "Long press!");
            flag = false;
            flag2 = true;
            return true;
        }
        return super.onKeyLongPress(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
            event.startTracking();
            if (flag2 == true) {
                flag = false;
            } else {
                flag = true;
                flag2 = false;
            }

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {

            event.startTracking();
            if (flag) {
                Log.d("Test", "Short");
            }
            flag = true;
            flag2 = false;
            return true;
        }

        return super.onKeyUp(keyCode, event);
    }
}


Any help is appreciated.
 

klin1344

Senior Member
Nov 11, 2011
3,486
5,611
It would be helpful to take a look at CM code and how the long press hardware key actions, as well as lock screen button actions are implemented.

Sent from my SGH-T999 using Tapatalk 2
 
  • Like
Reactions: geekasso

Ateisti

Member
Aug 25, 2010
36
19
Not sure if I understood you correctly, but you can't just put the code in a text file like a PHP script and expect it to work :) You need to compile it first using the Android SDK.

In any case, you can't hook keys globally the way you are suggesting. You'd need to modify the system's framework files (inside android.policy.jar) for it to work, which frankly sounds to be beyond your capabilities at the moment.
 

elesbb

Senior Member
Jun 20, 2010
7,883
5,324
Are you talking about in your own app or for your ROM?

Sent from my SGH-T999

"So I put my phone into airplane mode and threw it... worst transformer ever. -.-" -My friend
 

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    It would be helpful to take a look at CM code and how the long press hardware key actions, as well as lock screen button actions are implemented.

    Sent from my SGH-T999 using Tapatalk 2