Payday Loan Payday Loan

Archive for April 5th, 2011

How to use vibrate function on Android

Hi, Today i will show you how to use the vibrate function on your Android device.
You will need also to put the following permission on the Manifest file: android.permission.VIBRATE.
So just write down this code in Your activity:

import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
 
public class Main extends Activity implements OnClickListener {
 
private Button simpleVibration;
private Button patternVibrate;
private Button cancelVibrate;
 
private Vibrator vibrator;
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  simpleVibration= (Button)findViewById(R.id.simpleVibration);
  simpleVibration.setOnClickListener(this);
   patternVibration = (Button)findViewById(R.id.patternVibration);
  patternVibration.setOnClickListener(this);
  cancelVibration = (Button)findViewById(R.id.cancelVibration);
  cancelVibration.setOnClickListener(this);
  vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
}
public void onClick(View v) {
  if(v==simpleVibration){
 
    vibrator.vibrate(300);//vibrate for 300 miliseconds
  }else if(v == patternVibration){
    long[] pattern = {0L,200L,350L,1300L,260L,230L};
    vibrator.vibrate(pattern,2); //vibrate on the pattern
  }
 
  else{
    vibrator.cancel();
  }
}
 
 
}

Code for main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/simpleVibration" android:text="Slow Vibrate"/>
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/patternVibration"
    android:text="Pattern Vibrate"
  />
  <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cancelVibration"
    android:text="Cancel Vibrate"
  />
 
 
</LinearLayout>
||||| 0 I Like It! |||||

Hi,
I think many of you thought which is best to retrieve from the Android Device, so that you can be sure the device will not trick you and the identifier you asked is unique and unchangeable.
For example we can get the IMEI code but users would became suspicious because you will need to set a permission to use this, and beside this IMEI have only phones, not tablets.
You may choose to retrive ANDROID_ID with the following code:

String deviceId = Settings.System.getString(getContentResolver(),
                                Settings.System.ANDROID_ID);

But this is documented to be changeable at the factory reset and unpredictive changeable on rooted phones.
Other option would be to get a Serial Number:

TelephonyManager manager= (TelephonyManager)myActivity.getSystemService(Context.TELEPHONY_SERVICE);
String serial= manager.getDeviceId();

This will need permission: android.permission.READ_PHONE_STATE

getDeviceID() will return the MDN or MEID of the device depending on which radio the phone uses (GSM or CDMA).

As you already guess this option have the same disadvantage(DON’ T WORK FOR TABLETS ).
We have one last solution and maybe it’ s the best, since over 95% of Android Powered Devices have a wireless card.
So we will retrieve the MAC address:

WifiManager wimanager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String address= wimanager.getConnectionInfo().getMacAddress();

This will need permission: android.permission.ACCESS_WIFI_STATE
WiFi MAC address can be easily faked on rooted phones, so we have another unique serial number on Bluetooth.
Code for retrieving Bluetooth unique identifier:

BluetoothAdapter btAdapt= null; 
    	btAdapt = BluetoothAdapter.getDefaultAdapter();
    	String address= btAdapt.getAddress();

This code will need permission: android.permission.BLUETOOTH

||||| 3 I Like It! |||||

Search

Popular

Sponsors