本文主要是介绍android开发之耳机调至最大音量时,提示损伤听力,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
android开发之耳机调至最大音量时,提示损伤听力
通过提示语,我们可以查出,只要的逻辑代码是在framework/base/packages/SystemUI/src/com/android/systemui/SafetyWarningDialog.java
查看代码可知,在这个类的构造函数中,首先初始化了一个dialog
public SafetyWarningDialog(Context context, AudioManager audioManager) {
45 super(context);
46 mContext = context;
47 mAudioManager = audioManager;
48
49 getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
50 setMessage(mContext.getString(com.android.internal.R.string.safe_media_volume_warning));
51 setButton(DialogInterface.BUTTON_POSITIVE,
52 mContext.getString(com.android.internal.R.string.yes), this);
53 setButton(DialogInterface.BUTTON_NEGATIVE,
54 mContext.getString(com.android.internal.R.string.no), (OnClickListener) null);
55 setOnDismissListener(this);
56
57 final IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
58 context.registerReceiver(mReceiver, filter);
59 }
当用户做按键操作时,会调用相关的listener 函数,具体的代码实现如下:
@Override 64 public boolean onKeyDown(int keyCode, KeyEvent event) { 65 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getRepeatCount() == 0) { 66 mNewVolumeUp = true; 67 } 68 return super.onKeyDown(keyCode, event); 69 } 70 71 @Override 72 public boolean onKeyUp(int keyCode, KeyEvent event) { 73 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && mNewVolumeUp 74 && (System.currentTimeMillis() - mShowTime) > KEY_CONFIRM_ALLOWED_AFTER) { 75 if (D.BUG) Log.d(TAG, "Confirmed warning via VOLUME_UP"); 76 mAudioManager.disableSafeMediaVolume(); 77 dismiss(); 78 } 79 return super.onKeyUp(keyCode, event); 80 } 81 82 @Override 83 public void onClick(DialogInterface dialog, int which) { 84 mAudioManager.disableSafeMediaVolume(); 85 } 86
mAudioManager.disableSafeMediaVolume(); //关闭安全音量提醒
如果说平台需要,可以通过config xml配置,将config_safe_media_volume_enabled 置false 来关闭此功能
这篇关于android开发之耳机调至最大音量时,提示损伤听力的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!