我有一个首选项 Activity ,其中只有一个设置,即声音打开或关闭,它在我的所有游戏中都运行良好,除了在我的主菜单中,我仍然可以听到按钮点击声音。但是,当我退出游戏并重新启动后,我的声音就消失了。我的所有 Activity 中的代码都是完全相同的,但不知何故,它在游戏重新启动之前不适用于主要 Activity 。这是怎么回事?显然代码可以工作,但有些地方不对劲。无论如何,这是代码,不是全部,只是与此问题相关的代码...只有一个按钮,用于退出...下面,以防万一,我将发布整个 Activity 代码:
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[] { "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="523f332038333c303328333e333112353f333b3e7c313d3f" rel="noreferrer noopener nofollow">[email protected]</a>" });
// 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[] { "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="254844574f444b47445f44494446654248444c490b464a48" rel="noreferrer noopener nofollow">[email protected]</a>" });
// 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
}
}
最佳答案
您将在 Activity 的 onCreate()
方法中获取首选项。除非您在某处 finish()
您的主 Activity,否则它将保持“已创建”状态,直到该 Activity 由于其他原因完成或被系统关闭。
如果您获得首选项并在 onStart()
或 onRestart()
中更新声音设置,我想您会发现它按预期工作。
关于java - 优先禁用声音无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15412953/