Determines whether an alarm is being played

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.