织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

android timer与handler的用法体会

落鹤生 发布于 2014-02-06 20:42 点击:次 
最近在做一个关于应用程序叫BATTLE FARTS,呵呵,和放屁有关的项目,大体就是根据录进去的声音的大小和时长,通过一个算法,算出分数。里面要实现一个开机动画自动跳转。因为给了三张图 片,要实现闪烁的功能,所以我就想到了FRAME ANIMATION。
TAG: 定时器  Timer  

最近在做一个关于应用程序叫BATTLE FARTS,呵呵,和放屁有关的项目,大体就是根据录进去的声音的大小和时长,通过一个算法,算出分数。里面要实现一个开机动画自动跳转。因为给了三张图 片,要实现闪烁的功能,所以我就想到了FRAME ANIMATION。之前没用过,但是API文档里说了,start()方法不能写在ONCREATE()方法里,因为在ONCREATE()里还没有和 IMAGEVIEW绑定,所以只能写在拖拽等事件中。无奈,只能写在触发事件里写。但是,原来的程序是要实现开机自动运行动画的。技术高手可以指点一下 我。然后动画运行完,画面要跳转,想了很多方法,没有找到在哪里写。后来想到了,可以用一个定时器,于是查了一些资料,说是用TIMER和 HANDLER.写惯了JAVA的人都想直接用TIMER,但是其实在ANDROID里是不行的,这和ANDROID的安全性有关。只能用TIMER与 HANDLER实现,我要实现的是,五秒后自动,跳到下一个ACTIVITY.代码如下:

  1. package com.battlefart; 
  2. import android.app.Activity; 
  3.  
  4. import java.util.Timer;    
  5. import java.util.TimerTask; 
  6. import android.content.Intent; 
  7. import android.content.SharedPreferences; 
  8. import android.graphics.drawable.AnimationDrawable; 
  9. import android.view.KeyEvent; 
  10. import android.os.Bundle; 
  11. import android.widget.ImageView; 
  12. import android.os.Handler;    
  13. import android.os.Message; 
  14. public class Bf_mech_lpener_1 extends Activity { 
  15.     private ImageView OpenFlash; 
  16.     private String strPlayerName; 
  17.     private AnimationDrawable Open_animation; 
  18.        
  19.            
  20.             Timer timer = new Timer();    
  21.             Handler handler = new Handler(){    
  22.            
  23.                public void handleMessage(Message msg) {    
  24.                    switch (msg.what) {        
  25.                    case 1:        
  26. Intent intent=new Intent(); 
  27. intent.setClass(Bf_mech_lpener_1.this, bf_mech_1_1opener.class); 
  28. startActivity(intent); 
  29. Bf_mech_lpener_1.this.finish();    
  30. break;        
  31.                     }        
  32.                     super.handleMessage(msg);    
  33.            }    
  34.                     
  35.             };    
  36.             TimerTask task = new TimerTask(){    
  37.            
  38.                 public void run() {    
  39.                     Message message = new Message();        
  40.                     message.what = 1;        
  41.                     handler.sendMessage(message);      
  42.                 }    
  43.                     
  44.            };    
  45.     
  46.     @Override 
  47.     public void onCreate(Bundle savedInstanceState) 
  48.     { 
  49. super.onCreate(savedInstanceState); 
  50. SharedPreferences sp = getSharedPreferences("player_information",0); 
  51. strPlayerName=sp.getString("Player_name"""); 
  52. if(strPlayerName.equals("")) 
  53.         { 
  54.             setContentView(R.layout.bf_mech_opener_one); 
  55.             OpenFlash = (ImageView) findViewById(R.id.bf_mech_open_light1);   
  56.             OpenFlash.setBackgroundResource(R.anim.open_flash);  
  57.             Open_animation = (AnimationDrawable) OpenFlash.getBackground(); 
  58.             //5秒钟后自动跳到下一个页面timer.schedule(task, 5000);    
  59.              
  60.         } 
  61.         else { 
  62.             Intent intent=new Intent(Bf_mech_lpener_1.this,Home_2.class); 
  63.             startActivity(intent); 
  64.             Bf_mech_lpener_1.this.finish();    
  65.         } 
  66.          
  67.     } 
  68.     //按向上键,启动动画 
  69.     public boolean onKeyUp(int KeyCode,KeyEvent event) 
  70.     { 
  71.         switch(KeyCode) 
  72.         { 
  73.           case KeyEvent.KEYCODE_DPAD_UP: 
  74.                Open_animation.start(); 
  75.                break;        
  76.         } 
  77.         return true
  78.          
  79.     } 

 

(tquding)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201402/16895.html]
本文出处:CSDN博客 作者:tquding 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
相关文章
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容