How to detect alarm ringing or other apps using speaker? [closed]

左心房为你撑大大i 提交于 2019-11-27 02:58:20

问题


I am developing a music app. I want to detect that other apps using speaker or alarm ringing for play/pause music.

For phone call i use PhoneStateListener. I tryed OnAudioFocusChangeListener for to detect other audios. But didn't work.

I wondering how to solve this problem.


回答1:


I solved my problem for default alarm application:

public static final String ALARM_ALERT_ACTION = "com.android.deskclock.ALARM_ALERT";
public static final String ALARM_SNOOZE_ACTION = "com.android.deskclock.ALARM_SNOOZE";
public static final String ALARM_DISMISS_ACTION = "com.android.deskclock.ALARM_DISMISS";
public static final String ALARM_DONE_ACTION = "com.android.deskclock.ALARM_DONE";

private BroadcastReceiver mReceiver = new BroadcastReceiver() 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
          String action = intent.getAction();
          if (action.equals(ALARM_ALERT_ACTION) || action.equals(ALARM_DISMISS_ACTION) || action.equals(ALARM_SNOOZE_ACTION) || action.equals(ALARM_DONE_ACTION)) 
          {
              // for play/pause mediaplayer
              playPause();
          }
    }
};


@Override
public void onCreate(Bundle savedInstanceState) 
{
    IntentFilter filter = new IntentFilter(ALARM_ALERT_ACTION);
    filter.addAction(ALARM_DISMISS_ACTION);
    filter.addAction(ALARM_SNOOZE_ACTION);
    filter.addAction(ALARM_DONE_ACTION);
    registerReceiver(mReceiver, filter);
}


来源:https://stackoverflow.com/questions/11844657/how-to-detect-alarm-ringing-or-other-apps-using-speaker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!