Is it possible to handle some finishing stuffs when user press the X button in DismissOverlayView? Like save some files, etc.
Alternatively, I would like to pause the app before the X button and resume if X is not selected.
I imagine it would call your Activity's onPause(), onStop() and onDestroy() methods, which is normally where you handle the things you mentioned such as saving data. You should take a look at Android's Activity's lifecycle, which also applies to Android Wear. In particular, here's what it says about onPause(): "Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc." Sounds like what you want.
This will get the event of selecting around the edge of the DismissOverlayView:
DismissOverlayView dismissOverlayView = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
dismissOverlayView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("Cancelled DismissOverlayView");
}
return false;
}
});
Related
I have a certain activity that begins when I tap my smart watch's screen. There is a timer and bunch of stuff that happens, but the process is crucial, so I am handling certain cases or things that might happen that would disturb the flow of things.
So basically, I want to prevent the home button of my watch to exit the app and go to the homescreen while my timer is running. I keep looking this up and most people say to override the onBackPressed method. But this was for the back button, and I I realized the button is a home button not a back button.
frameLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
clicked = clicked + 1;
if (clicked == 2)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
startTimer();
}
else if (clicked >= 4)
{
Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
AlertMessage();
}
}
return true;
}
});
this is the main method I use
Just override the onBackPressed function.
#Override
public void onBackPressed ()
{
//Control the flow
}
Use third part library in case if it solves your problem.
Here is the link:
https://github.com/shaobin0604/Android-HomeKey-Locker
The general consensus is that you can't override the home button behavior on a Wear OS device, just like you can't override the home button on an Android phone. This is by design to prevent developers from preventing the user to leave an application. Even if there is a hacky way to do it, this is not officially supported and may stop working at any time in future OS versions. I highly suggest not doing it since it goes against the basic navigation model of the device.
More details, and some workarounds for common use cases where people think they need to override the home button can be found in this blog post.
I would like to be able to detect if my Activity has been obscured by, say, a system alert or some other overlay (for example the power menu when I long press on the power button), or some malware that detects the launch of my Activity. I noticed that the foreground app in this case would still be my app, so I can't simply base it on what the foreground app is. I also notice that onPause() isn't called when my Activity is obscured, so I can't put any logic in onPause() either. Even if I can though, I would then have to differentiate between a system alert/overlay and the user pressing the back/home button.
Are there any other ways for me to accomplish this?
You can check if Activity, Fragment or View is Obscured.
For Activity you need override dispatchTouchEvent method and check if event has flag FLAG_WINDOW_IS_OBSCURED. There is example code:
public class OverlayTouchActivity extends Activity {
private boolean mObscuredTouch;
public boolean isObscuredTouch() {
return mObscuredTouch;
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
mObscuredTouch = (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0;
return super.dispatchTouchEvent(event);
}
}
This is a part of Android code, please check OverlayTouchActivity.java. In order to check if Fragment is obscured, execute the following piece of code in Fragment that belongs to the OverlayTouchActivity activity:
OverlayTouchActivity activity = (OverlayTouchActivity) getActivity();
if (activity.isObscuredTouch()) {
// Fragment is bbscured
}
Please see AppPermissionsFragment.java fragment (search for OverlayTouchActivity).
For View you should override onFilterTouchEventForSecurity method. For more information please see security section of View documentation.
You can use the PackageManager to query whose of the installed packages has suspect permissions like SYSTEM_ALERT_WINDOW, BIND_ACCESSIBILITY_SERVICE or BIND_DEVICE_ADMIN.
I am writing an very simple application with following scenario:
1) Screen A have 3 button to move on other screen's.
2) Now if I hold one button(say Button 1) and perform rapid click on other button then it launch multiple instance of other screen. Which I think should not be happened. How can prevent this.
3) and it's more weird. After move on other screen if I don't release Button 1 which was on Screen A then it still allow to perform click for rest of two button of screen A even I can see second screen.
Here it's clear launch second screen but still first screen button event working.
Any idea how can avoid such scenario.
How you are going to disable other buttons while having 1 enabled, that's an algorhytmic problem. You can try creating a boolean or control variable in your activity (and then pass the final reference of the activity to wherever you need it), or in a static context. But to answer the title of the question - you can "Cancel Touch Event" either by adding an OnTouchListener, or if you're extending class Button, you can override onTouchEvent(MotionEvent ev) method.
Using OnTouchListener will disable any previously defined touch-event behavior. You can call the actual click event from the inside by calling performClick method from your button.
//in order to use button inside OnTouchEvent, its reference must be final
//if it's not, create a new final reference to your button, like this:
final finalButton = button;
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
if(canBeClicked) {
finalButton.performClick();
return true;
}
else return false;
}
}
Overriding onTouchEvent in a class extending Button should look something like this.
#Override
public boolean onTouchEvent(MotionEvent ev) {
// ... additional code if necessary
//here we don't really need to call performClick(), although API recommends it
//we just send the touch event to the super-class and let it handle the situation.
if(activity.canBeClicked) return super.onTouchEvent(ev);
else return false;
}
One solution that I found is to disable click listener in onPause() and enable it in onResume() . Can we have better support for this?
Is there a clever way to "do something" every time a user clicks or taps on the screen? I'd like to know which view they tapped on, and do it without having to replace all my standard views with custom views.
Basically, I want to add click tracking in my app to help with analytics. We already have page tracking (by using a shared superclass for every activity), but in some cases page tracking isn't enough and we actually want to track clicks.
How can I execute a function every time the user clicks on a View on the screen?
In each of your Activities, just add this at the end of onCreate (this is especially easy if all your Activities are subclasses of a custom Activity, because then you only need to write this once):
getWindow().getDecorView().findViewById(android.R.id.content).setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
//handle your code here
return false;//don't absorb the touch.
}
});
Edit
The above code does not allow easy recognition of which Views are touched. The below code will. Note that this may break other onTouchListeners if the View in question already has registered an onTouch event. If not, this will work great. It uses the droidQuery library to select all views in the hierarchy and set uses code like above to handle touches without absorbing the event. The best place for this would be at the end of onCreate, in every Activity:
First, select the highest level of the architecture you want. If your layout's root view has an id, that would be the best thing:
$.with(this, R.id.root_id)
If not, either add one, or select the topmost view (above your layout). Note that for analytics this may provide some results you do not need:
$.with(this).selectAll()
Now, on the end of this selection, append the following:
.each(new Function() {
#Override
public void invoke($ d, Object... args) {
d.view(0).setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
//do something with the clicked view, such as:
handleTouch(v, e);
return false;//don't absorb the touch.
}
})
}
});
Then have a new method:
private void handleTouch(View v, MotionEvent e) {
//handle the view touch.
}
on Android! I need to get user input (touch event, keyboard event). is there any way? In java code, It seems there is no way. What about native code?
In java code, It seems there is no way.
=> sorry, there is a way to do detect any action user made and play with application.
Some examples:
KeyboardView.OnKeyboardActionListener
Responding to Touch Events
for touch you can use
mView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//show dialog here
return false;
}
});
if your YourActivity implements OnTouchListener you can get the event and where the user touched on the screen:
public class YourActivity extends Activity implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println("I touched: "+event.getX()+"-"+event.getY());
}
}
Actually I don't know what you exactly mean. Do you mean text input on an EditText or in General when something is touched?
If you mean text you can use TextWatcher
http://developer.android.com/reference/android/text/TextWatcher.html
If you mean on a View directly you can use OnTouchListener as mentioned above.
https://developer.android.com/reference/android/app/Activity.html#onUserInteraction()
Activity
public void onUserInteraction ()
Added in API level 3 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.