关闭来电的声音是随机的

时间:2014-07-03 05:57:16

标签: android

经过长时间的搜索,我无法解决我的问题。

在我的应用中,我想用白名单静默控制来电号码。

这是使用的代码,有时它完美但不是每次都能正常工作。 铃声模式已恢复,当我按下上或下音量控制时我可以控制它,但没有声音输出到设备。

手机响铃时是否可以更改振铃模式? 我的测试是在S3 4.3

感谢您抽出时间帮助我。

public class CallReceiver extends BroadcastReceiver {

private static final String TAG = CallReceiver.class.getSimpleName();
private static final String EMERGENCY="112";
private ITelephony telephonyService;
private AudioManager maudio;

  @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) { 

              AudioManager maudio=(AudioManager)context.getSystemService(context.AUDIO_SERVICE);
            //backup audio setting before mute
              Integer ringcheck=maudio.getRingerMode();

            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
                // Incoming call

                //set mute before control allowed/denied
                maudio.setRingerMode(0);

                String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                if( !Control.AuthorizeSender(context,"in","CALL",incomingNumber,"Phone call."))
                {

                     TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
                      try {
                       Class c = Class.forName(telephony.getClass().getName());
                       Method m = c.getDeclaredMethod("getITelephony");
                       m.setAccessible(true);
                       telephonyService = (ITelephony) m.invoke(telephony);
                       telephonyService.endCall();
                      } catch (Exception e) {
                       e.printStackTrace();
                      }
                }
                else
                {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                //restore audio
                if (ringcheck == 1)
                    maudio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                else if (ringcheck == 2)
                    maudio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                Log.d(TAG,"Offhook");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE" />

1 个答案:

答案 0 :(得分:0)

许可:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

代码:

// Disable ringtone & vibration
AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE);
int ringerMode = am.getRingerMode();
am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

... doing something ...

// Restore ringtone settings after check
am.setRingerMode(ringerMode);