Android_Android系统服务—Vibrator(振动器)

Android_Android系统服务—Vibrator(振动器)_第1张图片

一个简单的振动器Demo

界面:

Android_Android系统服务—Vibrator(振动器)_第2张图片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Vibrator(振动器)"
        android:textSize="23sp" />

    <Button
        android:id="@+id/bt_short"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="短振动" />

    <Button
        android:id="@+id/bt_long"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="长振动" />

    <Button
        android:id="@+id/bt_cadence"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="节奏振动" />

    <Button
        android:id="@+id/bt_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消振动" />

</LinearLayout>

package com.liangdianshui.vibrator;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.security.Provider;


public class MainActivity extends Activity implements View.OnClickListener {

    private Button bt_short;
    private Button bt_long;
    private Button bt_cadence;
    private Button bt_cancel;
    private Vibrator vibrator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

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

    }

    /**
     * 控件的初始化
     */
    private void init() {
        bt_short = (Button) findViewById(R.id.bt_short);
        bt_long = (Button) findViewById(R.id.bt_long);
        bt_cadence = (Button) findViewById(R.id.bt_cadence);
        bt_cancel = (Button) findViewById(R.id.bt_cancel);
        bt_short.setOnClickListener(this);
        bt_long.setOnClickListener(this);
        bt_cadence.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_short:
                vibrator.cancel();
                vibrator.vibrate(new long[]{100, 200, 100, 200}, 0);
                Toast.makeText(getApplicationContext(), "短振动", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_long:
                vibrator.cancel();
                vibrator.vibrate(new long[]{100, 100, 100, 1000}, 0);
                Toast.makeText(getApplicationContext(), "长振动", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_cadence:
                vibrator.cancel();
                vibrator.vibrate(new long[]{500, 100, 500, 100, 500, 100}, 0);
                Toast.makeText(getApplicationContext(), "节奏振动", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_cancel:
                vibrator.cancel();
                Toast.makeText(getApplicationContext(), "取消振动", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

最后记得加权限:

 <uses-permission android:name="android.permission.VIBRATE"></uses-permission>



你可能感兴趣的:(Android_Android系统服务—Vibrator(振动器))