我对 Android 开发完全陌生,我正在通过一本书(实用的 android 4 游戏开发)制作一款名为星际战斗机的游戏。这是菜单屏幕上的问题。
退出按钮用于工作,并关闭应用程序。由于我通过在后台播放音乐来工作,它不再起作用。单击退出会停止音乐,但不会关闭应用程序,我不知道为什么,请帮忙?代码如下:
SFMainMenu.java:
package com.example.starfighter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class SFMainMenu extends Activity {
//Called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Fire up background music
SFEngine.musicThread = new Thread(){
public void run(){
Intent bgmusic = new Intent(getApplicationContext(), SFMusic.class);
startService(bgmusic);
SFEngine.context = getApplicationContext();
}
};
SFEngine.musicThread.start();
final SFEngine engine = new SFEngine();
//Set menu button options
ImageButton start = (ImageButton)findViewById(R.id.btnStart);
ImageButton exit = (ImageButton)findViewById(R.id.btnExit);
start.getBackground().setAlpha(SFEngine.MENU_BUTTON_ALPHA);
start.setHapticFeedbackEnabled(SFEngine.HAPTIC_BUTTON_FEEDBACK);
exit.getBackground().setAlpha(SFEngine.MENU_BUTTON_ALPHA);
exit.setHapticFeedbackEnabled(SFEngine.HAPTIC_BUTTON_FEEDBACK);
start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//Start the game!!!
}
});
exit.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
boolean clean = false;
clean = engine.onExit(v);
if(clean){
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
}
}
});
}
}
SFEngine.java
package com.example.starfighter;
import android.view.View;
import android.content.Context;
import android.content.Intent;
public class SFEngine {
//Constants that will be used in the game
public static final int GAME_THREAD_DELAY = 4000;
public static final int MENU_BUTTON_ALPHA = 0;
public static final boolean HAPTIC_BUTTON_FEEDBACK = true;
public static final int SPLASH_SCREEN_MUSIC = R.raw.looptrouble;
public static final int R_VOLUME = 100;
public static final int L_VOLUME = 100;
public static final boolean LOOP_BACKGROUND_MUSIC = true;
public static Context context;
public static Thread musicThread;
//Kill game and exit
public boolean onExit(View v){
try{
Intent bgmusic = new Intent(context, SFMusic.class);
context.stopService(bgmusic);
musicThread.stop();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
}
Eclipse 在 musicThread.stop() 行上发出警告 - 不推荐使用 Thread 类型的方法 stop。
提前致谢!:)
编辑:
还添加了此类,SFMusic,这可能是相关的:
package com.example.starfighter;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.content.Context;
public class SFMusic extends Service {
public static boolean isRunning = false;
MediaPlayer player;
@Override
public IBinder onBind(Intent arg0){
return null;
}
@Override
public void onCreate(){
super.onCreate();
setMusicOptions(this, SFEngine.LOOP_BACKGROUND_MUSIC, SFEngine.R_VOLUME, SFEngine.L_VOLUME,
SFEngine.SPLASH_SCREEN_MUSIC);
}
public void setMusicOptions(Context context, boolean isLooped, int rVolume, int lVolume, int soundFile){
player = MediaPlayer.create(context, soundFile);
player.setLooping(isLooped);
player.setVolume(rVolume, lVolume);
}
public int onStartCommand(Intent intent, int flags, int startId){
try{
player.start();
isRunning = true;
} catch(Exception e){
isRunning = false;
player.stop();
}
return 1;
}
public void onStart(Intent intent, int startId){
}
public void onStop(){
isRunning = false;
}
public IBinder onUnBind(Intent arg0){
//TODO Auto-generated method stub
return null;
}
public void onPause(){
}
@Override
public void onDestroy(){
player.stop();
player.release();
}
@Override
public void onLowMemory(){
player.stop();
}
}
我尝试在 onClick 方法中添加一个 println 作为第一行代码。没有任何东西输出到控制台,所以我不确定该方法是否被调用。但后来我很困惑为什么音乐仍然停止。