2013-05-06 148 views
8

我试图找到一种方法来禁用和启用键盘声音和振动时敲击键。我在上搜索堆栈溢出和其他Android论坛但我没有找到任何结果。以编程方式启用/禁用键盘声音和振动

我试过AudioManager启用振动模式,但我想激活键盘上的振动模式和声音。

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

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

+0

任何想法?????! – Houcine 2013-05-08 10:59:04

+0

我想这实际上取决于键盘的实现。可能有键盘在那里每次点击一个键时播放电影。我不认为你能做到这一点。无论如何,好运。 – 2013-05-08 12:34:45

+0

感谢@SherifelKhatib的回复,但是在设置中我们可以启用/禁用按键敲击声音和按键敲击振动器,我的问题是:是否有一种方法可以通过编程方式执行此操作,就像我们可以通过“Wifi” 'WifiManager'和'亮度',音量通过'AudioManager'..等等 – Houcine 2013-05-08 13:32:30

回答

4

根据您的意见:

我说的是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的振动/声音效果。

+0

你能解释一下吗?我无法理解在哪里以及如何实现这一点。 @ozbek – 2017-10-24 12:52:34

+0

@UpendraShah,你在开发一个IME吗? – ozbek 2017-10-25 13:07:08

+0

其实我想处理来自我的应用程序的vibrate_on_keypress和sound_on_keypress,所以我想要实用地启用或禁用这些。 – 2017-10-26 04:46:21

9

看看How to disable default sound effects for all my application or activity禁用敲击声。

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

更容易做的是通过定义在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" > 
+0

这不适用于键盘按键, – Houcine 2013-05-14 10:28:59

+0

你能解释一下“键盘按键”吗?您的意思是软键盘(显示在屏幕上)或硬键盘(例如,连接到华硕Transformer-Pad)。所以当用户使用键盘时,你想关闭触觉反馈和声音? – 2013-05-14 22:55:04

+0

我说的是软键盘,就像三星GALAXY S2,HTC ONE等一样。 – Houcine 2013-05-14 23:13:24

0

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

步骤:1 创建xml文件,名为com.android.inputmethod.latin_preferences.xml并保存在资产中。使用asset manager的,你需要

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

此功能将文件拷贝这个文件复制到你的应用程序文件夹(任何地方,您可以访问):

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步资产

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:重启设备

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