Android Accelerometer Sensor

前端 未结 2 1803
半阙折子戏
半阙折子戏 2021-01-28 06:54

I am trying to work with Accelerometer Sensor. So i tried this example: http://blog.androgames.net/85/android-accelerometer-tutorial/

It work perfectly. But when i chang

相关标签:
2条回答
  • 2021-01-28 07:14

    For CONTEXT try initializing it as

    this.getApplicationContext()
    
    0 讨论(0)
  • 2021-01-28 07:22

    The Above code had NULLPointerException in case of CONTEXT. Thats why the application was crashing. While showing toast done use this. Use getApplicationContext(). Hope this will solve your problem.

    Modified Code:

    class Accelerometer extends Service implements AccelerometerListener{ 
    
    @Override
    public IBinder onBind(Intent intent) {
    // TODO Put your code here
    return null;
    }
    
    @Override
    public void onCreate() {
    System.out.println(”start listening”);
    // if (AccelerometerManager.isSupported()) { AccelerometerManager.startListening(this);
    
    // }
    }
    
    @Override
    public void onDestroy() {
    System.out.println(”stop listening”);
    // if (AccelerometerManager.isListening()) { AccelerometerManager.stopListening();
    // }
    }
    
    /**
    * onShake callback
    */
    public void onShake(float force) {
    Toast.makeText(getApplicationContext(), “Phone shaked niktilha omha ya 3ammi el7ag: ” + String.valueOf(force), 1000).show(); }
    
    /**
    * onAccelerationChanged callback
    */
    public void onAccelerationChanged(float x, float y, float z) { System.out.println(”x = “+x+” y = “+y+” z = “+z); }
    
    }
    
    0 讨论(0)
提交回复
热议问题