android手機震動

Vibrator是安卓提供的震動器,其沒有構造器,經過getSystemService(Context.VIBRATOR_SERVICE)方法獲取對象。但使用此類時須要在清單文件中添加訪問權限android.permission.VIBRATE.在實際使用能夠設置震動週期已經訪問時間android

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();
            }
        });
    }

}
相關文章
相關標籤/搜索