Android:如何创建1秒的计时器

时间:2013-11-17 10:49:00

标签: java android

好吧我正在开发一个andoird应用程序,这是游戏simon,我遇到了一个问题,当我从菜单移动到游戏。 我想游戏将在随机选择的游戏开始时播放3个声音 和应用程序一个接一个地播放它们在1秒延迟,但问题是应用程序之前的游戏背景加载声音所有播放同时..很快一切都不起作用,我很鄙视。 为了使用这些按钮,我创建了一个名为column的类,它包含视图中的按钮及其产生的声音,并使用方法play来激活声音并更改图像。 游戏逻辑的类游戏 这是代码:

列类:

package com.example.thegreatsimon;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class Column {

ImageButton button;
SoundPool sound;
int soundId;    
int srcColor,lightColor;
Context con;
public Column(ImageButton button,Context con)
{
    sound=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    this.button=button;
    switch (button.getId())
    {   
    case R.id.ibBlue:
    {
        soundId=sound.load(con,R.raw.a ,1);
        srcColor=R.drawable.blue;
        lightColor=R.drawable.lightblue;
    } break;
    case R.id.ibGreen:
    {
        soundId=sound.load(con,R.raw.b,1);
        srcColor=R.drawable.green;
        lightColor=R.drawable.lightgreen;
    } break;
    case R.id.ibRed:
    {
        soundId=sound.load(con,R.raw.c ,1);
        srcColor=R.drawable.red;
        lightColor=R.drawable.lightred;
    } break;
    case R.id.ibYellow:
    {
        soundId=sound.load(con,R.raw.d ,1);
        srcColor=R.drawable.yellow;
        lightColor=R.drawable.lightyellow;
    } break;
    }   
    button.setOnClickListener(new Listen());
}   

class Listen implements OnClickListener
{
    @Override
    public void onClick(final View v) 
    {
        play();
    }   
}

void play()
{
    sound.play(soundId, 1, 1, 0, 0, 1)  ;
    button.setBackgroundResource(lightColor);
    Handler handler = new Handler();    
    handler.postDelayed(new Runnable() 
    {
        @Override
        public void run() 
        {
            button.setBackgroundResource(srcColor);
        }
    }, 300);

}

}   

游戏类:

package com.example.thegreatsimon;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageButton;

public class Game extends Activity {

Column bYellow,bRed,bGreen,bBlue;
SoundPool a=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
int diff;
int life=3;
Random rand= new Random();
Handler hand = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    diff = Settings.difficulty;
    switch(diff)
    {
    case(0):
        setContentView(R.layout.gameeasy);
    break;
    case(1):
        setContentView(R.layout.game);
    bYellow=new Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
    break;
    case(2):
        setContentView(R.layout.game);
    bYellow=new      Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
    break;
    }
    bRed=new Column((ImageButton)findViewById(R.id.ibRed),getApplicationContext());
    bGreen=new Column((ImageButton)findViewById(R.id.ibGreen),getApplicationContext());
    bBlue=new Column((ImageButton)findViewById(R.id.ibBlue),getApplicationContext());
    EasyGame();
}


public boolean EasyGame()
{
    int r;
    ArrayList list = new ArrayList();
    boolean proceed;
    for(int i=0;i<3;i++)
    {
        r=rand.nextInt(3);
        switch(r)
        {
        case 0:
        {
            list.add(bRed);
            Sleep(bRed);                
        }break;
        case 1:
        {
            list.add(bGreen);
            Sleep(bGreen);
        }break;
        case 2:
        {
            list.add(bBlue);
            Sleep(bBlue);
        }break;
        }
    }
    proceed=UserPlay();

    /*for(int i=0;i<2;i++)
    {
        r=rand.nextInt(3);
        switch(r)
        {
        case 0:
            list.add(bRed);
            break;
        case 1:
            list.add(bGreen);
            break;
        case 2:
            list.add(bBlue);
            break;
        }
        for(int j=0;j<list.size();j++)
        {
            ((Column)list.get(j)).play();
            Sleep();
        }
        proceed=UserPlay();
    }*/

    return true;
}

public void MediumGame()
{

}
public void HardGame()
{

}



    public void Sleep(final Column c)
{
    Handler handler = new Handler();    
    handler.postDelayed(new Runnable() {
    @Override
                public void run() {
                   c.play();
                }
            }, 1000);
}



public boolean UserPlay()
{
    return true;
}

@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;
}

}

2 个答案:

答案 0 :(得分:5)

Handler handler = new Handler();    
handler.postDelayed(new Runnable() {
@Override
            public void run() {
               //write your code here to be executed after 1 second
            }
        }, 1000);

答案 1 :(得分:1)

看看这段代码,我会这样做:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TestActivity extends Activity {

    TextView timerTextView;
    long startTime = 0;

    //runs without a timer by reposting this handler at the end of the runnable
    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {

        @Override
        public void run() {
            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            timerTextView.setText(String.format("%d:%02d", minutes, seconds));

            timerHandler.postDelayed(this, 1000);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity);

        timerTextView = (TextView) findViewById(R.id.timerTextView);

        Button b = (Button) findViewById(R.id.button);
        b.setText("start");
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Button b = (Button) v;
                if (b.getText().equals("stop")) {
                    timerHandler.removeCallbacks(timerRunnable);
                    b.setText("start");
                } else {
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                    b.setText("stop");
                }
            }
        });
    }

}