2

我在尋找如何更改Jelly Bean中的振動設置。我發現所有的JB前振動設置都已被棄用,但沒有看到任何新的AudioManager。[更改振動設置]代碼。有一個設置「振鈴時振動」,我想知道如何玩。更改Jelly Bean中的振動設置Android

感謝您的幫助。

回答

6

在android4.1你可以用它來控制「振動&振鈴」

Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0); 
+0

謝謝sooo ...太多了!我已經搜索了整個世界各地的這個答案。謝謝! – Matthew

+1

到目前爲止,這在Android M上引發了一個IllegalArgumentException異常:「您無法更改私有安全設置。」 –

2

從文檔:

This method is deprecated. 
Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode(). 

好像谷歌希望振動設置由每個應用程序本身上的應用到應用的基礎上進行處理,調整是通過查詢getRingerMode()設置。

+2

但getRingerMode()將不會返回「振鈴時振動」設置。 – BrainCrash

0

Settings.System.VIBRATE_WHEN_RINGING聲明與@hide註釋,所以你可能有麻煩Android Studio中使用它。 因此,也許你有一個由它的值來代替Settings.System.VIBRATE_WHEN_RINGING"vibrate_when_ringing"

這是煩人,它宣佈與@hide註解,因爲這意味着谷歌不使用它希望外部developpers ...

0

這個問題困擾了我幾天。最後讓它工作。確保您擁有正確的權限。

使用許可權的android:NAME = 「android.permission.WRITE_SETTINGS」/

protected void onCreate(Bundle savedInstanceState) { 
    audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE); 

} 
在我的點擊監聽

然後我採用了以下內容:

   lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
        if (vibrate == 1) { 
         try { 
          Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1); 
          audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on ring: 
          audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on notify 

          boolean vibrateWhenRinging; 
          vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1); 
          Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show(); 
         } catch (Exception e) { 
          Toast.makeText(MainActivity.this, "System vibrate error", Toast.LENGTH_LONG).show(); 
         } 
        } else { 
         try { 
          Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0); 
          audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on ring: 
          audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on notify 

          boolean vibrateWhenRinging; 
          vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1); 
          Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show(); 
         } catch (Exception e) { 
          Toast.makeText(MainActivity.this, "System vibrate error in else statement", Toast.LENGTH_LONG).show(); 
         } 
        } 
       } 
相關問題