`

手机震动的节奏

 
阅读更多

 手机震动可以是一种提醒或是替换铃声的事件,如果想要让手机乖乖的震动,需要创建Vibrator对象通过Vibrator方法来实现达到震动的模式,在Vibrator的构造方法中有四个参数前三个值是设置震动的大小,可以把数值改成一大一小这样就可以明显感觉到震动的差异,而最后一个值是震动的时间,  Repeat=0时,震动会一直持续repeat=-1震动只会出现一轮,运动完毕后就不再震动

判断ToggleButton是否被开启。如果单击就是“ON”就会启动震动模式如果点击OFF就会关闭振动模式

先定义一个Activity

 

package cn.hwttnet.com.ui;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class EX05_06Activity extends Activity {

	private Vibrator vbt;
	private ToggleButton tb1, tb2, tb3;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		vbt = (Vibrator) getApplication().getSystemService(
				Service.VIBRATOR_SERVICE);
		final ToggleButton tb1 = (ToggleButton) findViewById(R.id.toggleButton1);
		final ToggleButton tb2 = (ToggleButton) findViewById(R.id.toggleButton2);
		final ToggleButton tb3 = (ToggleButton) findViewById(R.id.toggleButton3);
		//短震动
		tb1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (tb1.isChecked()) {

					vbt.vibrate(new long[] { 100, 10, 100, 1000 }, -1);
					Toast.makeText(getApplicationContext(), "震动开启",
							Toast.LENGTH_LONG).show();
				} else {
					vbt.cancel();
					Toast.makeText(getApplicationContext(), "震动取消",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		//长震动
		tb2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (tb2.isChecked()) {

					vbt.vibrate(new long[] { 100, 100, 100, 1000 }, 0);
					Toast.makeText(getApplicationContext(), "震动开启",
							Toast.LENGTH_LONG).show();
				} else {
					vbt.cancel();
					Toast.makeText(getApplicationContext(), "震动取消",
							Toast.LENGTH_LONG).show();
				}
			}
		});
		//节奏震动
		tb3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (tb3.isChecked()) {

					vbt.vibrate(new long[] { 1000, 50, 1000, 50,1000 }, 0);
					Toast.makeText(getApplicationContext(), "震动开启",
							Toast.LENGTH_LONG).show();
				} else {
					vbt.cancel();
					Toast.makeText(getApplicationContext(), "震动取消",
							Toast.LENGTH_LONG).show();
				}
			}
		});
	}
}

 

定义一个main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:collapseColumns="3"
        android:stretchColumns="1" >

        <TableRow
            android:id="@+id/tablerow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <ToggleButton
                android:id="@+id/toggleButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="短震动" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <ToggleButton
                android:id="@+id/toggleButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="长震动" />
        </TableRow>

        <TableRow
            android:id="@+id/tablerow3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" >

            <ToggleButton
                android:id="@+id/toggleButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="节奏震动" />
        </TableRow>
    </TableLayout>

</LinearLayout>

 

然后震动事件必须允许android .permission.VIBARTE权限

<uses-permission android:name="android.permission.VIBRATE"/>
试着把震动的大小做多变化的改变在long[]里面多做一些变化

tb3.vibrate(new long[]{10000,500,10000,500,10000,500,10000},0);

分享到:
评论

相关推荐

    android手机震动节奏

    Vivrator--android手机震动的节奏.(转)

    android 手机震动的节奏 Vibrator

    android 手机震动的节奏 Vibrator

    google android sdk开发范例大全 第二版 PDF 光盘代码

     5.6 手机震动的节奏   5.7 图文可视化提醒   5.8 状态栏的图标与文字提醒   5.9 搜索手机通讯录自动完成   5.10 取得联系人资料   5.11 制作有图标的文件资源 .管.理. 器   5.12 还原...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用...

    Google Android SDK开发范例大全(完整版)

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——使用...

    Google Android SDK开发范例大全的目录

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用-p178 5.9 搜索手机通讯录自动完成——...

    Google+Android+SDK开发范例大全

    5.3 自制发送短信程序——SmsManager与PendingIntent对象 5.4 自制发送Email程序——Intent在Email上的使用 5.5 自制日历手机数据库——实现SQLiteOpenHelper 5.6 手机震动的节奏——Vibrator对象及周期运用 ...

    Google Android SDK开发范例大全(第3版) 1/5

    完整的手机数据存取功能:铃声模式设置、震动控制、WiFi服务、屏幕旋转、电池计量、温度测量、电信网络信息、SIM卡信息、拨打电话、短信解析、通讯录联系人、电子罗盘、屏幕手写等手机控制功能。 系统服务及研发的...

    Google Android SDK开发范例大全(第3版) 4/5

    完整的手机数据存取功能:铃声模式设置、震动控制、WiFi服务、屏幕旋转、电池计量、温度测量、电信网络信息、SIM卡信息、拨打电话、短信解析、通讯录联系人、电子罗盘、屏幕手写等手机控制功能。 系统服务及研发的...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用 5.9 搜索手机通讯录自动完成——...

    Google Android SDK开发范例大全(第3版) 3/5

    完整的手机数据存取功能:铃声模式设置、震动控制、WiFi服务、屏幕旋转、电池计量、温度测量、电信网络信息、SIM卡信息、拨打电话、短信解析、通讯录联系人、电子罗盘、屏幕手写等手机控制功能。 系统服务及研发的...

    Google Android SDK开发范例大全(第3版) 5/5

    完整的手机数据存取功能:铃声模式设置、震动控制、WiFi服务、屏幕旋转、电池计量、温度测量、电信网络信息、SIM卡信息、拨打电话、短信解析、通讯录联系人、电子罗盘、屏幕手写等手机控制功能。 系统服务及研发的...

    Google Android sdk 开发范例大全 部分章节代码

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用-p178 5.9 搜索手机通讯录自动完成——...

    Google Android SDK 开发范例大全01

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用-p178 5.9 搜索手机通讯录自动完成——...

    Google Android SDK 开发范例大全02

    5.6 手机震动的节奏——Vibrator对象及周期运用 5.7 图文可视化提醒——Toast与LinearLayoutView 5.8 状态栏的图标与文字提醒——NotificationManager与Notification对象的应用-p178 5.9 搜索手机通讯录自动完成——...

Global site tag (gtag.js) - Google Analytics