手机的震动功能相信大家都不会陌生,现在就让我们解读手机的震动。
其实,要实现手机的震动并不难,只需要实现一个类,并调用其中的方法,设定相应的参数即可。
下面给出介绍:
这段文档来自Google SDK文档
Class that operates the vibrator on the device.
If your process exits, any vibration you started with will stop.
To obtain an instance of the system vibrator, callgetSystemService(String)
withVIBRATOR_SERVICE
as
argument.
我们通常实现的方法如下:
public abstract voidvibrate(long[] pattern, int repeat)
Vibrate with a given pattern.
Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator
on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.
This method requires the caller to hold the permissionVIBRATE
.
Parameters
pattern
an array of longs of times for which to turn the vibrator on or off.
|
repeat
the index into pattern at which to repeat, or -1 if you don't want to repeat.
|
下面给出一个具体的实例实现震动效果(需要在真机上运行)
1.布局文件的代码
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 建立第一個TextView -->
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<!-- 建立第二個TextView -->
<TextView
android:id="@+id/myTextView2"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="33px"
android:text="@string/str_text1"
/>
<!-- 建立第三個TextView -->
<TextView
android:id="@+id/myTextView3"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="115px"
android:text="@string/str_text2"
/>
<!-- 建立第四個TextView -->
<TextView
android:id="@+id/myTextView4"
android:layout_width="127px"
android:layout_height="35px"
android:layout_x="90px"
android:layout_y="216px"
android:text="@string/str_text3"
/>
<!-- 建立第一個ToggleButton -->
<ToggleButton
android:id="@+id/myTogglebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="31px"
/>
<!-- 建立第二個ToggleButton -->
<ToggleButton
android:id="@+id/myTogglebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="114px"
/>
<!-- 建立第三個ToggleButton -->
<ToggleButton
android:id="@+id/myTogglebutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="29px"
android:layout_y="214px"
/>
</AbsoluteLayout>
2.主程序的代码
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);
/*设定ToggleButton使用OnClickListener来启动事件*/
mtogglebutton1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (mtogglebutton1.isChecked())
{
// public abstract void vibrate (long[] pattern, int repeat)
// Added in API level 1
// Vibrate with a given pattern.
//
// Pass in an array of ints that are the durations for which to turn on or off the vibrator in milliseconds. The first value indicates the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds for which to keep the vibrator on before turning it off. Subsequent values alternate between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
//
// To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.
//
// This method requires the caller to hold the permission VIBRATE.
//
// Parameters
// pattern an array of longs of times for which to turn the vibrator on or off.
// repeat the index into pattern at which to repeat, or -1 if you don't want to repeat.
/*设定震动的周期*/
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();
}
}
});
/*设定ToggleButton使用OnClickListener来启动事件*/
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();
}
}
});
}
}
分享到:
相关推荐
这个压缩包"Android应用源码-实现Vibrator测试功能,短震动,长震动,频率震动,取消震动.zip"包含了一个示例应用,它详细展示了如何利用Vibrator类的各种方法来控制手机或平板电脑的振动效果。现在我们将深入探讨...
这个Demo展示了如何使用`Vibrator`来实现手机的震动效果。在移动应用开发中,震动功能常常被用作用户反馈,例如在游戏中提示玩家或者在接收通知时提供触觉反馈。 首先,我们需要导入必要的Android SDK库,这通常在...
综上所述,Android的`Vibrator`服务是实现手机震动的关键。通过获取`Vibrator`实例,调用其提供的方法,结合适当的权限管理,开发者可以创建出具有丰富触觉反馈的应用程序。理解并熟练运用这些知识点,将使你的应用...
总之,在Android中实现手机震动效果,主要依赖于Vibrator类,包括获取Vibrator服务、设置震动时长或模式,以及停止震动。这为开发者提供了丰富的可能性,可以根据应用场景定制各种不同的震动反馈,提升用户体验。...
安卓摇一摇传感器相关-...实现震动效果涉及到获取Vibrator服务实例、判断设备是否有震动能力、设置单次震动和震动模式等功能。合理的使用震动效果,配合良好的用户体验设计,是提升安卓应用品质的一个重要方面。
总之,Android的Vibrator类为我们提供了方便的方式来实现手机震动效果。通过理解和实践,开发者可以创造出丰富多样的用户体验,无论是简单的提示震动还是复杂的震动序列。记住,在使用震动功能时,始终尊重用户的...
这篇博客文章,"安卓开发-Vibrator手机震动服务 - 小小程序员 - 博客频道 - CSDN"深入探讨了如何在Android应用中实现手机震动功能。通过阅读这篇文章,开发者可以学习到以下关键知识点: 1. **Vibrator类**:...
首先,要实现震动效果,我们需要使用Android系统的Vibrator类。Vibrator类提供了震动的相关功能,允许应用程序请求设备进行震动。以下是如何在代码中使用Vibrator的基本步骤: 1. 获取Vibrator实例:通过Context的...
本文将深入探讨如何使用`Vibrator`来实现Android手机的节奏性震动,以及相关的编程知识点。 首先,我们需要了解`Vibrator`类的基本用法。`Vibrator`是`Context`的一个成员,可以通过`getSystemService()`方法获取到...
在Android系统中,实现手机震动功能是通过与硬件抽象层(HAL)交互来完成的,主要是利用了Android的Vibrator服务。这篇资料“Android安卓手机震动功能如何实现【源码】”应该包含了实现这一功能的具体Java代码和可能...
在Android平台上,实现手机震动功能是一项常见的需求,无论是为了提醒用户、增强游戏体验还是其他交互设计。本篇文章将深入探讨如何在Android应用中实现在不同场景下的震动效果,以供开发者参考。 首先,为了能够在...
在Android开发中,实现手机震动主要涉及到`Vibrator`类,它是Android系统提供的一个接口,用于控制设备的震动。首先,你需要在AndroidManifest.xml文件中为应用添加振动权限: ```xml <uses-permission android:...
在Android平台上,插件通常会利用Android的Vibrator服务来实现震动。开发者可以调用插件提供的API设置震动的时长和模式,甚至可以实现连续或复杂的震动序列。在iOS系统中,插件会利用AudioServicesPlaySystemSound...
在Android开发中,实现图片点击震动效果是一种常见的交互设计,它可以增强用户操作的感知和反馈。这个"Android代码-图片点击震动效果源码.zip"压缩包包含了一个实现这一功能的示例项目。以下是对其中核心知识点的...
开发者可以通过获取到系统的Vibrator服务,然后调用其方法来实现设备的震动效果。例如,我们可以使用`vibrate(long[] pattern, int repeat)`方法,其中`pattern`参数是一个包含震动模式的数组,`repeat`参数表示重复...
本文将详细介绍如何在Android系统中实现调用震动服务、控制滑动解锁震动效果的开启和关闭,以及如何获取当前的情景模式和铃声设置,进而实现震动和铃声提醒。 首先,让我们了解如何在Android中实现震动功能。...
本资源“android应用源码手机震动.zip”提供了一个关于如何在Android应用中实现手机震动功能的示例源码,非常适合开发者进行参考和学习。 在Android系统中,手机震动的实现主要依赖于`Vibrator`类,它位于`android....
为了实现手机震动功能,我们需要使用Android系统提供的Vibrator服务。Vibrator服务是一个系统服务,可以用来控制手机的震动器件。我们可以通过getSystemService方法来获取Vibrator服务的实例。 首先,我们需要在...
Android vibrator方法关闭和打开振动功能,通过Android vibrator方法设置不同的参数,来关闭和打开手机振动功能: ToggleButton tb2 = (ToggleButton)findViewById(R.id.tb2); //获得ToggleButton对象 tb2....
【标题】"android应用源码手机震动.rar"指的是一个Android应用程序的源代码,该源代码实现了手机震动功能。在Android平台上,开发者可以利用系统提供的API来控制设备的震动,为用户带来不同的交互体验。 【描述】...