Search code examples
javaandroideclipsealarmmanager

Making a repeated alarm stop during a phone call


My app has a repeating alarm with a number picker, so if i chose to repeat every one minute a sound goes off every one minute, but if the user received a phone call the alarm doesn't stop. How can i solve this problem? What should i implement in the receiver activity in order for it to detect a phone call?

Receiver:

    import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;


public class MainReceiver extends BroadcastReceiver {



         @Override
        public void onReceive(Context context, Intent intent) {

             MediaPlayer m=MediaPlayer.create(context, R.raw.sound2);
             m.start();

        }

    }

Solution

  • Just updated my above answer a bit:As per Android documentation for AlarmManager.setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation), it will continue to come until explicitly removed with cancel(PendingIntent). If you are using this API then either you have to cancel it on call or in your receiver you have to add call condition and just return without doing anything.