我开发了一款应用程序,即使手机处于静音模式,它也会选择性地拨打某些号码。我已经编写了代码,可以在广播接收器中大声播放。
广播接收器
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手机中工作,但它在三星工作
答案 0 :(得分:1)
您的BroadcastReceiver做得太多了。它只与onReceive()方法一样长,所以你没有得到你所有的功能。尝试将您的音量操作代码移动到服务中,并使用BroadcastReceiver作为一个简单的代理,在呼叫进入时告诉服务。通常,您应避免在BroadcastReceiver的上下文中进行大量操作。
此外,仅调用setRingerMode(AudioManager.RINGER_MODE_NORMAL)并不足以使振铃器无声。您还需要调用setStreamMute(AudioManager.STREAM_RING,false)和setStreamVolume(AudioManager.STREAM_RING,someAudibleVolume,0)。在许多设备上,振铃器模式可以是“正常”,而振铃器音量为“0”。
BTW我写了一个与你描述的非常相似的应用程序,祝你好运:)。在尝试一致地操纵铃声音量时,您会看到Android设备生态系统中的一些最糟糕的碎片。例如在三星Galaxy Nexus上,您可以在来电开始后轻松覆盖和操纵音量。然而在Galaxy S3上,铃声音量忽略了在呼叫进入时改变它的尝试,并且需要一些聪明的(阅读“hackish”)解决方法。