투케이2K

161. (AndroidStudio/android/java) AudioManager 사용해 onKeyDown 이벤트 발생 시 볼륨 조절 실시 본문

Android

161. (AndroidStudio/android/java) AudioManager 사용해 onKeyDown 이벤트 발생 시 볼륨 조절 실시

투케이2K 2021. 5. 24. 18:06

/* =========================== */

[ 개발 환경 설정 ]

개발 툴 : AndroidStudio

개발 언어 : java

/* =========================== */

/* =========================== */

[소스 코드]

 

/** ========== [키 이벤트 확인] ========== **/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	// TODO [디바이스의 키 이벤트 : 뒤로가기]
	if (keyCode == KeyEvent.KEYCODE_BACK) {
		Log.d("---","---");
		Log.d("//===========//","================================================");
		Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : 백버튼 터치시 뒤로 가기 이벤트 실시]");
		Log.d("//===========//","================================================");
		Log.d("---","---");
		try {
			//TODO [액티비티 종료 실시]
			finish();
			overridePendingTransition(0,0);
		}
		catch (Exception e){
			e.printStackTrace();
		}
	}

	//TODO [디바이스 키 이벤트 : 볼륨 높임]
	if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){		
		if(tittle.contains("미디어")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.w("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 높임 이벤트 수행]");
			Log.d("","\n"+"[타입 : 미디어 볼륨 높임]");
			Log.w("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("벨소리")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.w("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 높임 이벤트 수행]");
			Log.d("","\n"+"[타입 : 벨소리 볼륨 높임]");
			Log.w("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("알림")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.w("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 높임 이벤트 수행]");
			Log.d("","\n"+"[타입 : 알림 볼륨 높임]");
			Log.w("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("시스템")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.w("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 높임 이벤트 수행]");
			Log.d("","\n"+"[타입 : 시스템 볼륨 높임]");
			Log.w("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("알람")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.w("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 높임 이벤트 수행]");
			Log.d("","\n"+"[타입 : 알람 볼륨 높임]");
			Log.w("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("통화")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.w("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 높임 이벤트 수행]");
			Log.d("","\n"+"[타입 : 통화 볼륨 높임]");
			Log.w("//===========//","================================================");
			Log.d("---","---");				
		}
	}

	//TODO [디바이스 키 이벤트 : 볼륨 내림]
	if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
		if(tittle.contains("미디어")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.e("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 내림 이벤트 수행]");
			Log.d("","\n"+"[타입 : 미디어 볼륨 내림]");
			Log.e("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("벨소리")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_RING, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.e("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 내림 이벤트 수행]");
			Log.d("","\n"+"[타입 : 벨소리 볼륨 내림]");
			Log.e("//===========//","================================================");
			Log.d("---","---");
		}
		if(tittle.contains("알림")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.e("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 내림 이벤트 수행]");
			Log.d("","\n"+"[타입 : 알림 볼륨 내림]");
			Log.e("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("시스템")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.e("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 내림 이벤트 수행]");
			Log.d("","\n"+"[타입 : 시스템 볼륨 내림]");
			Log.e("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("알람")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.e("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 내림 이벤트 수행]");
			Log.d("","\n"+"[타입 : 알람 볼륨 내림]");
			Log.e("//===========//","================================================");
			Log.d("---","---");				
		}
		if(tittle.contains("통화")){
			AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			mAudioManager.adjustStreamVolume(AudioManager.STREAM_VOICE_CALL, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
			Log.d("---","---");
			Log.e("//===========//","================================================");
			Log.d("","\n"+"[A_KeyEvent > onKeyDown() 메소드 : KEYCODE_VOLUME_UP 볼륨 내림 이벤트 수행]");
			Log.d("","\n"+"[타입 : 통화 볼륨 내림]");
			Log.e("//===========//","================================================");
			Log.d("---","---");				
		}
	}
	return true;
}

/* =========================== */

/* =========================== */

[결과 출력]

/* =========================== */

 

반응형
Comments