我开发了一个应用程序,即使手机处于静音模式,它也会有选择地为某些号码响铃。我编写了代码,让它在广播接收器中更响亮。
广播接收器是
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("MyApp","Silent mode");
profileStatus = 1;
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("MyApp","Vibrate mode");
break;
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","Normal mode");
profileStatus= 2;
break;
}
System.out.println("The Profile Status"+profileStatus);
System.out.println("The Old Profile Status"+oldProfileStatus);
System.out.println("The Count = "+count );
if(profileStatus==1)
{
System.out.println("ProfileStatus"+profileStatus);
OpenHelper db = new OpenHelper(context);
//Getting Data From The DataBase
CONTACT_DATA = db.Contact_Records();
if (CONTACT_DATA.size() > 0)
{
//Log.e("ARRAY", "not null");
for(int i =0;i<CONTACT_DATA.size();i++)
{
HashMap<String, String> map = CONTACT_DATA.get(i);
phone_number = map.get("phone_number");
}
}
Bundle bundle = intent.getExtras();
final AudioManager mobilemode = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
if(null == bundle)
return;
Log.i("IncomingCallReceiver",bundle.toString());
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.i("IncomingCallReceiver","State: "+ state);
if(state.equals("IDLE"))
{
System.out.println("THE STATE IS IDLE");
mobilemode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("IncomingCallReceiver","Incomng Number:from receiver------------------ " + phonenumber);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
bundle = intent.getExtras();
this.context = context;
if(null == bundle)
return;
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
check();
PhoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
for(int i =0;i<CONTACT_DATA.size();i++)
{
HashMap<String, String> map = CONTACT_DATA.get(i);
phone_number = map.get("phone_number");
if (PhoneNumber.equals(phone_number)) {
}
if((PhoneNumber.equals(phone_number)))
{
makeitNormal();
maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
oldProfileStatus = profileStatus;
System.out.println("The Profile Status==="+oldProfileStatus);
count=1;
}
}
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
makeItSilent();
mobilemode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE))
{
makeItSilent();
mobilemode.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}
//
}
}
else
{
System.out.println("The System Is In Normal Mode ");
count=0;
}
}
private void check() {
// TODO Auto-generated method stub
AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
ringerMode=maudio.getRingerMode();
}
private void makeitNormal() {
// TODO Auto-generated method stub
AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
private void makeItSilent() {
// TODO Auto-generated method stub
AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
int ringer=maudio.getRingerMode();
if (ringerMode==AudioManager.RINGER_MODE_SILENT){
maudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}else if(ringerMode==AudioManager.RINGER_MODE_NORMAL){
maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
else if(ringerMode==AudioManager.RINGER_MODE_VIBRATE){
maudio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
}
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
}
我面临的问题是 1)它在安装后第一次没有响铃。之后它完美地工作 2)它不能在 HTC 手机中工作,但它在三星中工作