怎么判断系统在播放闹铃?
AudioManager里有个方法isMusicActive()来判断当前是否在播放音乐。
对于闹铃呢?目前没找到相关的API。
两种解决方法:
1. 在AudioManager里增加方法,需要改动源码。可以参考Android获取第三方闹钟的闹铃信息
2. 通过反射。
反射的解决方法:
/**
* 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;
}
你只需要传递你所要检测的Stream type.对于闹铃一般用的是ALARM.