我正在编写一个应用程序,它会在某些号码的静音模式下响铃。我创建了一个广播监听器,它将监听来电并为特定号码响铃。我面临的问题是第一次安装后它不工作,之后它工作得很好。
我正在使用的代码是
public class Ring extends BroadcastReceiver{
private Context context;
private String PhoneNumber;
private int ringerMode=0;
String state=null;
Bundle bundle=null;
@Override
public void onReceive(Context context, Intent intent) {
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);
if(PhoneNumber.equals("+918281306132"))
makeitNormal();
maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
makeItSilent();
}
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE))
{
makeItSilent();
}
}
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
} }