android手机震动

Vibrator是安卓提供的震动器,其没有构造器,通过getSystemService(Context.VIBRATOR_SERVICE)方法获取对象。但使用此类时需要在清单文件中添加访问权限android.permission.VIBRATE.在实际使用可以设置震动周期已经访问时间

package com.basillee.asus.demo;



import android.content.Context;

import android.os.Vibrator;

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;





public class MainActivity10 extends ActionBarActivity {



    Button shortButton=null;

    Button longButton=null;

    Button stopButton=null;

    Vibrator vibrator=null;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_activity10);

        shortButton= (Button) findViewById(R.id.short_vibrate);

        longButton= (Button) findViewById(R.id.long_vibrate);

        stopButton= (Button) findViewById(R.id.stop_vibrate);

        vibrator= (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);



        shortButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

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

            }

        });



        longButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                vibrator.vibrate(new long[]{1000,3000,1000,3000},-1);

            }

        });



        stopButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                vibrator.cancel();

            }

        });

    }



}


 

你可能感兴趣的:(android)