Get Key Press Action - java

In some phone we have the option in which we can state if power button locks the screen or just switches off the back light. I was wondering if there's a way to get the keypress action because as far as my understanding goes, they are just changing the actions in power key press event.
Can anyone help? Thanks in advance!

Try this.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);//prevent phone from being locked
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_POWER)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
//do something
}
else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
//do something
}
}
return super.onKeyDown(keyCode, event);
}

Related

Back Button Webview Problem (Delayed work after few clicks)

I made a back button for my webview, but I got a problem.
When I click it more time (for example 5/6) it's back to the main page, but later button work like on delay, so I'm on some page and my app goes back. It's possible to limit the click of the button to only 1 click per page?
Thanks.
Button przycisk_powrot = (Button) findViewById(R.id.przycisk_powrot);
przycisk_powrot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
You can set the button state to enabled/disabled - and can make a check like this:
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
// Now check is it still can go back, ?
backButton.setEnabled(webView.canGoBack());
backButton.setAlpha((float)1.0);
}
else {
backButton.setEnabled(false);
// Also make the button little dim, so will show to user that it is currently in disabled state
backButton.setAlpha((float)0.5);
}
}
});
Enjoy..!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}

background button press detection

I am trying to build an android app which detects button pressed on a background and plays a sound or something. For demo, I am trying to add this method
public boolean onKeyDown(int keyCode, KeyEvent event) {
final MediaPlayer abc = MediaPlayer.create(this, R.raw.abc);
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
abc.start();
}
return super.onKeyDown(keyCode, event);
}
to this class
public class RSSPullService extends IntentService{
public RSSPullService(){
super("Nevermind me");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
}
}
Is there something that can be helpful? I am new to android development and Java.
In touch android mobile phones there is not any keyboard hardware. So please specify which button you want to get pressed?
Edited:
Add the following permission in Manifest fie:
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
and override following methods:
//To capture any short click of button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// this is method which detect press even of button
event.startTracking(); // Needed to track long presses
return true;
}
return super.onKeyDown(keyCode, event);
}
//To capture long press of power button
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Here we can detect long press of power button
return true;
}
return super.onKeyLongPress(keyCode, event);
}

What is the proper implementation of the back button press in same activity?

I want to detect the back button.
However my current implementation does not even detect the back button.
CODE:
#Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
//... OTHER CODE ...
else if(e.getAction() == MotionEvent.BUTTON_BACK){
System.out.println("BACK BUTTON PRESSED");
setCurrentState(new MenuState());
}
return true;
}
}
You can use onBackPressed() inside your Activity:
#Override
public void onBackPressed() {
//Do something
}
It's written in the documentation:
public static final int BUTTON_BACK
Button constant: Back button pressed (mouse back button). The system may send a KEYCODE_BACK key press to the application when this button is pressed.
You need to override onKeyUp function from the Activity (not from the View):
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//todo
}
}
Use Intent inside onBackPressed()like this:
#Override
public void onBackPressed()
{
Intent BackIntent = new Intent(getApplicationContext(), NewActivity.class);
startActivityForResult(BackIntent);
finish();
}

Is there any method to find out the display-changing events of android smartphone?

I'm a student of Yonsei graduate school, Korea.
I want to make a simple application that measures an time interval - between
touching the screen and display-updating.
I found that following method catch the touching event.
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN ){
...
Now I'm searching the Android APIs, but I coudn't find the method which catches
display-updating event. If you have any information about this problem,
please show mercy to me. Thank you.
Try adding a ViewTreeObserver.onDrawListener to a view in your Activity's content view. You can make a class that implements the that interface. When you get the touch event, call a method on your draw listener to record the time of the next draw event.
private MyDrawListener myDrawListener = new MyDrawListener();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
findViewById(...).getViewTreeObserver().addOnDrawListener(myDrawListener);
}
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
myDrawListener.recordNextDrawTime();
}
}
public static class MyDrawListener implements ViewTreeObserver.OnDrawListener {
private boolean recordNextDrawTime;
public void recordNextDrawTime() {
recordNextDrawTime = true;
}
#Override
public void onDraw() {
if (recordNextDrawTime) {
Log.d("MyDrawListener", "Draw time = " + System.currentTimeMillis());
recordNextDrawTime = false;
}
}
}

Button Down Event in Eclipse (Android)

I wanna use a button (not a key) just like backspace so when it is down do something repeatedly.
I've found proper code for hardware keys but as I mentioned I want a BUTTON do such things.
Thanks
You can set an OnTouchListener on a Button instance. You can then override the onTouch method of the of the listener to do what it is that you want until MotionEvent passed to the onTouch method has MotionEvent.getAction == MotionEvent.ACTION_UP. See this link for an example:
Android onTouch Listener event
A switch statement is sufficient, just customize it to fit your needs using what I said above. --hope this helps, Scott
Thanks Scott. Finally I found the answer and did the job.
public MyActivity extends Activity
{
private Handler mHandler = new Handler();
private Runnable mUpdateTask = new Runnable()
{
public void run()
{
Log.i("repeatBtn", "repeat click");
mHandler.postAtTime(this, SystemClock.uptimeMillis() + 100);
}
};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button repeatButton = (Button) findViewById(R.id.repeatButton);
repeatButton.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View view, MotionEvent motionevent)
{
int action = motionevent.getAction();
if (action == MotionEvent.ACTION_DOWN)
{
Log.i("repeatBtn", "MotionEvent.ACTION_DOWN");
mHandler.removeCallbacks(mUpdateTask);
mHandler.postAtTime(mUpdateTask, SystemClock.uptimeMillis() + 100);
}
else if (action == MotionEvent.ACTION_UP)
{
Log.i("repeatBtn", "MotionEvent.ACTION_UP");
mHandler.removeCallbacks(mUpdateTask);
}
return false;
}
});
}
}

Categories