Android: Enable Haptic Feedback and Keypress Sound

I have a problem with the system settings in Android. In short, we are developing an application that does some custom experiments using different input parameters. We will need to enable and disable keystroke sounds and keystroke vibrations programmatically in different experiments.

I've explored a few options, but would like to avoid actually manually tuning the listener and vibration, if at all possible.

I gave myself permission to change system settings:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

      

Below is the code I am using to change system settings:

   android.provider.Settings.System.putInt(getContentResolver(),
             android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED,
             1);

      

Changes are not reflected in the system settings.

The following code works, confirmed by the fact that the settings are actually changed in the system settings window on the emulator.

   android.provider.Settings.System.putInt(getContentResolver(),
             android.provider.Settings.System.TEXT_AUTO_PUNCTUATE,
             0);

   android.provider.Settings.System.putInt(getContentResolver(),
             android.provider.Settings.System.TEXT_AUTO_REPLACE,
             0);

      

I also tried to set specific preferences in the view, as such:

    EditText inputArea = (EditText) findViewById(R.id.editText1);
    inputArea.setHapticFeedbackEnabled(true);
    inputArea.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
    inputArea.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING | HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);

      

I pushed him to the phone and it has no effect either.

What I need is a way to programmatically enable / disable haptic feedback and sound on key presses during the experiment. I searched for Stack Overflow but didn't find anything useful.

Does anyone know any tricks or will I have to override myself with key handlers and manually vibrate the phone?

Thank!

+3


source to share





All Articles