Thursday, November 21

How to make an Android vibrate?

Google+ Pinterest LinkedIn Tumblr +

This is my first english blog but i want to try. I show how to make an android device vibrate. Let’s begin.

Vibrator function is make Android Device vibrate

We will see often when notification alert.

1. Vibrate by Button

First Step Start by pressing the Start Button to shake and press the Stop button to stop shaking. But you must add User-Permission of Vibration earlier in AndroidManifest.xml.

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

AndroidManifest.xml

[xml] <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kamonway.vibrator" >

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
[/xml]

I will create 2 button.Start and Stop in layout xml.

[xml] <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/startBtn"
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/stopBtn"
android:text="Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>
[/xml]

In MainActivity.java declare Vibrator variable.

[java]Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);[/java]

At Onclick of Start button. We will start vibrate by use vibrate(1000). vibrate(1000) is mean vibrate 1 second (1000 millisec = 1 second).

[java] startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrator.vibrate(1000);
}
});
[/java]

At Onclick of Stop button. We will stop vibrate by vibrator.cancel();

[java] stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrator.cancel();
}
});
[/java]

When we press Start button. Device will vibrate 1 second and stop. But if we press Stop button, device will stop immediately.

2. Pattern Vibrate

We will create pattern of vibration. Declare long[] to fill order of pattern.

[java]long[] pattern = { 0, 200, 500 , 200 };[/java]

0 = Vibrate immediately.
200 = vibrate 200 milliseconds.
500 = stop and wait 500 milliseconds.
200 = vibrate 200 milliseconds.

to vibrate with pattern, We will change something.
[java]vibrator.vibrate(pattern, -1);[/java] pattern is long[] we declared.
-1 is do 1 time. You will fill -1 Only.

3. Vibrate untill press Stop button

To vibrate untill stop, We will change some code again.

[java]vibrator.vibrate(pattern, 0);[/java]

Be careful. Don’t forget to cancel vibrate when close activity. Because when you close activity or pause it. Device isn’t stop vibrate.In onPause must add vibrator.cancel();

Extra!!!

If you want to know Vibrator is still working or not?

[java] if (vibrator.hasVibrator()) {
Log.d("Can Vibrate", "YES");
} else {
Log.d("Can Vibrate", "NO");
}
[/java]

Thank you for reading. I hope it help.

MainActivity.java
[java] public class MainActivity extends AppCompatActivity {

Button startBtn, stopBtn;
Vibrator vibrator;

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

startBtn = (Button) findViewById(R.id.startBtn);
stopBtn = (Button) findViewById(R.id.stopBtn);

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

startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrator.vibrate(1000);
}
});

stopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
vibrator.cancel();
}
});

}
}
[/java]

Reference
http://android.konreu.com/developer-how-to/vibration-examples-for-android-phone-development/
http://stackoverflow.com/questions/13950338/how-to-make-an-android-device-vibrate

Facebook Comments
Share.

About Author

สวัสดีครับ ผมไอซ์ กมลวัฒน์ ผู้ก่อตั้งช่อง Youtube Kamonway และเว็บไซต์ kamonway.com เพื่อช่วยแนะนำความรู้จากเกม และเป็นช่องทางที่ช่วยให้ทุกท่านเล่นเกมอย่างสนุกสนานยิ่งขึ้น

Comments are closed.