Vibrate Android Phone Custom

Vibrating your Android phone programmatically

Vibrate Android Phone Custom

You can vibrate your Android device using few lines of code in your project. Vibrator is one of the most and useful devices for mobile phones. Vibrating your Android phone programmatically is very easy and simple.

When user touches the application, and you would like to give a feedback to user then you can use this vibrate service. First of all you need to change in your AndroidManifest.xml. You need to insert this line in your file.

 

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

 

Then your AndroidManifest.xml should be like this:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.legendblogs.vibrate"
    android:versionCode="1"
    android:versionName="1.0" >
     
    <uses-permission android:name="android.permission.VIBRATE"/>
  
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="23" />
 
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.legendblogs.vibrate.VibratorActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

 

Once you have added the permission you need to make a changes in your Activity file:

MainActivity.java

com.legendblogs.vibrate;

import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
 
 
public class VibratorActivity extends Activity
  {
     
    public Vibrator vibrator;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button vibrate_button = (Button)findViewById(R.id.vibrate_button);
        vibrate_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v1) {
                vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
                vibrator.vibrate(3000); // vibrate for 3 seconds (e.g 3000 milliseconds)
            }
        });

    }
}

 

And your activity_main.xml file should be:

activity_main.xml

<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:orientation="vertical"
         tools:context=".MainActivity">
  
    <Button
        android:id="@+id/Vibrate_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start vibrating" />
 
</RelativeLayout>

 

 

Top