在静音模式下振铃

时间:2013-02-05 11:48:26

标签: android

我正在编写一个应用程序,它会在某些数字的静音模式下响铃。我创建了一个广播监听器,它将收听来电并拨打特定号码。我面临的问题是安装后第一次无法正常工作,之后就完美无缺。

我正在使用的代码是

    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

}   }

1 个答案:

答案 0 :(得分:0)

我想到的唯一想法就是在check()之外调用onReceive()方法,在ringerMode被移除之前设置onRecieve()字段。