我正在使用铃声管理器开发简单的应用程序。我可以为传入号码设置特定的铃声。但问题是当第二次来电时我的铃声工作正常。我需要自己第一次覆盖默认铃声。我怎样才能做到这一点。下面是我的代码片段。
public class IncomingCallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
Bundle b = intent.getExtras();
incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
incom_call=incommingNumber;
// Get Current Time
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
currentTime=today.format("%k:%M");
try
{
currentTime_int = Integer.parseInt(currentTime.replaceAll("[^0-9.]",""));
}
catch(NumberFormatException nfe)
{
}
try
{
contacts_db = new ContactsDB(context);
contacts_db.open();
Utilities.writeIntoLog("DB open");
cursor = contacts_db.fetchGroupName_fromContacttable(incom_call,op_flag);
if( cursor != null )
{
cursor.moveToFirst();
groupname = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDB.KEY_GROUPNAME));
contactname = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDB.KEY_FIRSTNAME));
}
cursor.close();
contacts_db.close();
}
catch (Exception e)
{
}
try
{
contacts_db = new ContactsDB(context);
contacts_db.open();
cursor = contacts_db.fetchDnD_Detail_fromGrouptable(groupname);
if( cursor != null )
{
cursor.moveToFirst();
ringtonepath = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDB.KEY_GROUPRINGTONE_INGROUPTABLE));
dnd_start_time = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDB.KEY_GROUP_DND_STARTTIME));
dnd_end_time = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDB.KEY_GROUP_DND_ENDTIME));
dnd_ring_mode = cursor.getString(cursor.getColumnIndexOrThrow(ContactsDB.KEY_GROUP_DND_RINGMODE));
}
cursor.close();
contacts_db.close();
}
catch (Exception e)
{
}
dnd_starttime_int = Integer.parseInt(dnd_start_time.replaceAll("[^0-9.]",""));
dnd_endtime_int = Integer.parseInt(dnd_end_time.replaceAll("[^0-9.]",""));
if(currentTime_int>=dnd_starttime_int && currentTime_int<=dnd_endtime_int)
{
if(dnd_ring_mode.equals("Silent"))
{
mobilemode = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); mobilemode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
else
{
mobilemode = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); mobilemode.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
else
{
mobilemode = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
mobilemode.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Uri ruri = Uri.parse(ringtonepath); RingtoneManager.setActualDefaultRingtoneUri(context,RingtoneManager.TYPE_RINGTONE, ruri);
}
}
catch (Exception e)
{
}
}
}