2013-03-24 48 views
0

我的设置活动中,当用户单击ToggleButton时,它应该将整个应用程序中的声音静音,但它不工作。SoundPool onClick按钮听起来像我放在我的教程类中仍然在播放声音onClick.I已经使用ToggleButton指定了我的共享首选项。打开/关闭声音按钮不起作用

这里是我的代码,

package com.fullfrontalgames.numberfighter; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ToggleButton; 


public class Settings extends Activity { 











    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 


     Button Notifications = (Button) findViewById(R.id.Notifications); 
     Button Done = (Button) findViewById(R.id.done); 
     Button AccountSettings = (Button) findViewById(R.id.AccountSettings); 
     final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle); 



     AT.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       SharedPreferences appPrefs = 
         getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences", 
           MODE_PRIVATE); 
       SharedPreferences.Editor editor = appPrefs.edit(); 
       editor.putBoolean("atpref", AT.isChecked()); //value to store 
       editor.commit(); 

       if ((AT.isChecked())) { 
        AT.setSoundEffectsEnabled(true); 

        } else { 
        AT.setSoundEffectsEnabled(false); 
         } 
       } 

     }); 







     SharedPreferences appPrefs = 
       getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences", 
       MODE_PRIVATE); 
     boolean atpref = appPrefs.getBoolean("atpref", true); //default is true 
     AT.setChecked(atpref); 
     Done.setOnClickListener(new View.OnClickListener() { 








      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent Intent = new Intent(Settings.this,activity_main.class); 
       Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(Intent); 



      } 
     }); 
     Notifications.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications")); 
      } 
     }); 
     AccountSettings.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings")); 
      } 
     }); 









    } 
+0

想方设法把使用该值at.ischecked()刚过sharedpreferences调试,我不觉得现在有任何问题。尝试这个 – 2013-03-24 04:40:12

+0

你的代码看起来OK尝试使用'Log'值或'Toast' – 2013-03-24 04:42:21

回答

1

根据setSoundEffectsEnabled

设置这种观点是否应该对事件 如点击和触摸启用声音效果doc

如果您已经播放了 声音,例如,播放dtmf音调的拨号键,您可能希望禁用视图的声音效果。

因此,此功能应设置为关闭视图的声音效果(如点击或触摸)。不要关闭设备声音。

你的目的检查AudioManager

+0

谢谢,我想这是这样的,我会查找AudioManager为此! – Cranosaur 2013-03-24 04:48:53

+1

搜索静音取消静音设备在所以..我敢打赌,你会很快找到解决方案 – stinepike 2013-03-24 04:53:57

+0

setSoundEffectsEnabled我已经把它设置为false我的一个按钮,但没有效果。它仍然会发出声音。在Google Nexus 10 – 2014-01-01 12:02:58