Android开发,onClickListener无法正常工作

时间:2013-03-29 13:10:37

标签: java android onclicklistener

我对Android开发完全陌生,我正在通过一本书(实用的android 4游戏开发)制作一款名为starfighter的游戏。这是菜单屏幕上的问题。

退出按钮用于工作,并关闭应用程序。由于我在后台播放音乐,它不再有效。点击退出停止音乐,但不关闭应用程序,我不知道为什么,任何帮助,请?代码如下:

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类型的方法停止。

提前致谢! :)

编辑:

还添加了这个类,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作为第一行代码。没有输出到控制台,所以我不确定是否正在调用该方法。但后来我很困惑为什么音乐还在停止。

2 个答案:

答案 0 :(得分:0)

Use 

System.exit(0);

Before that 

stopservice();
Thread.stop();

should be done

答案 1 :(得分:0)

我建议你删除通过ID杀死你的进程的代码,因为这是一个不好的做法,Android可能会重启kill应用程序。 请改用finish();