Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

Long Press on Home

OP geekasso

3rd January 2013, 04:41 PM   |  #1  
OP Senior Member
Marlboro, NJ
Thanks Meter: 120
 
230 posts
Join Date:Joined: Jan 2011
More
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.
4th January 2013, 05:48 AM   |  #2  
klin1344's Avatar
Recognized Contributor
Flag Cerritos, CA
Thanks Meter: 5,536
 
3,477 posts
Join Date:Joined: Nov 2011
Donate to Me
More
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
The Following User Says Thank You to klin1344 For This Useful Post: [ View ]
14th February 2013, 04:58 PM   |  #3  
Member
Thanks Meter: 19
 
36 posts
Join Date:Joined: Aug 2010
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.
16th February 2013, 06:27 PM   |  #4  
elesbb's Avatar
Senior Member
Thanks Meter: 4,475
 
6,924 posts
Join Date:Joined: Jun 2010
Donate to Me
More
Re: Long Press on Home
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
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes