EzDevInfo.com

shake.js

A custom 'shake' event plugin for mobile web browsers using device accelerometer. Shake.js

Objective C: Detecting a shake

I'm using the shake api like this:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        [img stopAnimating];    
    }
}

How do I detect that the shaking has stopped?


Source: (StackOverflow)

How do I detect when someone shakes an iPhone?

I want to react when somebody shakes the iPhone. I don't particularly care how they shake it, just that it was waved vigorously about for a split second. Does anyone know how to detect this?


Source: (StackOverflow)

Advertisements

Android: I want to shake it

I need to add a shake feature that will refresh my Android application.

All I find of documentation involves implementing the SensorListener, but Eclipse tells me it's deprecated and suggest SensorEventListener.

Anybody that has a nice guide to how I go about creating this shake controller?


Source: (StackOverflow)

How to detect shake motion in iPhone

On the shake of the iPhone device i want some function to be called, i dont know how to recognize shake so i tried some link and tried this code

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"called");
        [self.view setBackgroundColor:[UIColor greenColor]];
    }
}

- (BOOL)canBecomeFirstResponder
{ 
return YES; 
}

But alas no luck so can you please let me know how i can do the same

Thanks and Regards


Source: (StackOverflow)

Shake the device to launch an application in Android

How can we launch a application by just shaking the device? I want to do something similar to the app "Appshaker".


Source: (StackOverflow)

android - shake detection

I use this good library for detect shake motion on android devices. Everything is good! But on my phone (Sony Xperia P) receiving SMS cause my phone to vibrate and shake detected!
In some other phone like HTC Sensation XE, the problem doesn't exist.
Any suggestion?
Thanks is advance.


Source: (StackOverflow)

Can I shake a users desktop using WPF?

I'm writing an application for personal use that will track how often I reach for the mouse, and will keep a counter of how long I've gone mouseless. When I use the mouse, I would like it to shake my desktop workspace for a second as a negative reinforcement action.

The application is going to be called WristSlap and will be on github as soon as I have a version 0.1 ready.


Source: (StackOverflow)

iOS : How to detect Shake motion?

I added the following code to my appDelegate.m

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake )
    {
        // User was shaking the device. Post a notification named "shake".
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shake" object:self];
    }
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{   
}

- (void)shakeSuccess
{
    // do something...
}

and then I added :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // INIT THE BACKGROUND PATH STRING

    [self refreshFields];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];
    ***[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeSuccess) name:@"shake" object:nil];***

    return YES;
}

When I start my app on my iPhone, the method named "shakeSuccess" doesn't called. What should I do to implement this function in my app? any idea?


Source: (StackOverflow)

How Can I add Shake Gesture in Cocos2d CCLayer?

I've tried using shake Gesture Recognition wrapper to detect the shake gesture. I've also tried to implement the code suggested in the Geeky Lemon. But I am unable to handle the shake gesture. Is there any way to get the shake gesture recognized?

Thank you!

EDIT 1:

The code is done by the help of the shake Gesture Recognition wrapper demo.

in the init method

ShakeDispatcher * dispatcher = [ShakeDispatcher sharedInstance];
[dispatcher addShakeListener:self];    
[[[CCDirector sharedDirector]view] addGestureRecognizer:dispatcher];

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {  
    if ( event.subtype == UIEventSubtypeMotionShake ){      
        NSLog(@"Shake detected");   
    } 
}

Source: (StackOverflow)

How to animate button in android?

I am making an android app, and I have a button which leads to a messaging place. On the activity with the button, I check if there is any unread messages, and if so I want to do something to the button to let the user know that there is something unread.

I was thinking of having the button sorta vibrate horizontally like 3 shakes every 2 or 3 seconds.

I know how to run a thread in the background which does something every x milliseconds. But what I don't know what to do is shake it horizontally 3 times.

Can anyone help with this?

I was thinking of using the sin function, for the animation, I can use output from a sin function to get values that go up and down, which I can set the horizontal position of the button... But this seems too extreme, is there a better way?

Thanks.


Source: (StackOverflow)

how to detect and program around shakes for the iphone

I'm trying to implement the shake "tutorial" on this page, but I think I'm missing something. I copied his accelerometer function into myAppViewController.m file and put some nslogs in there to see if it even gets into the function when I use the simulators "shake" function. Nothing shows up in the debug console.

http://mithin.in/2009/07/28/detecting-a-shake-using-iphone-sdk

Can anyone explain what I might be missing? Or point me to a tutorial?

I found this, which looks promising, but I don't know how to "put it in a UIView" http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone


EDIT - now here's my working code because of the accepted answer's suggestion.

Here's my code to detect the shake gesture in 3.0 iphone sdk.

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}


- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    NSLog(@"{motion ended event ");

    if (motion == UIEventSubtypeMotionShake) {
    	NSLog(@"{shaken state ");

    }
    else {
    	NSLog(@"{not shaken state ");		
    }
}

Source: (StackOverflow)

Shake Device to Launch App

I am using this to work with Shake, and that works fine for me, but i wanna launch application when user shake their device, see my code below:

 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    transcript=(TextView)findViewById(R.id.transcript);
    scroll=(ScrollView)findViewById(R.id.scroll);

    shaker=new Shaker(this, 1.25d, 500, this);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();

    shaker.close();
  }

  public void shakingStarted() {
    Log.d("ShakerDemo", "Shaking started!");
    transcript.setText(transcript.getText().toString()+"Shaking started\n");
    scroll.fullScroll(View.FOCUS_DOWN);
  }

  public void shakingStopped() {
    Log.d("ShakerDemo", "Shaking stopped!");
    transcript.setText(transcript.getText().toString()+"Shaking stopped\n");
    scroll.fullScroll(View.FOCUS_DOWN);
  }

So here is my question, how can i launch an application by shaking my device ?


Source: (StackOverflow)

Detecting when UITextField text has changed due to shake undo?

I have a UITextFieldDelegate that does a whole bunch of validation on user input to determine whether or not they should be allowed to end editing. In one particular example, it is not valid to leave the field blank.

Right now I'm using textField:shouldChangeCharactersInRange:replacementString: to validate the text input after each edit by the user.

The problem is this: if the user clears the field (with the little 'x' button), the validation code goes into "invalid" mode and prevents the user from navigating away until they have entered valid text. If the user then shakes the phone to get the old text back, shouldChangeCharactersInRange is not called again and the delegate stays in the "invalid" state instead of recognizing that everything is fine again.

Not sure if I'm using it correctly, but it seems like the built-in UITextFieldDelegate machinery is not able to cope with text changes due to undo / redo.

What's the best way to achieve proper validation in this scenario? Do I really need to subclass UITextField in order to implement motionEnded:withEvent:? Seems like the edit-handling stuff in UITextField should really be independent of whether the user actually typed it or it happened due to undo, so would be bummed if I actually had to go that route.


Source: (StackOverflow)

Is there a stable way to detect shaking event?

I've tried to detect shaking event using accelerometer. I've found that when I give a contiuous shake to the phone, the value difference of the acceleration is quite stable. But when I rotate the phone, there's always a big change on the value( the value is usually bigger than "shaking without rotation"). I want to focus on the shaking event, not the rotation event. Is there a way to solve the problem?

here's my code for shaing detection

public void onSensorChanged(SensorEvent event)
    {
        if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER)
        {
            nowTime = System.currentTimeMillis();

            float x = event.values[SensorManager.DATA_X];
            float y = event.values[SensorManager.DATA_Y];
            float z = event.values[SensorManager.DATA_Z];
            nowAcc = Math.sqrt(x*x+y*y+z*z);
            accDiff = Math.abs(nowAcc - preAcc);
            timeDiff = (nowTime - preTime);
                        //  try to get the sum of 10 samplings of accDiff
            tempAccDiff10 = tempAccDiff9;
            tempAccDiff9 = tempAccDiff8;
            tempAccDiff8 = tempAccDiff7;
            tempAccDiff7 = tempAccDiff6;
            tempAccDiff6 = tempAccDiff5;
            tempAccDiff5 = tempAccDiff4;
            tempAccDiff4 = tempAccDiff3;
            tempAccDiff3 = tempAccDiff2;
            tempAccDiff2 = tempAccDiff1;
            tempAccDiff1 = accDiff;
            sumAcc = tempAccDiff10+tempAccDiff9+tempAccDiff8+tempAccDiff7+tempAccDiff6+
                     tempAccDiff5+tempAccDiff4+tempAccDiff3+tempAccDiff2+tempAccDiff1;
            Log.i("SSSS",String.valueOf(sumAcc));
                        //when I give the phone a big & continuous "shake", it returns
                        //a value about 30~40, but when I give the phone a small
                        //"rotation", it returns a value of 80~120
            preAcc = nowAcc;
            preTime = nowTime;
            if (sumAcc>100)
            {
                SM.unregisterListener(sensorListener, sensor);
            }

            //123
        }


    }//end of onSensorChanged();

is it possible to neglect the rotation event using accelerometer? Or should I try to get the orientation change and do some computations on the sumAcc?


Source: (StackOverflow)

Detecting shaking in html5 mobile

I'm building a web app in html5 at the moment that needs to be able to detect when the user shakes their phone and how fast they are shaking it. I've been browsing around but can't seem to find any examples of how I could implement this. I know html5 has an accelerometer that can detect the orientation of the phone, but how does it detect the speed at which the user is shaking it?

Also I'm testing this on my iPhone, although ideally I would like it to work on Android devices too. Thoughts? Examples would be great too. Thanks!


Source: (StackOverflow)