尝试允许某些联系人响铃甚至手机处于静音模式

时间:2013-01-04 18:13:23

标签: android

即使手机处于静音模式,我也试图允许某些联系人响铃。当安装应用程序后第一次打电话时,手机没有响铃,但后来病房工作正常......代码中是否有任何问题......

      package com.cnu.incoming;


      import android.app.PendingIntent;
      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
      import android.hardware.Sensor;
      import android.hardware.SensorEvent;
      import android.hardware.SensorEventListener;
      import android.hardware.SensorManager;
      import android.media.AudioManager;
      import android.os.Bundle;
      import android.telephony.SmsManager;
      import android.telephony.TelephonyManager;
      import android.util.Log;
      import android.widget.Toast;

      public class IncomingCall extends BroadcastReceiver{


      private Context context;
      private String PhNumber;
      private int ringcheck=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;

        Log.i("IncomingCallReceiver",bundle.toString());

        state = bundle.getString(TelephonyManager.EXTRA_STATE);

        Log.i("IncomingCallReceiver","State: "+ state);

        if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
        {
                check();
                if(PhNumber.equals("+919876543210"))
                makeitNormal();
                PhNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

                Log.i("IncomingCallReceiver","Incomng Number: " + PhNumber);

                String info = "Detect Calls Incoming number: " + PhNumber;


                maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);


        }

        Log.i("baddu gadu" ,"baddu gadu");

        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);
    ringcheck=maudio.getRingerMode();

}

private void makeitNormal() {
    // TODO Auto-generated method stub

    AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
    maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    //Toast.makeText(context, "this is baradwaj", Toast.LENGTH_LONG).show();

}


private void makeItSilent() {
    // TODO Auto-generated method stub
    AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
    int ringer=maudio.getRingerMode();
    if (ringcheck==AudioManager.RINGER_MODE_SILENT){

        maudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);

    }else if(ringcheck==AudioManager.RINGER_MODE_NORMAL){
        maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
    }
    else if(ringcheck==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


}

}

0 个答案:

没有答案