手机震动效果

在android中,可以控制手机进行震动,先是在配置文件中对权限进行配置:
<uses-permission android:name="android.permission.VIBRATE" />

简单示例:
主程序中,判断 ToggleButton 是否有被启动,如果单击“ ON ”的话,就会启动震动模式,并会跳出 Toast 文字;如果再一次单击“ OFF ”,就会把震动关掉。

public class EX05 _ 06 extends Activity

{

  private Vibrator mVibrator01;

 

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState)

  {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main); 

 

    /* 设置 ToggleButton 的对象 */

    mVibrator01 = ( Vibrator )getApplication().getSystemService

    (Service.VIBRATOR _ SERVICE);

 

    final ToggleButton mtogglebutton1 =

    (ToggleButton) findViewById(R.id.myTogglebutton1);

 

    final ToggleButton mtogglebutton2 =

    (ToggleButton) findViewById(R.id.myTogglebutton2);

 

    final ToggleButton mtogglebutton3 =

    (ToggleButton) findViewById(R.id.myTogglebutton3);

 

    /* 短震动 */

    mtogglebutton1.setOnClickListener(new OnClickListener()

    {

      public void onClick(View v)

      {

        if (mtogglebutton1.isChecked())

        {

          /* 设置震动的周期 */

          mVibrator01.vibrate( new long[]{100,10,100,1000},-1);

          /* 用 Toast 显示震动启动 */

          Toast.makeText

          (

            EX05 _ 06.this,

             getString(R.string.str _ ok),

            Toast.LENGTH _ SHORT

          ).show();

        }

        else

        {

          /* 取消震动 */

          mVibrator01.cancel();  

          /* 用 Toast 显示震动已被取消 */

          Toast.makeText

          (

            EX05 _ 06.this,

            getString(R.string.str _ end),

            Toast.LENGTH _ SHORT

          ).show();

        }

      }

    });

 

    /* 长震动 */

    mtogglebutton2.setOnClickListener(new OnClickListener()

    {

      public void onClick(View v)

       {

        if (mtogglebutton2.isChecked())

        {

          /* 设置震动的周期 */

          mVibrator01.vibrate(new long[]{100,100,100,1000},0);

       

          /* 用 Toast 显示震动启动 */

          Toast.makeText

          (

            EX05 _ 06.this,

            getString(R.string.str _ ok),

            Toast.LENGTH _ SHORT

          ).show();

        }

        else

        {

          /* 取消震动 */

          mVibrator01.cancel();

         

          /* 用 Toast 显示震动取消 */

          Toast.makeText

          (

            EX05 _ 06.this,

            getString(R.string.str _ end),

            Toast.LENGTH _ SHORT

          ).show();

        }

      }

    }); 

   

    /* 节奏震动 */

    mtogglebutton3.setOnClickListener(new OnClickListener()

    {

      public void onClick(View v)

      {

         if (mtogglebutton3.isChecked())

        {

          /* 设置震动的周期 */

          mVibrator01.vibrate( new long[]{1000,50,1000,50,1000},0);

       

          /* 用 Toast 显示震动启动 */

          Toast.makeText

          (

            EX05 _ 06.this, getString(R.string.str _ ok),

            Toast.LENGTH _ SHORT

          ).show();

        }

        else

        {

          /* 取消震动 */

          mVibrator01.cancel();

          /* 用 Toast 显示震动取消 */

          Toast.makeText

          (

            EX05 _ 06.this,

            getString(R.string.str _ end),

            Toast.LENGTH _ SHORT

          ).show();

        }

      }

    });

  }

}

 

你可能感兴趣的:(vibrate)