Open top menu






 Android Example –How to Vibrate android device programmatically?

Step 1:- Add bellow permission in your manifest file.
<uses-permission android:name="android.permission.VIBRATE"/>

Step 2: Get Vibrate System Service.
Vibrator vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

Step 3: Set Vibrate  Time period in milliseconds and Time Pattern. Time Pattern means like start/stope vibration.
vibrate.vibrate(300);//in millisecond.

Pattern
long pattern[] = {60,120,180,240,300,360,420,480};
vibrate.vibrate(pattern,1);

 .................................................................................................................................................................

 activity_main.xml
.................................................................................................................................................................
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
    
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:gravity="center"
  
android:orientation="vertical">

    <
TextView
       
android:id="@+id/textView1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text=""
       
android:textAppearance="?android:attr/textAppearanceLarge" />

    <
Button
       
android:id="@+id/btn_start"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView1"
       
android:text="Vibrate Start"
       
android:layout_margin="20dp"/>
    <
Button
       
android:id="@+id/btn_stope"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_below="@+id/textView1"
       
android:text="Vibrate Stope"
       
android:layout_margin="20dp"/>

</
LinearLayout>
.................................................................................................................................................................



MainActivity.java
.................................................................................................................................................................

 

import android.content.Context;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView
text;
    Vibrator
vibrate;
    Button
btn_start,btn_stope;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
text = (TextView) findViewById(R.id.textView1);
       
btn_start = (Button) findViewById(R.id.btn_start);
       
btn_stope = (Button) findViewById(R.id.btn_stope);

       
vibrate = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
       
btn_start.setOnClickListener(new View.OnClickListener() {

           
@Override
           
public void onClick(View v) {
               
long pattern[] = {60,120,180,240,300,360,420,480};
               
vibrate.vibrate(pattern,1);
               
//vibrate.vibrate(300);
               
text.setText("Vibrate Start...");

            }
        });
       
btn_stope.setOnClickListener(new View.OnClickListener() {

           
@Override
           
public void onClick(View v) {
               
vibrate.cancel();
               
text.setText("Vibrate Stope...");


            }
        });
    }

}

.................................................................................................................................................................


Tagged

0 comments