markos
markos

Reputation: 69

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();

        }

    }

Upvotes: 0

Views: 216

Answers (3)

markos
markos

Reputation: 69

Guys i finally figured it out, i saw couple of examples of telephonymanager and i wrote this one out and worked fine.

public class MainReceiver extends BroadcastReceiver{        

@Override

public void onReceive(Context context, Intent intent) {

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

                TelephonyManager tm = 

                    (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                        

                switch (tm.getCallState()) {

                case TelephonyManager.CALL_STATE_RINGING:

                        m.stop();
                        break;

                case TelephonyManager.CALL_STATE_OFFHOOK:                                


                        m.stop();
                                break;


                case TelephonyManager.CALL_STATE_IDLE:                                


                             m.start();

                            break;

                } 

        }

}

Upvotes: 0

Khang .NT
Khang .NT

Reputation: 1579

try code:

        final MediaPlayer m = MediaPlayer.create(context, R.raw.sound2);
        m.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                m.start();
            }
        });

        AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        am.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
            @Override
            public void onAudioFocusChange(int focusChange) {
                Log.i("FOCUS",focusChange + "");
                if (focusChange > 0)
                    m.start();
                else
                    m.pause();
            }
        }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

Or make your BroadcastReceiver receiver phone incoming state:

public class MainReceiver extends BroadcastReceiver {

    private static final String PLAYAUDIO = "PLAYAUDIO";
    private static final String PHONE_STATE = "android.intent.action.PHONE_STATE";
    private static MediaPlayer m;

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

        if (intent.getAction().equals(PLAYAUDIO)) {
            m= MediaPlayer.create(context, R.raw.sound2);
            m.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    m.start();
                }
            });
            m.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    m = null;
                }
            });
        } else
            if (intent.getAction().equals(PHONE_STATE)) {// NEW CALL STATE
                String extra = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                if (extra.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE) 
                                     && m != null)
                     m.start();
                else if (m != null)
                     m.pause();
            }

    }

}

AndroidManifest.xml like this:

    <receiver android:name="YourPackage.MainReceiver">
        <intent-filter>
            <action android:name="PLAYAUDIO"/>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
    //Add permistion
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Upvotes: 0

vicky
vicky

Reputation: 91

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.

Upvotes: 1

Related Questions