创建自定义振动模式
首先,您需要在清单中声明此权限
<uses-permission android:name="android.permission.VIBRATE"/>
其次,您需要获取 Vibrator 类的实例
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
这是有关如何设置自定义振动模式的建议
// 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);
参考这里
获取联系人姓名
像往常一样,声明许可
<uses-permission android:name="android.permission.READ_CONTACTS" />
使用ContentResolver
查询获取联系人。此查询获取联系人 ID、姓名和电话号码为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();
}
}
}
参考这里
将联系人映射到振动模式
这种映射存储可以通过多种方式完成,具体取决于您选择的持久存储实现。在我看来,有两种明显的方法
id -> pattern
此外,实际映射可以通过name -> pattern
或来完成phone_nr -> pattern
。同样,这是您对实现设计的选择。就个人而言,我会实现数据库支持,因为它支持可维护性和可扩展性,映射phone_nr -> pattern
。
例如,您实现了两种方法来设置映射,另一种方法从给定的phone_nr
.
设置映射
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();
}
}
获取模式
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;
}
全部使用
要收听来电,可以以BroadcastReceiver
. 接收器类监听CallStateChange
,因此当您接到来电时,您将保留来电电话号码。如果您将电话号码映射到您的振动模式,则非常方便,如上所示。
在你的AndroidManifest.xml
:
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
接收器类
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);
}
}
编辑
要关闭振动,您可以使用以下代码:
AudioManager am =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
要再次启用它:
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);