Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,733,899 Members 49,398 Now Online
XDA Developers Android and Mobile Development Forum

Long Press on Home

Tip us?
 
geekasso
Old
#1  
Senior Member - OP
Thanks Meter 119
Posts: 228
Join Date: Jan 2011
Location: Marlboro, NJ
Question Long Press on Home

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
Old
#2  
klin1344's Avatar
Recognized Contributor
Thanks Meter 5530
Posts: 3,467
Join Date: Nov 2011
Location: Cerritos, CA

 
DONATE TO ME
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
Nexus 5 - Android 4.4.2 KitKat

| [APP][FREE][4.0+]Cardify Flashcards |
Intuitive and simple user interface, yet powerful and feature-packed.

The Following User Says Thank You to klin1344 For This Useful Post: [ Click to Expand ]
 
Ateisti
Old
#3  
Member
Thanks Meter 19
Posts: 36
Join Date: 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.
 
elesbb
Old
#4  
elesbb's Avatar
Senior Member
Thanks Meter 4248
Posts: 6,652
Join Date: Jun 2010

 
DONATE TO ME
Default 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
Thread Tools
Display Modes