Anroid Vibrate Example
We can use Vibrator as a alert to the user, for example getting incoming calls, message and Geo fence.
You can download source code here.
Following steps to create Android Vibrator :
Add permission from Android Manifest.
You can download source code here.
Following steps to create Android Vibrator :
Add permission from Android Manifest.
<uses-permission android:name="android.permission.VIBRATE"/>
Next we have to create activity_vibrator.xml in that add two button one for start vibrator and another one for stop vibrator.
In Vibrator class have four method
1)vibrate(long milliseconds)
2)vibrate(long[] patern, int repeat)
3)hasvibreate()
4)cancel()
We are going to use vibrate(long milliseconds) and cancel() methods.
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".VibratorActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:text="@string/start" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="@string/stop" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".VibratorActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:text="@string/start" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="@string/stop" />
</RelativeLayout>
In Vibrator class have four method
1)vibrate(long milliseconds)
2)vibrate(long[] patern, int repeat)
3)hasvibreate()
4)cancel()
We are going to use vibrate(long milliseconds) and cancel() methods.
package com.etr.vibratorexample;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VibratorActivity extends Activity implements OnClickListener {
private Button mStart, mStop;
private Vibrator mVibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vibrator);
mStart = (Button) findViewById(R.id.button2);
mStop = (Button) findViewById(R.id.button1);
mStart.setOnClickListener(this);
mStop.setOnClickListener(this);
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
} catch (Exception e) {
Log.e("Exception : ", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.vibrator, menu);
return true;
}
private void startVibrate(View v) {
try {
mVibrator.vibrate(50000);
} catch (Exception e) {
Log.e("Exception : ", e.toString());
}
}
private void stoptVibrate(View v) {
try {
mVibrator.cancel();
} catch (Exception e) {
Log.e("Exception : ", e.toString());
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
startVibrate(v);
break;
case R.id.button2:
stoptVibrate(v);
}
}
}
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VibratorActivity extends Activity implements OnClickListener {
private Button mStart, mStop;
private Vibrator mVibrator;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vibrator);
mStart = (Button) findViewById(R.id.button2);
mStop = (Button) findViewById(R.id.button1);
mStart.setOnClickListener(this);
mStop.setOnClickListener(this);
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
} catch (Exception e) {
Log.e("Exception : ", e.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.vibrator, menu);
return true;
}
private void startVibrate(View v) {
try {
mVibrator.vibrate(50000);
} catch (Exception e) {
Log.e("Exception : ", e.toString());
}
}
private void stoptVibrate(View v) {
try {
mVibrator.cancel();
} catch (Exception e) {
Log.e("Exception : ", e.toString());
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
startVibrate(v);
break;
case R.id.button2:
stoptVibrate(v);
}
}
}
Comments
Post a Comment