2013-05-06 145 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:重啓設備

這一切完成。這些步驟將關閉按鍵聲音和按鍵振動