Is there a way to view all intents that are generated by the Android OS's at any moment but maybe filtered by the activity ? Specifically I am testing the onHoverListener - I want to know if my activity is throwing away the hover motionevent or whether none is being generated (system not capable/ some other problem)
Ideally I would like a log of all intents given to my activity - but some other trick is also fine.
Android intents are , capable for starting a new activity , service , complete any action i.e send email , click photo , fetch data from a content provider etc .
Capturing any intent of that kind , you need to have intent filters registered to your activity with the same actions , as that of system intents .[The framework will pick your actvity/app if the same intent is fired and hence you may be able to intercept those intents if the user prefers to] This may be little too much as there will be so many of the actions declared for different android components .But some popular examples are sending sms , picking a contact , send mail .
Please refer to this for more info :http://developer.android.com/guide/components/intents-filters.html
Events and Intents are different all together , all events generated by your app will go the event handler queue of the UI thread .
So in order to intercept you should set appropriate event listeners to your activity components .
Try using onTouch instead .
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}`
Related
I have an intent that when the user touches anywhere on the splashscreen activity they're directed to the gallery activity, this seemingly works fine for the most part and when running the app does as is expected until the back button is pressed. When pressed multiple presses are required to return to the splashscreen.
Using logcat I was able to find out that the intent is being run multiple times however I'm unable to understand why, here's the method for the intent.
private void FullScreenOnTouchEvent() {
LinearLayout layout = (LinearLayout) findViewById(R.id.activity_splashscreen_layout);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
startActivity(new Intent(Splashscreen.this, Gallery.class));
Log.d("Splashscreen", "splashscreen executed");
return true;
}
});
}
I placed a check when the intent was executed and the new activity (gallery) had opened, the messages displayed are "splashscreen executed" and "gallery executed" respectively.
Here's a copy of the logcat.
splashscreen executed /
Gallery has executed /
splashscreen executed /
Gallery has executed /
I'm unable to see why this is ocurring and i'm at a bit of a deadend research wise, any help would be much appreciated.
Thank you,
Damon.
onTouch() is being called multiple times since it recognizes touch inputs of many kinds (DOWN, UP, MOVE...). You can fix this by an example written HERE or change layout listener to the OnClickListener which would be more simple to implement (less code).
I am making an app in which if power key is pressed twice,The app will perform some task.Please provide me some help.I have made it but its not working.
I am including my class files here,Please correct me if I am going into wrong direction.
Broadcaster
Listener
I don't know how to insert code here it is giving some error so I included links here.I am sorry for that.Please help me.
Android recommends avoiding double clicks and using the long-click besides the normal click.
For the long click use this :
#Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_POWER) {
// Do something here...
return true;
}
return super.onKeyLongPress(keyCode, event);
}
You also need to add this to the manifest
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
If this doesnt work properly onKeyDown
But I have never actually used this.
My personal opinion though is that you shouldn't hook the power key if you are planning to publish your app because unless it is doing something really an app shouldn't prevent the user from closing the screen
Solved the problem by receiving broadcast on ACTION_SCREEN_ON/OFF.
When the power button is pressed screen turns on/off depending on the fact whether screen is on or off at the time when pressing the power button.
I used this facility and put the logic in code such that if the user presses power button more than 5 times an sms will be sent to the trusted contacts.
I would like to disable the side volume buttons so the only way to control the volume will be from a dedicated activity inside my android app.
I managed to disable it for all my activities by adding the following code:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.d(TAG, "onKeyDown = " + keyCode);
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
return super.onKeyDown(keyCode, event);
}
But I don't know how to disable it for the activities i start from my app (for example I start the gallery app)!
I know it is possible as 'Volume Locker' app doing similar stuff.
It is not possible within public APIs to suppress the key events outside of your own Activities, if there is an app that has managed to do it what they are doing would be considered malicious by the platform designers and will get fixed at some point.
Based on the description given for that app (note: I've never used it personally)
Prevent accidental changes to your volume settings, install Volume Locker today.
This app helps prevent against accidental volume changes by confirming the change you made, by either tray notification or a pop up. If you don't approve the change, the volume will be reset within a set amount of seconds... By setting the timeout to "instant", the locked volumes will revert instantly without prompting.
I suspect what that is actually doing is listening for the volume buttons using a similar technique to the one in this answer and just reverting whatever change was made instantly(ish). That would make it seem to the user like the key press did nothing but in reality what happened is the volume changed and then quickly changed back.
I would like to know when the user pressed the home button while he was running my app.
BUT:
The problem is that I don't want to edite the existing code.
meaning I don't want to add logic to the existing activities onPause() method.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
HOME: Starting: Intent { act=android.intent.action.MAIN
cat=[android.intent.category.HOME]
Is there any other way to do it or is it really impossible?
The optimal solution would have been to catch an intent in the Manifest.xml, like:
action android:name="android.intent.category.HOME"
and implement a new class to catch it. (But it doesn't seem to catch it).
I would like to know when the user pressed the home button while he was running my app.
What specifically are you trying to achieve?
BUT: The problem is that I don't want to edite the existing code.
By definition, that is impossible.
The only solution I found was to add a service to the application which listens to the Log detecting if there was an intent to run the
That is modifying the code, violating your own requirement. Moreover, it requires a permission that really you should not be asking for.
Is there any other way to do it or is it really impossible?
That depends on what specifically you are trying to achieve.
The optimal solution would have been to catch an intent in the Manifest.xml...and implement a new class to catch it. (But it doesn't seem to catch it).
HOME is a category. It is not an action. Home screens are activities that respond to the MAIN action in the HOME category. However, it is modifying the code, violating your own requirement.
You can use key press event handler
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Home key pressed
} else {
return super.onKeyDown(keyCode, event);
}
return false;
}
I'm trying to implement an input method service that receives intents sent by a remote client, and in response to those sends an appropriate KeyEvent.
I'm using in the input method service this method
private void keyDownUp(int keyEventCode)
{
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
getCurrentInputConnection().sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
}
to send KeyEvents as in the Simple Sofykeyboard Sample, and it works in the home, in Activities... but it doesn't works when a Dialog or the menu of a Spinner is in foreground.
The events is sent to the parent activity behind the Dialog.
Is there any way to send keys and control the device like using the hardware keys from an input method?
Better explanation on what I'm trying to do:
I am kind of writng an Input Method that allows to control the device from remote.
I write in a client (a java application on my desktop pc) a command (for example "UP"), a server on the device with sendBroadcast() sends the intent with the information, and a receiver in the input method gets it and call keyDownUp with the keycode of the DPAD_UP key.
It generally works, but when I go to an app that shows a dialog, the keyDownUp method don't sends the key event to the dialog, for example for select the yes or not buttons, but it keeps to control the activty behind the Dialog.
Here I have found someone with my same problem... but no answer...
First, let me explain what I did understand.
You go to an app
You open a Dialog in that activity (For example go to sms app, long press a thread)
You press the HOME key.
Go to a different application that sends the intent
The IME gets the intent, and writes something down.
I don't understand how the activity with the Dialog gets opened again.
AFAIK, when you press a key in the softKeyboard, IME's onKey(int primaryCode, int[] keyCodes) is called and when you press a hard key the IME is called at:
public boolean onKeyUp(int keyCode, KeyEvent event)
public boolean onKeyDown(int keyCode, KeyEvent event)
You can try calling that methods instead, but I don't know if emulating a hard key would fix it. I guess it's a focus issue. Did you tried getting the text from getCurrentInputConnection() to see where it is standing?