Android vibrate phone

In this Android tip, I am going to show you how to vibrate a phone to notify the user of something in your application. Vibration is a system service. To obtain an instance of system vibrator, you need to call the getSystemSerive() method with VIBRATOR_SERVICE as argument. The Vibrator instance has a method called vibrate() that can be used to vibrate a device constantly in the specified period of time or in a pattern. You need to the VIBRATE permission to the AndroidManifest.xml file to use the vibration service.
Typically, a vibration can take few seconds. To avoid locking your application interface while the vibration is performing, you should place the code to vibrate the device in a service that runs in background.
Now to have an example of vibrating a device with service, you create an Android project named AndVibrate. Then, in the src folder, create a class called VibrateService that extends IntentService class.


package com.example.andvibrate;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;

public class VibrateService extends IntentService {
public VibrateService() {
super("SERVICE");
// TODO Auto-generated constructor stub
}
public void onHandleIntent(Intent intent){
// Get Vibrator instance
     Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
     // Vibrate the device 3 seconds
     v.vibrate(300);

/* To vibrate in pattern, you have to pass a long array
                 long pattern[]={0,600,200,400};
The first value is the number of milliseconds to wait before turning the vibrator on. The next value indicates the number of milliseconds 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. The 2nd argument is for repetition . You pass -1 if you do not want to repeat the Vibrate.
                 v.vibrate(pattern,-1);
*/  
    
    

}
}


In the AndroidManifest.xml file immediately above </application> tag, you will declare the VibrateSerive service as shown below.

<service android:name=".VibrateService"/>

Now to start the service, in MainActivity class you have to create an intent object with specifying the current context and the service class to receive the intent and call the startService() method passing the intent.

package com.example.andvibrate;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent and start the service
     Intent intent=new Intent(this,VibrateService.class);
     startService(intent);
}
}


Android-vibrate-phone

Posted by: Dara | post date: 11-25-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.