У меня есть функция предпочтения, в которой есть только одна настройка, Звук включен или выключен, и он отлично работает во всей моей игре EXCEPT в моем главном меню, где я все еще слышу звуки нажатия кнопки. Но, после выхода из игры и начала ее снова, мой звук отключен. Код точно такой же во всех моих действиях, но почему-то он не работает для основного действия до перезапуска игры. Что это значит? Очевидно, что код работает, но что-то не так. Во всяком случае, вот код, не все, только код, связанные с этим вопросом ... только одна кнопка, для выхода ... и ниже, на всякий случай, я буду размещать весь код деятельности:Отключение звука с предпочтением не работает правильно
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[] { "[email protected]" });
// 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[] { "[email protected]" });
// 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
}
}
Это работало как шарм! Я не думал, что, когда я вернусь из prefs, моя деятельность уже создана, и именно по этой причине она не работает. Спасибо друг. – user2083882