我有一个Preference活动,其中只有一个设置,声音打开或关闭,它在我的所有游戏中工作正常除了我的主菜单,我仍然可以听到按钮点击声音。但是,在我退出游戏并重新开始游戏后,我的声音就消失了。代码在我的所有活动中完全相同,但不知何故,在重新启动游戏之前,它不适用于主要活动。那是什么意思?显然代码正在运行,但有些事情是不对的。无论如何,这里是代码,而不是全部,只是与此问题相关的代码...只有一个按钮,对于退出...以及以下,以防万一,我将发布整个活动代码:
public boolean music;
MediaPlayer buttonClicks;
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
music = getPrefs.getBoolean("checkbox", true);
buttonClicks = MediaPlayer.create(this, R.raw.click);
dugme5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonBack.start();
}
finish();
}
});
全班:
public class Menu extends SwarmActivity implements OnClickListener{
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
// There are no active networks.
return false;
} else
return true;
}
MediaPlayer buttonClicks;
MediaPlayer buttonBack;
Button dugme1, dugme2, dugme3, dugme4, dugme5, dugme6;
TextView tvKviz;
public boolean music;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
music = getPrefs.getBoolean("checkbox", true);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
//if ( Swarm.isEnabled() ) {
Swarm.init(this, 4583, "e8398d93819da3d6d5f7d13f5b5a0deb");
//}
addListenerOnButton();
boolean firstrun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("firstrun", true);
if (!firstrun){
Intent swarm = new Intent("rs.androidaplikacijekvizopstekulture.SWARMPOPUP");
startActivity(swarm);
// Save the state
getSharedPreferences("PREFERENCE", MODE_PRIVATE)
.edit()
.putBoolean("firstrun", false)
.commit();
}
if ((!firstrun)&&(!isNetworkConnected())){
Intent netPopup = new Intent("rs.androidaplikacijekvizopstekulture.NETPOPUP");
startActivity(netPopup);
}
}
private void addListenerOnButton() {
Typeface naslov = Typeface.createFromAsset(getAssets(), "Lobster.ttf");
Typeface dugmad = Typeface.createFromAsset(getAssets(), "Bebas.ttf");
buttonClicks = MediaPlayer.create(this, R.raw.click);
buttonBack = MediaPlayer.create(this, R.raw.button31);
tvKviz = (TextView) findViewById(R.id.tvKviz);
dugme1 = (Button) findViewById(R.id.bStart);
dugme2 = (Button) findViewById(R.id.bPravila);
dugme3 = (Button) findViewById(R.id.bTopScores);
dugme4 = (Button) findViewById(R.id.bPodesavanja);
dugme5 = (Button) findViewById(R.id.bIzlaz);
dugme6 = (Button) findViewById(R.id.bKontakt);
dugme1.setTypeface(dugmad);
dugme2.setTypeface(dugmad);
dugme3.setTypeface(dugmad);
dugme4.setTypeface(dugmad);
dugme5.setTypeface(dugmad);
dugme6.setTypeface(dugmad);
tvKviz.setTypeface(naslov);
dugme1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
startActivity(new Intent("rs.androidaplikacijekvizopstekulture.IZBOR"));
}
});
dugme2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
Intent pref = new Intent("rs.androidaplikacijekvizopstekulture.PRAVILA");
startActivity(pref);
}
}
});
dugme3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
Swarm.showLeaderboards();
}
});
dugme4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
Intent pref = new Intent("rs.androidaplikacijekvizopstekulture.PREFS");
startActivity(pref);
}
});
dugme5.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonBack.start();
}
finish();
}
});
dugme6.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(music == true){
buttonClicks.start();
}
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "marjanbazalac@gmail.com" });
// email.putExtra(Intent.EXTRA_CC, new String[]{ to});
// email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, "subject...");
email.putExtra(Intent.EXTRA_TEXT, "poruka...");
// need this to prompts email client only
email.setType("message/rfc822");
try {
startActivity(Intent.createChooser(email, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Menu.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.menu_contact:
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[] { "marjanbazalac@gmail.com" });
// email.putExtra(Intent.EXTRA_CC, new String[]{ to});
// email.putExtra(Intent.EXTRA_BCC, new String[]{to});
email.putExtra(Intent.EXTRA_SUBJECT, "subject...");
email.putExtra(Intent.EXTRA_TEXT, "poruka...");
// need this to prompts email client only
email.setType("message/rfc822");
try {
startActivity(Intent.createChooser(email, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(Menu.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
return true;
}
return false;
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
据我所见,您只在onCreate()
创建活动时检查偏好设置。这就是为什么在重新开始游戏之前它不会改变的原因。
如果你想在每次点击按钮时检查它,只需写一个简单的函数:
public boolean isMusicEnabled()
{
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
return getPrefs.getBoolean("checkbox", true);
}
现在,您可以使用if(music == true)
而不是检查每个侦听器中的if(isMusicEnabled())
。
或者,将支票移至onResume()
,以便在从偏好活动返回菜单时调用。
答案 1 :(得分:1)
您在Activity的onCreate()
方法中获得了偏好。除非您在某处将finish()
作为主要活动,否则它将保持“创建”状态,直到活动因其他原因完成或由系统关闭。
如果您获得了偏好设置并更改了onStart()
或onRestart()
中的声音设置,我认为您会发现它按预期工作。