How to refresh app upon shaking the device?

前端 未结 16 1769
眼角桃花
眼角桃花 2020-11-22 13:42

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

All I find of documentation involves implementing the SensorListener, but Eclips

16条回答
  •  花落未央
    2020-11-22 14:18

    You might want to try open source tinybus. With it shake detection is as easy as this.

    public class MainActivity extends Activity {
    
        private Bus mBus;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
    
            // Create a bus and attach it to activity
            mBus = TinyBus.from(this).wire(new ShakeEventWire());
        }
    
        @Subscribe
        public void onShakeEvent(ShakeEvent event) {
            Toast.makeText(this, "Device has been shaken", 
                    Toast.LENGTH_SHORT).show();
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            mBus.register(this);
        }
    
        @Override
        protected void onStop() {
            mBus.unregister(this);
            super.onStop();
        }
    }
    

    It uses seismic for shake detection.

提交回复
热议问题