AudioManager黑名单来电自动静音

添加权限

 

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

 

 

private TextView mTextView01;
	private TextView mTextView03;
	private EditText mEditText1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_silent);

		/* 设定PhoneCallListener */
		MyPhoneCallListener phoneListener = new MyPhoneCallListener();
		/* 设定TelephonyManager去抓取Telephony Severice */
		TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
		/* 设定Listen Call */
		telMgr.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

		/* 设定找寻TextView、EditText */
		mTextView01 = (TextView) findViewById(R.id.myTextView1);
		mTextView03 = (TextView) findViewById(R.id.myTextView3);
		mEditText1 = (EditText) findViewById(R.id.myEditText1);
		
		mEditText1.addTextChangedListener(new TextWatcher(){

			public void afterTextChanged(Editable s) {
				
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				mTextView03.setText(mEditText1.getText());
			}
			
		});

	}

	/* 判断PhoneStateListener现在的状态 */
	public class MyPhoneCallListener extends PhoneStateListener {
		public void onCallStateChanged(int state, String incomingNumber) {
			switch (state) {
			/* 判断手机目前是待机状态 */
			case TelephonyManager.CALL_STATE_IDLE:
				mTextView01.setText("手机目前是待机状态");

				try {
					AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
					if (audioManager != null) {
						/* 设定手机为待机时,响铃模式为正常 */
						audioManager
								.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
						audioManager.getStreamVolume(AudioManager.STREAM_RING);
					}
				} catch (Exception e) {
					mTextView01.setText(e.toString());
					e.printStackTrace();
				}
				break;

			/* 判断状态为通话中 */
			case TelephonyManager.CALL_STATE_OFFHOOK:
				mTextView01.setText("通话中");
				break;

			/* 判断状态为来电 */
			case TelephonyManager.CALL_STATE_RINGING:
				/* 显示现在来电的讯息 */
				mTextView01.setText("来电:"
						+ incomingNumber);

				/* 判断与输入电话是否为一样,当一样时响铃模式转为静音 */
				if (incomingNumber.equals(mTextView03.getText().toString())) {
					try {
						AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
						if (audioManager != null) {
							/* 设定响铃模式为静音 */
							audioManager
									.setRingerMode(AudioManager.RINGER_MODE_SILENT);
							//audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);震动
							audioManager
									.getStreamVolume(AudioManager.STREAM_RING);
							Toast.makeText(PhoneSilent.this,
									"静音模式",
									Toast.LENGTH_SHORT).show();
						}
					} catch (Exception e) {
						mTextView01.setText(e.toString());
						e.printStackTrace();
						break;
					}
				}
			}

			super.onCallStateChanged(state, incomingNumber);
		}
	}
 


AudioManager黑名单来电自动静音

你可能感兴趣的:(manager)