Android的:如何创建一个定时器1秒 [英] Android: how can i create a timer for 1 second

查看:174
本文介绍了Android的:如何创建一个定时器1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在开发一个应用程序andoird这是本场比赛西门,我遇到了一个问题,当我从菜单中移动游戏。
我想本场比赛将是随机选择的游戏玩beggining 3声音
和应用程序发挥他们1相继在1秒的延迟,但问题是应用程序游戏backgound装载前的声音在同一时间都打..不久,一切都不起作用,我desprate。
为了使用按钮,我创建一类称为柱含有从视图,它使与使用的方法发挥激活声音和改变图像的声音的按钮。
类游戏的游戏逻辑
这里是code:

列类:

 包com.example.thegreatsimon;
进口android.content.Context;
进口android.content.Intent;
进口android.content.res.AssetManager;
进口android.media.AudioManager;
进口android.media.MediaPlayer;
进口android.media.SoundPool;
进口android.os.Handler;
进口android.os.SystemClock;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageButton;公共类列{的ImageButton按钮;
声音的Soundpool;
INT soundId;
INT srcColor,lightColor;
上下文CON;
公开栏(ImageButton的按钮,上下文CON)
{
    声音=新的Soundpool(10,AudioManager.STREAM_MUSIC,0);
    this.button =按钮;
    开关(button.getId())
    {
    案例R.id.ibBlue:
    {
        soundId = sound.load(CON,R.raw.a,1);
        srcColor = R.drawable.blue;
        lightColor = R.drawable.lightblue;
    }打破;
    案例R.id.ibGreen:
    {
        soundId = sound.load(CON,R.raw.b,1);
        srcColor = R.drawable.green;
        lightColor = R.drawable.lightgreen;
    }打破;
    案例R.id.ibRed:
    {
        soundId = sound.load(CON,R.raw.c,1);
        srcColor = R.drawable.red;
        lightColor = R.drawable.lightred;
    }打破;
    案例R.id.ibYellow:
    {
        soundId = sound.load(CON,R.raw.d,1);
        srcColor = R.drawable.yellow;
        lightColor = R.drawable.lightyellow;
    }打破;
    }
    button.setOnClickListener(新监听());
}一流的聆听工具OnClickListener
{
    @覆盖
    公共无效的onClick(最终查看V)
    {
        玩();
    }
}无效播放()
{
    sound.play(soundId,1,1,0,0,1);
    button.setBackgroundResource(lightColor);
    处理程序处理程序=新的处理程序();
    handler.postDelayed(新的Runnable()
    {
        @覆盖
        公共无效的run()
        {
            button.setBackgroundResource(srcColor);
        }
    },300);}}

Game类:

 包com.example.thegreatsimon;
进口的java.util.ArrayList;
进口了java.util.Random;
进口java.util.Timer中;
进口java.util.TimerTask中;
进口android.media.AudioManager;
进口android.media.MediaPlayer;
进口android.media.SoundPool;
进口android.os.Bundle;
进口android.os.Handler;
进口android.os.SystemClock;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.view.Menu;
进口android.widget.Button;
进口android.widget.ImageButton;公共类游戏延伸活动{列bYellow,育成,bGreen,bBlue;
的Soundpool一个=新的Soundpool(10,AudioManager.STREAM_MUSIC,0);
INT差异;
INT生活= 3;
随机兰特=新的随机();
处理器手=新的处理程序();
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    差异= Settings.difficulty;
    开关(差异)
    {
    情况下(0):
        的setContentView(R.layout.gameeasy);
    打破;
    情况1):
        的setContentView(R.layout.game);
    bYellow =新柱((的ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
    打破;
    情况(2):
        的setContentView(R.layout.game);
    bYellow =新柱((的ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
    打破;
    }
    BRED =新柱((的ImageButton)findViewById(R.id.ibRed),getApplicationContext());
    bGreen =新柱((的ImageButton)findViewById(R.id.ibGreen),getApplicationContext());
    bBlue =新柱((的ImageButton)findViewById(R.id.ibBlue),getApplicationContext());
    EasyGame();
}
公共布尔EasyGame()
{
    INT R;
    ArrayList的列表=新的ArrayList();
    布尔前进;
    的for(int i = 0;我3;;我++)
    {
        R = rand.nextInt(3);
        开关(r)的
        {
        情况下0:
        {
            list.add(育成);
            睡眠(育成);
        }打破;
        情况1:
        {
            list.add(bGreen);
            睡眠(bGreen);
        }打破;
        案例2:
        {
            list.add(bBlue);
            睡眠(bBlue);
        }打破;
        }
    }
    继续= UserPlay();    / *的for(int i = 0;我2;我++)
    {
        R = rand.nextInt(3);
        开关(r)的
        {
        情况下0:
            list.add(育成);
            打破;
        情况1:
            list.add(bGreen);
            打破;
        案例2:
            list.add(bBlue);
            打破;
        }
        对于(INT J = 0; J<则为list.size(); J ++)
        {
            ((柱)list.get(J))播放()。
            睡觉();
        }
        继续= UserPlay();
    } * /    返回true;
}公共无效MediumGame()
{}
公共无效HardGame()
{}    公共无效睡眠(最终列C)
{
    处理程序处理程序=新的处理程序();
    handler.postDelayed(新的Runnable(){
    @覆盖
                公共无效的run(){
                   c.play();
                }
            },1000);
}公共布尔UserPlay()
{
    返回true;
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}}


解决方案

看这个code,我会做这样:

 进口android.app.Activity;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;公共类测试活动扩展了活动{    TextView的timerTextView;
    长STARTTIME = 0;    //没有计时器通过在可运行的结束重新发布此处理程序运行
    处理器timerHandler =新的处理程序();
    可运行timerRunnable =新的Runnable(){        @覆盖
        公共无效的run(){
            长米利斯= System.currentTimeMillis的() - startTime时;
            INT秒=(INT)(米利斯/ 1000);
            INT分钟=秒/ 60;
            秒秒= 60%;            timerTextView.setText(的String.format(%d个:%02D,分,秒));            timerHandler.postDelayed(这一点,1000);
        }
    };    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.test_activity);        timerTextView =(的TextView)findViewById(R.id.timerTextView);        按钮B =(按钮)findViewById(R.id.button);
        b.setText(开始);
        b.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                按钮B =(按钮)V;
                如果(b.getText()。等于(停止)){
                    timerHandler.removeCallbacks(timerRunnable);
                    b.setText(开始);
                }其他{
                    STARTTIME = System.currentTimeMillis的();
                    timerHandler.postDelayed(timerRunnable,0);
                    b.setText(停止);
                }
            }
        });
    }}

ok i am developing an andoird app which is the game simon, i encountered a problem that when i move from the menu to the game. i want the game will play 3 sounds at the beggining of the game which is selected randomly and the app to play them 1 after another in 1 sec delay, but the problem is the app before the game backgound is loaded the sounds all play at the same time.. shortly everything doesn't work and i am desprate. in order to use the buttons, i created a class called column which contains the button from the view and the sound it makes and use the method play to activate the sound and change the image. the class Game for the logic of the game here is the code:

Column Class:

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);

}

}   

The Game Class:

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

}

解决方案

Look this code, i would have done so:

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");
                }
            }
        });
    }

}

这篇关于Android的:如何创建一个定时器1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆