Application Close Using Combination of Button - android

I have a requirement where I need to close my opened application using combination of keys.
Like when user will open my application and user will press twice combination of Power button and Option then application will close. Anyone can suggest me how it is possible or not?
Thanks .

You can setOnTouchListener() on each of the buttons that must be combined and use some boolean variables to control which buttons are pressed at a given time, then finish() your application when appropriate. See Touch Release method in Android for an example (which needs a little fix BTW: onTouch() must return true when the event has been consumed, false otherwise).
For the Power button you can override your activity's onKeyDown() and onKeyUp() methods. There is a key code for the Power button at http://developer.android.com/reference/android/view/KeyEvent.html

Related

Does the Back Button fail to call stop()?

My app currently has only one Form, which listens to the accelerometer sensor. In my start() method, I turn the listener on, and in the stop() method, I turn it off. I have verified that the listener turns off when I hit the Android's home button, but when I hit the back button, the application exits and the Android returns to the home screen, but the listener keeps going, which means the stop() method never got called. Is it my responsibility to handle the back button with code to call the stop() method? Or is this a bug in CodenameOne's framework? It seems to me that when the back button returns the user to the home screen, it should call stop() for me.
I am not sure about all the details of your issue, however you can resolve it by calling the setBackCommand on that one form.
yourForm.setBackCommand(
new Command("closing the sensor listener"){
#Override
public void actionPerformed(ActionEvent ev){
// your code to close the listener
}
}
);
I don't know about CodenameOne framework, but When app is visible and you press back button it calls all four methods in following order
1)onBackPressed()
2)onPause()
3)onStop()
4)onDestroy()
and when you press home button it calls only
1)onPause()
2)onStop()
methods
so, when you press back button onStop must be called.
You please put source code so that people can understand your problem clearly.
I don't know about CodenameOne framework, but I know the Android SDK.
Activity.onBackPressed() should be invoked when you use the back button. Just because your Activity is no longer visible does not mean it has been reaped, and this might explain why Activity.stop() is not invoked (immediately).
Depending upon your use case, Activity.onPause() might also work better.
HTH. Good luck w/your project.
This has been fixed. I've verified it, and it works correctly now.

Distinguish activity calling cases: from other activity/other package/by system

I'm making a simple e-book reader app, and an activity can be called by many cases.
I'd like to distinguish callee activity to know its origin action in case of
From my another activity: this can be easily solved by
StartActivityForResult from calling activity.
Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
Switched by user's multitasking choice.
Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
How to know above cases?
I have no idea why you would need to do this but...
1.From my another activity: this can be easily solved by StartActivityForResult from calling activity.
Yes, as long as the calling Activity is your own as you can't guarantee any 3rd-party code will use startActivityForResult(...). You can, however, use getCallingPackage() and getCallingActivity() in other cases.
2.Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
When the user presses the BACK button your Activity isn't being "called" - it's simply being resumed or re-started. The original calling app/Activity/method will still hold true - there is no way to ascertain that this has happened as the normal Activity life-cycle methods (onStart() and onResume()) are always called even when an Activity is first created.
3.Switched by user's multitasking choice.
If you mean using the "Recent" apps view, the same applies for my answer to 2. above.
4.Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
In this case onCreate() will be called although if your Activity is simply stopped for whatever reason, it may simply be restarted depending on the launch mode you use.
In short, I can't see you being able to gather much in the way of any accurate information as to how your Activity becomes active.
I am not too sure about the actual way for the above question as I am too a new guy in android.
But to the best of my knowledge... called by back button and switched by user's multitasking leads the activity to enter pause state.
So you can access it from "onPause" method in your activity.

How can I check programmatically that user has an interaction with their Android phone?

How can I check programmatically to see if the user has interacted with an Android Phone's touch screen or keypad? Interaction may be a touch or key press event. Will creating a service for this will help me or not ? How can I do this?
If you are talking about doing this in Activity.. then you can override onUserInteraction() method of the Activity. Following is the java doc of the method:
Called whenever a key, touch, or trackball event is dispatched to the
activity. Implement this method if you wish to know that the user has
interacted with the device in some way while your activity is running.
This callback and onUserLeaveHint() are intended to help activities
manage status bar notifications intelligently; specifically, for
helping activities determine the proper time to cancel a notfication.
All calls to your activity's onUserLeaveHint() callback will be
accompanied by calls to onUserInteraction(). This ensures that your
activity will be told of relevant user activity such as pulling down
the notification pane and touching an item there.
Note that this callback will be invoked for the touch down action that
begins a touch gesture, but may not be invoked for the touch-moved and
touch-up actions that follow.
there are so much methods for completing this purpose. You can override on touch event, onclick event on the View instance.

the onKeydown event is being handled by android, thought the methods are there to handle the event

I have a menu activity which is getting extended from another activity.
I have my onKeyUp/Down methods written in the parent activity.
When the menu is shown on via a touch event then there is no focus shown anywhere, and that is when if I do an event from the external hardware(the intention is to control the device through an external hardware). the onKeyDown method is being handled by android and not by the activity. This only happens for the first time when the focus is shown nowhere(no menu item highlighted).but after i do a keyevent again from the hardware, it comes in the onKeyUp/Down events but not for the forst time.
You're most probably returning super.onKeyDown/up on your overrides. Try returning true.

How to scan user activity in order to reset timer in Android

I'm facing one issue and I cannot find the solution.
My application has a PIN code and I want to un-verify it after 2 minutes without any action from user. For instance user verified the PIN and than leave the phone on the table (with running application) and I want to un-verify the pin after 2 minutes.
Question is how to scan the user activity (it means touch anywhere on the screen or buttons). Of course I can scan the touch on active components (buttons etc) but I also want to scan the touch anywhere on the screen to reset the timer.
Do you have any idea how to do it?
Thanks a lot!
you can override the activity's main layout's onTouchEvent to detect any touch gesture within the whole viewable area(which should fill the whole screen). just remember to return super.onTouchEvent to ensure that the other active components can still consume the event when they are touched separately.
Override activity method onUserInteraction()
#overide
public void onUserInteraction() {
//reset you timer here
super.onUserInteraction();
}

Categories

Resources