2

How can we set different vibration pattens for different contacts with incoming call ?

During my research and development I have refereed this :

1) How to provide customized vibration on specific incoming calls
2) How to change incoming call Vibration level when incoming call made?

But I didnt get success with this, while we can set different vibration petterns with different incoming call. its possible.

Example app :

1) https://play.google.com/store/apps/details?id=ac.vibration&hl=en

2) https://play.google.com/store/apps/details?id=com.bfc.morsecall

I hope that someone could give some advice on this issue. Comments on these two ways or other suggest are welcomed.

1 Answer 1

3

Create a custom vibrate pattern

First off, you will need to declare this permission in your manifest

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

Secondly, you will need to get an instance of the Vibrator class

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

Here's a suggestion on how you can set up a custom vibrate pattern

// Start without a delay
// Each element then alternates between vibrate, sleep, vibrate, sleep...
long[] pattern = {0, 100, 1000, 300, 200, 100, 500, 200, 100};

// The '-1' here means to vibrate once, as '-1' is out of bounds in the pattern array
v.vibrate(pattern, -1);

Reference here

Get contact names

As usual, declare permission

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

Fetch the contacts using a ContentResolver query. This query fetches contact id, name and telephone number as String.

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                  String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));                      
                  if (Integer.parseInt(cur.getString(
                        cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                     Cursor pCur = cr.query(
                               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                               null,
                               ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                               new String[]{id}, null);
                     while (pCur.moveToNext()) {
                         String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                         Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
                     }
                    pCur.close();
                }
            }
        }

Reference here

Map the contact to a vibrate pattern

This mapping storage could be done in a couple of ways, depending on your choice of persistent storage implementation. As I see it, there's two obvious ways

Also, the actual mapping could be done either by id -> pattern, name -> pattern or phone_nr -> pattern. Again, it's your choice of implementation design. Personally, I'd implement database support because it supports maintainability and scaleability, mapping phone_nr -> pattern.

For example, you implement two methods to set your mapping, and another method to get that mapping from a given phone_nr.

Set mapping

public void setPatternMapping(String phonr_nr, long[] pattern){

    //Database call here. Example:
    try{
        database.open();
        database.setPattern(phone_nr, pattern);
        database.close();
    }catch(SQLiteException e){
        e.printStackTrace();
    }

}

Get pattern

public long[] getPattern(String phone_nr){

    //Database call here. Example:
    long[] pattern = null;
    try{
        database.open();
        pattern = database.setPattern(phone_nr, pattern);
        database.close();
    }catch(SQLiteException e){
        e.printStackTrace();
    }
    return pattern;

}

Putting it all to use

To listen to an incoming call, one could implement a listener in the form of a BroadcastReceiver. The receiver class listens for CallStateChange, so when you get an incoming call, you will get a hold on the incoming call telephone number. Pretty handy if you mapped the telephone number to your vibrate pattern, as shown above.

In your AndroidManifest.xml:

<receiver android:name=".CallReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver> 

Receiver class

public class CallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);

                //Make a database call, to get the vibrate pattern 
                long[] pattern = getPattern(incomingNumber);
                //Set the phone to vibrate using that pattern, if there was a mapping
                if(pattern != null){                     
                    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                    v.vibrate(pattern, -1);
                }
                System.out.println("incomingNumber : "+incomingNumber);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }
}

Edit

To turn off the vibration, you can use this code:

AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

To enable it again:

am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
1
  • Thank you for reply and Nicely explained, but it will not turn off current vibration of incoming call, what i am trying to achieve is, change incoming call vibration. If device's current vibration is ON, this code fails. so we need to first turn of vibration, then vibrate phone with our vibration setting. Please let me know how can we achieve this. Feb 25, 2015 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.