![](/img/trans.png)
[英]How to remember that the user has pressed a button when reentering web app in Phonegap
[英]Android: How to get app to remember if mute button has been pressed or not
我已经成功创建了一个按钮,可以根据需要使我的按钮按下声音静音。 我遇到的问题是,当我离开页面或关闭应用程序并返回声音时,将返回到默认的打开状态。 我正在尝试通过首选项实现此目的,但是它不起作用。 我以前曾使用首选项来成功存储高分,但这次我可以按预期使用它。 这是我的声音代码:
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
public class homePage extends Activity {
;
Button Mute;
int Sound = 1;
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.home_page);
Mute = (Button) findViewById(R.id.mute);
setButtonOnClickListeners();
RelativeLayout layout = new RelativeLayout(this);
layout = (RelativeLayout) findViewById(R.id.home);
}
public void onPause(Bundle savedInstanceState) {
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("SP", Context.MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt("SP", Sound);
editor.commit();
}
public void onResume(Bundle savedInstanceState) { SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("SP", Context.MODE_PRIVATE);
int counter = sharedPrefs.getInt("SP", 0);}
public void sound(){
final MediaPlayer mp = new MediaPlayer();
if (Sound==1){
try {
AssetFileDescriptor afd;
afd = getAssets().openFd("butpress.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(Sound==0){mp.stop();}}
private void setButtonOnClickListeners(){
Mute.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(Sound==1){
Mute.setBackgroundResource( R.drawable.sound_off);
--Sound;
}
else if(Sound==0){
Mute.setBackgroundResource( R.drawable.sound_high);
Sound++;
}
}
});
}
}
我只需要保存声音即可使声音静音,并知道出了什么问题?
更新1:
这是编辑后的代码。
public class homePage extends Activity {
Button Mute;
int Sound = 0;
private final static String PREFS_KEY = "shared_prefs";
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.home_page);
super.onCreate(savedInstanceState);
Mute = (Button) findViewById(R.id.mute);
setButtonOnClickListeners();
onResume();// is this needed here? not noticed any difference either way
if(Sound==1){
Mute.setBackgroundResource( R.drawable.sound_high);
getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE).edit().putBoolean("mute_pressed", true).commit();
}
else if(Sound==0){
Mute.setBackgroundResource( R.drawable.sound_off);
getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE).edit().putBoolean("mute_pressed", true).commit();
}
}
public void sound(){
final MediaPlayer mp = new MediaPlayer();
if (Sound==1){
try {
AssetFileDescriptor afd;
afd = getAssets().openFd("butpress.mp3");
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(Sound==0){mp.stop();}}
private void setButtonOnClickListeners(){
Mute.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(Sound==1){
Mute.setBackgroundResource( R.drawable.sound_off);
--Sound;
getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE).edit().putBoolean("mute_pressed", true).commit();
}
else if(Sound==0){
Mute.setBackgroundResource( R.drawable.sound_high);
Sound++;
getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE).edit().putBoolean("mute_pressed", true).commit();
}
}
});
} public void onPause(Bundle savedInstanceState) {
SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE);
Editor editor = sharedPrefs.edit();
editor.putInt("SP", Sound);
editor.commit();
}
public void onResume(Bundle savedInstanceState) { SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences("PREFS_KEY", Context.MODE_PRIVATE);
Sound = sharedPrefs.getInt("SP", 0);}
}
我认为存储的号码不正确
在OnClickListener的onClick方法中,您可以保存状态
getSharedPreferences("SP", Context.MODE_PRIVATE).edit().putBoolean("mute_pressed", true).commit();
或者如果您想要像在onPause中一样保存int值,请执行
getSharedPreferences("SP", Context.MODE_PRIVATE).edit().putBoolean("mute_val", Sound).commit();
因为到目前为止,您实际上并没有在增加/减少时进行保存,而是仅保存onPause()。 然后,您可以通过更改onResume()中的值
int counter = ...
// to
Sound =...
这会将您的类Sound变量设置为旧的存储值。
我也建议不要使用"SP"
(我强烈建议不要在共享状态键和共享值键同时使用相同的键值)使用在类顶部设置的静态变量像
private final static String PREFS_KEY = "shared_prefs";
然后像这样使用变量
getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)...
这有助于本地化。 您还应该检查Java中的命名约定,以免在事情变得更加复杂时避免头痛。
更新以使用您的代码
// this should be HomePage, not homePage
public class homePage extends Activity {
private final static String PREFS_KEY = "shared_prefs";
private final static String MUTE_PRESSED_KEY = "mute_pressed";
private Button Mute; // this should be mute, not Mute
private int Sound = 0; // this should be sound, not Sound
private SharedPreferences prefs;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_page);
Mute = (Button) findViewById(R.id.mute);
setButtonOnClickListeners();
prefs = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
if(prefs.getBoolean(MUTE_PRESSED_KEY, false))
Sound = 1;
else
Sound = 0;
}
// I DIDN'T REALLY LOOK AT THIS SO COUNTING ON YOU THAT IT WORKS
public void sound() {
final MediaPlayer mp = new MediaPlayer();
if (Sound == 1) {
try {
AssetFileDescriptor afd;
afd = getAssets().openFd("butpress.mp3");
mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
afd.getLength());
mp.prepare();
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (Sound == 0) {
mp.stop();
}
}
private void setButtonOnClickListeners() {
Mute.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Sound == 1) {
Mute.setBackgroundResource(R.drawable.sound_off);
Sound = 0;
prefs.edit().putBoolean(MUTE_PRESSED_KEY, false).commit();
} else if (Sound == 0) {
Mute.setBackgroundResource(R.drawable.sound_high);
Sound = 1;
prefs.edit().putBoolean(MUTE_PRESSED_KEY, true).commit();
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.