How can I determine whether the system is playing an alarm?
There is an isMusicActive () method in AudioManager to determine whether the current music is being played.
What about the alarm? No related APIs are found.
Two solutions:
1. Add a method to AudioManager and modify the source code. You can refer to Android to get the third-party alert message.
2. reflection.
Reflection solution:
/** * Unhide android api: check is stream is active now (AudioManager.STREAM_RING, AudioManager.STREAM_NOTIFICATION...), * uses reflection * @param audioStream * @return */public static boolean isStreamActive(int audioStream) { Class
audioSystemClazz = null; Boolean res = false; try { audioSystemClazz = Class.forName("android.media.AudioSystem"); if (null != audioSystemClazz) { // isStreamActive Method method = audioSystemClazz.getDeclaredMethod("isStreamActive", new Class
[] { int.class, int.class }); if (null != method) { res = (Boolean) method.invoke(null, audioStream, 0); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return res;}
You only need to pass the Stream type you want to detect. For the ALARM, ALARM is generally used.