0

我制作了一个带有主要活动和偏好活动的小应用程序。这是 PrefsActivity:

public class PrefsActivity extends PreferenceActivity {

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs);
    }
}

然后我通过菜单从主活动触发 Preference 活动,如果选中复选框,则要求将音量设置为 0:

这是整个主要活动:

package com.stressedmanasoft.clickcounter;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.graphics.*;
import android.media.*;
import android.content.*;
import android.preference.*;

public class MainActivity extends Activity {

ImageView counter;
MediaPlayer clicksnd;
MediaPlayer resetsnd;
int x=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    counter = (ImageView) findViewById(R.id.Counter);
    clicksnd = MediaPlayer.create(this, R.raw.clicksnd);
    resetsnd = MediaPlayer.create(this, R.raw.resetsnd);

    final TextView count = (TextView) findViewById(R.id.Count);
    Typeface face = Typeface.createFromAsset(getAssets(), "Bohemian_typewriter.ttf");
    count.setTypeface(face);
    count.setText(""+x);

    final Button countButton = (Button) findViewById(R.id.CountButton);
    countButton.setOnClickListener (new View.OnClickListener() {   
        public void onClick(View v) {
            clicksnd.start();
            x = x+1;
            count.setText(""+x);
        }
    });

    final Button resetButton = (Button) findViewById(R.id.ResetButton);
    resetButton.setOnClickListener (new View.OnClickListener() {   
        public void onClick(View v) {
            resetsnd.start();
            x = 0;
            count.setText(""+x);
        }
    });

    SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
    if(myPreference.getBoolean("checkBox", true)) {
        clicksnd.setVolume(0, 0);}

}

@Override
public boolean onCreateOptionsMenu(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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.menu_settings:
        Intent intent = new Intent(MainActivity.this, PrefsActivity.class);
        startActivity(intent);
        break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
super.onResume();

SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
if(myPreference.getBoolean("checkBox", true)) {
    clicksnd.setVolume(0, 0);
    }

}

}

当我启动应用程序时,音频打开并且未选中复选框。如果我选中它,音频会按预期关闭,但如果我取消选中它,音频将保持关闭状态。为什么?

我应该添加一个监听器吗?

提前致谢

4

1 回答 1

0

试试这个:

@Override
protected void onResume() {
    super.onResume();
    SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
    if(myPreference.getBoolean("checkBox", true)) {
        clicksnd.setVolume(0, 0);
    } else {
        clicksnd.setVolume(100, 100);
    }
}

唯一的区别是onResume()你将覆盖它,而不是onStart().

于 2013-01-07T13:39:51.637 回答