直截了当,我制作了一个游戏,一旦用户完成一个关卡,他们就会进入下一个活动。所以基本上 Activity1 完成然后 Activity 2 加载等等。我想保存他们进行的活动,然后在主菜单上有一个继续按钮,下次他们玩时会加载它。到目前为止,我所做的是使用 Int 将进度保存到共享首选项。但我无法弄清楚如何回调这些值,以便继续按钮知道如果这有意义的话要加载哪个活动。下面是我用来保存用户进度的代码,我感觉以这种方式使用 Ints 可能是错误的方法,所以任何建议都会很棒。我对 android 还很陌生,所以很好的明确答案会让我不费吹灰之力哈哈 :)
这是我进行保存的成功闪屏:
public class success1 extends Activity {
private boolean mIsBackButtonPressed;
private static final int SPLASH_DURATION = 2000; // 2 seconds
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sp =
this.getSharedPreferences("GameProgress",0);
SharedPreferences.Editor editor =
sp.edit();
editor.putInt("scene1",1);
editor.commit();
setContentView(R.layout.level_success);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
MediaPlayer player;
AssetFileDescriptor afd;
try {
// Read the music file from the asset folder
afd = getAssets().openFd("correct.mp3");
// Creation of new media player;
player = new MediaPlayer();
// Set the player music source.
player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength());
// Set the looping and play the music.
player.setLooping(false);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
finish();
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
if (!mIsBackButtonPressed) {
// start the home screen if the back button wasn't pressed already
Intent intent = new Intent(success1.this, scene2.class);
success1.this.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}
}
}, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called
}
@Override
public void onBackPressed() {
// set the flag to true so the next activity won't start up
mIsBackButtonPressed = true;
super.onBackPressed();
}
}
这只是一个带有继续按钮的调试主菜单:
public class DebugMain extends Activity {
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debugmenu);
addListenerOnButton();
}
public void addListenerOnButton() {
//Select a specific button to bundle it with the action you want
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}