-->

启用/禁用键盘的声音和振动编程(enable/disable keyboard sound and

2019-09-02 02:04发布

我试图找到一种方法来禁用和敲击按键时启用键盘的声音和振动。 我搜索在堆栈溢出或其他Android论坛 ,但我并没有发现任何结果。

我试过AudioManager ,使振动模式,但我想激活振动模式和健全的键盘上。

audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, 
                AudioManager.VIBRATE_SETTING_ON);

有什么办法如何改变android.provider.Settings键盘的声音和振动?

Answer 1:

根据您的评论:

我说的是android系统中默认的键盘,我想有禁用/启用键盘的声音和振动,当用户(在键盘的设置等)在敲击键盘的键,能力

我现在在三星Galaxy S2,HTC ONE ...等谈论软键盘一样

据我所知,你不能做到这一点,因为每个输入方法保持其内部声音/振动偏好值。 例如参见的Android(AOSP)IME (在写线30〜39的):

    <CheckBoxPreference
        android:key="vibrate_on"
        android:title="@string/vibrate_on_keypress"
        android:defaultValue="@bool/config_default_vibration_enabled"
        android:persistent="true" />
    <CheckBoxPreference
        android:key="sound_on"
        android:title="@string/sound_on_keypress"
        android:defaultValue="@bool/config_default_sound_enabled"
        android:persistent="true" />

正如你可以看到它存储在其共享偏好震动/声音值。 这适用于市场上大多数的IME。 因此,你无法控制所有IME的从单点震动/声音效果。



Answer 2:

看看如何关闭我所有的应用程序或活动的默认音效禁用自来水的声音。

要禁用触觉反馈和触摸声音编程看看http://developer.android.com/reference/android/view/View.html#setHapticFeedbackEnabled(boolean) http://developer.android.com/reference/android/视图/ View.html#setSoundEffectsEnabled(布尔值)

容易做的是通过定义你的styles.xml以下

<!-- Application theme. -->
<style name="AppTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:soundEffectsEnabled">false</item>
    <item name="android:hapticFeedbackEnabled">false</item>
</style>

在你的manifest.xml

<application [...] android:theme="@style/AppTheme" >


Answer 3:

是的,如果你有root权限,您可以做到这一点。 它是一个漫长的过程,但你可以这样做:

第1步:创建XML文件名为com.android.inputmethod.latin_preferences.xml和保存的资产。

com.android.inputmethod.latin_preferences.xml

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <boolean name="popup_on" value="false" />
    <string name="auto_correction_threshold">1</string>
    <boolean name="pref_enable_metrics_logging" value="true" />
    <boolean name="pref_voice_input_key" value="true" />
    <boolean name="pref_key_use_personalized_dicts" value="true" />
    <boolean name="pref_key_block_potentially_offensive" value="true" />
    <int name="last_shown_emoji_category_id" value="1" />
    <boolean name="sound_on" value="false" />
    <string name="emoji_recent_keys">[{&quot;Integer&quot;:128533}]</string>
    <boolean name="auto_cap" value="true" />
    <boolean name="show_suggestions" value="true" />
    <boolean name="pref_key_use_contacts_dict" value="true" />
    <boolean name="next_word_prediction" value="true" />
    <boolean name="pref_key_use_double_space_period" value="true" />
    <int name="emoji_category_last_typed_id1" value="0" />
    <boolean name="vibrate_on" value="false" />
</map>

第2步:使用这个文件复制到你的应用程序文件夹(任何地方,您可以访问) asset manager的,你需要

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

这个功能将复制从资产文件

public static void copyAssets(Context context, String assetPath, String outFilename) {
        AssetManager assetManager = context.getAssets();
        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(assetPath);
            File outFile = new File(context.getExternalFilesDir(null), outFilename);

            out = new FileOutputStream(outFile);
            copyFile(in, out);
        } catch (IOException e) {
            Log.e(TAG, "Failed to copy asset: " + outFilename, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }

public static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }

步骤3:覆盖系统偏好文件系统路径(destPath)是/data/data/com.android.inputmethod.latin/shared_prefs

public static void copyToSystem(final String sourceFilePath, final String destPath) {
        Thread background = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("su");
                    DataOutputStream os = new DataOutputStream(process.getOutputStream());
//                    
                    os.writeBytes("cp -f " + sourceFilePath + " " + destPath + "\n");
                    os.flush();
                    os.writeBytes("exit\n");
                    os.flush();
                    process.waitFor();
                    process.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG, e.toString());
                }
            }
        });
        background.start();
    }

第4步:重新启动设备

这一切完成。 这一步将关闭按键声音和按键振动



文章来源: enable/disable keyboard sound and vibration programmatically