2017-10-09 9 views
1

私はアンドロイドには新しく、現在ユーザーがアプリを使用しているときに音が出るクイズアプリに取り組んでいます。私は、アプリケーションのオンとオフを切り替える必要がある私の設定アクティビティにスイッチボタンを持っています。 私はいくつかのロジックを実装しましたが、まだ動作していません。私は知っているものを使い果たしてしまったので、何か助けが必要です。今秒間アプリケーション全体のサウンドをオフにしてください

style.xml

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:soundEffectsEnabled">true</item> 
    </style> 
    <style name="AppThemeMute" parent="Theme.AppCompat.Light.NoActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:soundEffectsEnabled">false</item> 
    </style> 

MyApplication.java

public class MyApplication extends Application { 

    private static int sTheme; 

    public final static int THEME_SOUND_ON = 0; 
    public final static int THEME_SOUND_OFF = 1; 



    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    public static void changeToTheme(Context context, int theme) { 
     sTheme = theme; 

     switch (sTheme) { 
      default: 
      case THEME_SOUND_ON: 
       context.setTheme(R.style.AppTheme); 
       break; 
      case THEME_SOUND_OFF: 
       context.setTheme(R.style.AppThemeMute); 
       break; 
     } 
    } 
} 

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity { 
    private Switch mSoundSwitch; 
    private Switch mAdsSwitch; 
    SharedPreferences sharedPreferences; 
    SharedPreferences.Editor editor; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 
     // handling switch button 
     mSoundSwitch = (Switch) findViewById(R.id.soundSwitch); 
     mSoundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
if (isChecked) { 
    sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    editor = sharedPreferences.edit(); 
    editor.putBoolean("sound", true); 
    editor.apply(); 
    // Change Whole App Theme 
    MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_ON); 
} else { 
    sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
    editor = sharedPreferences.edit(); 
    editor.putBoolean("sound", false); 
    editor.apply(); 
    MyApplication.changeToTheme(getApplicationContext(), MyApplication.THEME_SOUND_OFF); 
} 
      } 
     }); 

     sharedPreferences = getPreferences(Context.MODE_PRIVATE); 
     boolean isChecked = sharedPreferences.getBoolean("sound", true); 
     mSoundSwitch.setChecked(isChecked); 

    } 

} 

魔女のボタンはsharedPreferencesで完全に動作します。ただし、ボタンをトグルすると、サウンドはミュートされません。

+0

私はあなたの現在の活動がうまくいかないと思います。あなたの 'changeToTheme'メソッドが終了する前に' recreate() 'を使用してください – UltimateDevil

+0

もしこれがうまく行かない場合は、[ここ](https://stackoverflow.com/questions/14031399/issue-with-unmute-button-java)をチェックしてください。 -android) – UltimateDevil

+1

https://stackoverflow.com/questions/10895882/mute-the-global-sound-in-android – Robert

答えて

0
private void mute() { 
    //mute audio 
    AudioManager amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true); 
} 

public void unmute() { 
    //unmute audio 
    AudioManager amanager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
    amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false); 
} 

この2つの機能は非常によく、オーディオmuteunmuteを扱うことができます。

ユーザーpreferenceに応じて、muteまたはunmuteに切り替えることができます。

+0

どのようにグローバル変数を作成しますか? –

+0

ok私はそれを簡単に編集した回答を確認しました –

+0

これはすべてのアプリの通知ストリームをミュートします。 –

関連する問題