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

android开发

开发平台:

Java

  1. package irdc.ex05_06;
  2. import android.app.Activity;
  3. import android.app.Service;
  4. import android.os.Bundle;
  5. import android.os.Vibrator;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Toast;
  9. import android.widget.ToggleButton;
  10. public class EX05_06 extends Activity 
  11. {
  12.   private Vibrator mVibrator01;
  13.   /** Called when the activity is first created. */
  14.   @Override
  15.   public void onCreate(Bundle savedInstanceState)
  16.   {
  17.     super.onCreate(savedInstanceState);
  18.     setContentView(R.layout.main);  
  19.   
  20.     /*设置ToggleButton的对象*/
  21.     mVibrator01 = ( Vibrator )getApplication().getSystemService
  22.     (Service.VIBRATOR_SERVICE);
  23.   
  24.     final ToggleButton mtogglebutton1 = 
  25.     (ToggleButton) findViewById(R.id.myTogglebutton1);
  26.   
  27.     final ToggleButton mtogglebutton2 = 
  28.     (ToggleButton) findViewById(R.id.myTogglebutton2);
  29.   
  30.     final ToggleButton mtogglebutton3 =
  31.     (ToggleButton) findViewById(R.id.myTogglebutton3);
  32.   
  33.     /* 短震动 */
  34.     mtogglebutton1.setOnClickListener(new OnClickListener()
  35.     {
  36.       public void onClick(View v) 
  37.       {
  38.         if (mtogglebutton1.isChecked())
  39.         {
  40.           /* 设置震动的周期 */
  41.           mVibrator01.vibrate( new long[]{100,10,100,1000},-1);
  42.           /*用Toast显示震动启动*/
  43.           Toast.makeText
  44.           (
  45.             EX05_06.this,
  46.             getString(R.string.str_ok),
  47.             Toast.LENGTH_SHORT
  48.           ).show();
  49.         }
  50.         else
  51.         {
  52.           /* 取消震动 */
  53.           mVibrator01.cancel();   
  54.           /*用Toast显示震动已被取消*/
  55.           Toast.makeText
  56.           (
  57.             EX05_06.this,
  58.             getString(R.string.str_end),
  59.             Toast.LENGTH_SHORT
  60.           ).show();
  61.         } 
  62.       }
  63.     });
  64.   
  65.     /* 长震动 */
  66.     mtogglebutton2.setOnClickListener(new OnClickListener()
  67.     {
  68.       public void onClick(View v) 
  69.       {
  70.         if (mtogglebutton2.isChecked())
  71.         {
  72.           /*设置震动的周期*/
  73.           mVibrator01.vibrate(new long[]{100,100,100,1000},0);
  74.         
  75.           /*用Toast显示震动启动*/
  76.           Toast.makeText
  77.           (
  78.             EX05_06.this,
  79.             getString(R.string.str_ok),
  80.             Toast.LENGTH_SHORT
  81.           ).show();
  82.         }
  83.         else
  84.         {
  85.           /* 取消震动 */
  86.           mVibrator01.cancel();
  87.           
  88.           /* 用Toast显示震动取消 */
  89.           Toast.makeText
  90.           (
  91.             EX05_06.this,
  92.             getString(R.string.str_end),
  93.             Toast.LENGTH_SHORT
  94.           ).show();
  95.         } 
  96.       }
  97.     });  
  98.     
  99.     /* 节奏震动 */
  100.     mtogglebutton3.setOnClickListener(new OnClickListener()
  101.     {
  102.       public void onClick(View v) 
  103.       {
  104.         if (mtogglebutton3.isChecked())
  105.         {
  106.           /* 设置震动的周期 */
  107.           mVibrator01.vibrate( new long[]{1000,50,1000,50,1000},0);
  108.         
  109.           /*用Toast显示震动启动*/
  110.           Toast.makeText
  111.           (
  112.             EX05_06.this, getString(R.string.str_ok),
  113.             Toast.LENGTH_SHORT
  114.           ).show();
  115.         }
  116.         else
  117.         {
  118.           /* 取消震动 */
  119.           mVibrator01.cancel();
  120.           /* 用Toast显示震动取消 */
  121.           Toast.makeText
  122.           (
  123.             EX05_06.this,
  124.             getString(R.string.str_end),
  125.             Toast.LENGTH_SHORT
  126.           ).show();
  127.         } 
  128.       }
  129.     });
  130.   }
  131. }