EX10_07.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:9k
源码类别:

android开发

开发平台:

Java

  1. package irdc.EX10_07;
  2. import java.util.Date; 
  3. import java.util.Random;
  4. import android.app.Activity; 
  5. import android.content.Context; 
  6. import android.hardware.SensorListener; 
  7. import android.hardware.SensorManager; 
  8. import android.media.MediaPlayer; 
  9. import android.os.Bundle; 
  10. import android.util.Log;
  11. import android.widget.ImageView;
  12. import android.widget.TextView; 
  13. public class EX10_07 extends Activity 
  14.   /*声明变量*/
  15.   private TextView mTextView01; 
  16.   private ImageView mImageView01;
  17.   private SensorManager mSensorManager01;  
  18.   private float velocityFinal = 0; 
  19.   /*声明媒体播放器*/
  20.   private MediaPlayer mMediaPlayer01; 
  21.   private MediaPlayer mMediaPlayer02;
  22.   private MediaPlayer mMediaPlayer03;
  23.   private Date lastUpdateTime; 
  24.   private boolean bIfMakeSound = true; 
  25.   private int godanswer = 0;
  26.   /*声明常数*/
  27.   private final int saint=1;
  28.   private final int laugh=2;
  29.   private final int bad=3;
  30.    
  31.   /** Called when the activity is first created. */ 
  32.   @Override 
  33.   public void onCreate(Bundle savedInstanceState) 
  34.   { 
  35.     super.onCreate(savedInstanceState); 
  36.     setContentView(R.layout.main); 
  37.     /*取得目前系统时间*/ 
  38.     lastUpdateTime = new Date(System.currentTimeMillis()); 
  39.     /*通过findViewById构造器创建TextView与ImageView对象*/
  40.     mTextView01 = (TextView)findViewById(R.id.myTextView1);
  41.     mImageView01 = (ImageView)findViewById(R.id.myImageView1);
  42.     /*取得系统的加速度感测装备*/ 
  43.     mSensorManager01 =  
  44.     (SensorManager)getSystemService(Context.SENSOR_SERVICE); 
  45.     /*通过MediaPlayer.create()来取得raw底下音乐文件*/
  46.     mMediaPlayer01 = new MediaPlayer(); 
  47.     mMediaPlayer01 = MediaPlayer.create(EX10_07.this, R.raw.saint); 
  48.     mMediaPlayer02 = new MediaPlayer(); 
  49.     mMediaPlayer02 = MediaPlayer.create(EX10_07.this, R.raw.laugh); 
  50.     mMediaPlayer03 = new MediaPlayer(); 
  51.     mMediaPlayer03 = MediaPlayer.create(EX10_07.this, R.raw.bad); 
  52.   } 
  53.   /*覆盖onResuem方法*/
  54.   @Override 
  55.   protected void onResume() 
  56.   { 
  57.     // TODO Auto-generated method stub 
  58.     /*注册SensorListener与设置参数*/
  59.     mSensorManager01.registerListener 
  60.     ( 
  61.       mSensorLisener, 
  62.       SensorManager.SENSOR_ACCELEROMETER, 
  63.       SensorManager.SENSOR_DELAY_UI 
  64.     ); 
  65.     super.onResume(); 
  66.   } 
  67.    /*覆盖onPause方法*/
  68.   @Override 
  69.   protected void onPause() 
  70.   { 
  71.     /*解除SensorListener的设置*/
  72.     // TODO Auto-generated method stub 
  73.     mSensorManager01.unregisterListener(mSensorLisener); 
  74.     super.onPause(); 
  75.   }   
  76.   
  77.    /*创建SensorListener对象*/
  78.   private final SensorListener mSensorLisener = new SensorListener()
  79.   { 
  80.     /*声明变量*/
  81.     private float previousAcceleration; 
  82.     /*覆盖onSensorChanged事件来侦测手机投掷事件*/
  83.     @Override 
  84.     public void onSensorChanged(int sensor, float[] values) 
  85.     { 
  86.       // TODO Auto-generated method stub 
  87.       /*加速度是否存在*/
  88.       if(sensor == SensorManager.SENSOR_ACCELEROMETER) 
  89.       { 
  90.         float x = values[SensorManager.DATA_X]; 
  91.         float y = values[SensorManager.DATA_Y]; 
  92.         float z = values[SensorManager.DATA_Z]; 
  93.          /*手机是否落下的速度标准*/
  94.         double forceThreshHold = 1.5f;        
  95.         double appliedAcceleration = 0.0f; 
  96.          
  97.         /* SensorManager.GRAVITY_EARTH = 9.8m/s2 */ 
  98.         appliedAcceleration +=  
  99.         Math.pow(x/SensorManager.GRAVITY_EARTH, 2.0);  
  100.          
  101.         appliedAcceleration +=  
  102.         Math.pow(y/SensorManager.GRAVITY_EARTH, 2.0);  
  103.          
  104.         appliedAcceleration +=  
  105.         Math.pow(z/SensorManager.GRAVITY_EARTH, 2.0);  
  106.          
  107.         appliedAcceleration = Math.sqrt(appliedAcceleration);  
  108.           
  109.         /*判断手机使否落下的判断式
  110.          * 落地当时速度需小于1.5f, 落地前速度必须大于1.5f*/
  111.         if((appliedAcceleration < forceThreshHold) &&  
  112.            (previousAcceleration > forceThreshHold)) 
  113.         { 
  114.           /* Shake手机事件触发 */ 
  115.           Date timeNow = new Date(System.currentTimeMillis()); 
  116.            
  117.           /* 从初速度到末速度所经过的时间 */ 
  118.           long timePeriod =  
  119.           (long)timeNow.getTime() - (long)lastUpdateTime.getTime(); 
  120.            
  121.           /* (v) = a*t */ 
  122.           velocityFinal =  
  123.           (float) (appliedAcceleration * ((float)timePeriod/1000)); 
  124.           Log.i("V=",Float.toString(velocityFinal));
  125.           if(bIfMakeSound==true) 
  126.           { 
  127.             /*Random Number生成器*/
  128.             Random generator=new Random();
  129.             /*掷筊*/
  130.             godanswer = generator.nextInt(3) + 1;
  131.             
  132.             switch (godanswer)
  133.             {
  134.              /*圣筊*/
  135.             case saint:
  136.               mTextView01.setText 
  137.               ( 
  138.                 getResources().getText(R.string.str_saint).toString() 
  139.               ); 
  140.               mImageView01.setImageDrawable(getResources().
  141.               getDrawable(R.drawable.saint));
  142.               if (mMediaPlayer01 != null) 
  143.               { 
  144.                 try 
  145.                 { 
  146.                   mMediaPlayer01.seekTo(0); 
  147.                   mMediaPlayer01.stop(); 
  148.                   mMediaPlayer01.prepare(); 
  149.                   mMediaPlayer01.setVolume(10, 10);
  150.                   mMediaPlayer01.start(); 
  151.                 } 
  152.                 catch(Exception e) 
  153.                 { 
  154.                   mTextView01.setText(e.toString()); 
  155.                   e.printStackTrace(); 
  156.                 } 
  157.               } 
  158.               else 
  159.               { 
  160.                 try 
  161.                 { 
  162.                   mMediaPlayer01.prepare(); 
  163.                   mMediaPlayer01.setVolume(10, 10);
  164.                   mMediaPlayer01.start(); 
  165.                 } 
  166.                 catch(Exception e) 
  167.                 { 
  168.                   mTextView01.setText(e.toString()); 
  169.                   e.printStackTrace(); 
  170.                 } 
  171.               }
  172.               break;
  173.             /*笑筊*/
  174.             case laugh:
  175.               mTextView01.setText 
  176.               ( 
  177.                 getResources().getText(R.string.str_laugh).toString() 
  178.               ); 
  179.               mImageView01.setImageDrawable(getResources().
  180.               getDrawable(R.drawable.laugh));
  181.               if (mMediaPlayer02 != null) 
  182.               { 
  183.                 try 
  184.                 { 
  185.                   mMediaPlayer02.seekTo(0); 
  186.                   mMediaPlayer02.stop(); 
  187.                   mMediaPlayer02.prepare(); 
  188.                   mMediaPlayer02.setVolume(10, 10);
  189.                   mMediaPlayer02.start(); 
  190.                 } 
  191.                 catch(Exception e) 
  192.                 { 
  193.                   mTextView01.setText(e.toString()); 
  194.                   e.printStackTrace(); 
  195.                 } 
  196.               } 
  197.               else 
  198.               { 
  199.                 try 
  200.                 { 
  201.                   mMediaPlayer02.prepare(); 
  202.                   mMediaPlayer02.setVolume(10, 10);
  203.                   mMediaPlayer02.start(); 
  204.                 } 
  205.                 catch(Exception e) 
  206.                 { 
  207.                   mTextView01.setText(e.toString()); 
  208.                   e.printStackTrace(); 
  209.                 } 
  210.               }
  211.               break;
  212.               /*阴筊*/
  213.             case bad:
  214.               mTextView01.setText 
  215.               ( 
  216.                 getResources().getText(R.string.str_bad).toString() 
  217.               ); 
  218.               mImageView01.setImageDrawable(getResources().
  219.               getDrawable(R.drawable.bad));
  220.               if (mMediaPlayer03 != null) 
  221.               { 
  222.                 try 
  223.                 { 
  224.                   mMediaPlayer03.seekTo(0); 
  225.                   mMediaPlayer03.stop(); 
  226.                   mMediaPlayer03.prepare(); 
  227.                   mMediaPlayer03.setVolume(10, 10);
  228.                   mMediaPlayer03.start(); 
  229.                 } 
  230.                 catch(Exception e) 
  231.                 { 
  232.                   mTextView01.setText(e.toString()); 
  233.                   e.printStackTrace(); 
  234.                 } 
  235.               } 
  236.               else 
  237.               { 
  238.                 try 
  239.                 { 
  240.                   mMediaPlayer03.prepare(); 
  241.                   mMediaPlayer03.setVolume(10, 10);
  242.                   mMediaPlayer03.start(); 
  243.                 } 
  244.                 catch(Exception e) 
  245.                 { 
  246.                   mTextView01.setText(e.toString()); 
  247.                   e.printStackTrace(); 
  248.                 } 
  249.               }
  250.               break;
  251.             }
  252.           }
  253.         }
  254.         else 
  255.         { 
  256.           /* 末速度=0 */ 
  257.           /* 更新lastUpdateTime为现在 */ 
  258.           Date timeNow = new Date(System.currentTimeMillis()); 
  259.           lastUpdateTime.setTime(timeNow.getTime()); 
  260.         } 
  261.         previousAcceleration = (float) appliedAcceleration; 
  262.       } 
  263.     } 
  264.      
  265.     @Override 
  266.     public void onAccuracyChanged(int sensor, int accuracy) 
  267.     { 
  268.       // TODO Auto-generated method stub    
  269.     }     
  270.   }; 
  271. }