'Shake device to make a call

I've been trying to develop an android app (in java) which on shake can make a phone call. But I'm facing a small kind of error (not exactly error but can be said so). What is happening in the app is, First, I stored the contacts using SQLite DB and displayed it on an Activity using recyclerview. And each item in the view has got a star on right of it and we can select just one of it to make it as an urgent number. And once the user shakes the device, directly call should be made to that number. Sometimes call is made to null and most of the time the shake doesn't get detected.

Below is my code,

@Override
    public void hearShake() {
        Intent starIntent = new Intent(Intent.ACTION_CALL);
        starIntent.setData(Uri.parse("tel:" + starNum));

        startActivity(starIntent);
    }

In the MainActivity, under onCreate()


        Intent starIntent = getIntent();
        starNum = starIntent.getStringExtra("starNum");

        ShakeDetector shakeDetector = new ShakeDetector(this);
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        shakeDetector.start(sensorManager);

Only thing I want my app to do is to make a call properly to that particular phone number if I shake my device.

Any kind help would be really appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source