Now sounds alarm

This commit is contained in:
Doug Keen 2012-09-17 13:57:59 -07:00
parent 3a2623ab96
commit 5fe231abbe
2 changed files with 76 additions and 1 deletions

View File

@ -13,7 +13,7 @@ public class AlarmBroadcastReceiver extends BroadcastReceiver {
Intent targetIntent = new Intent(Intent.ACTION_VIEW, intent.getData()); Intent targetIntent = new Intent(Intent.ACTION_VIEW, intent.getData());
targetIntent.putExtra("boardedDeparture", intent.getExtras() targetIntent.putExtra("boardedDeparture", intent.getExtras()
.getParcelable("departure")); .getParcelable("departure"));
targetIntent.putExtra("isAlarm", true); targetIntent.putExtra("soundAlarm", true);
targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); targetIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(targetIntent); context.startActivity(targetIntent);

View File

@ -1,16 +1,25 @@
package com.dougkeen.bart; package com.dougkeen.bart;
import java.io.IOException;
import java.util.List; import java.util.List;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.v4.app.LoaderManager.LoaderCallbacks; import android.support.v4.app.LoaderManager.LoaderCallbacks;
@ -66,6 +75,8 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements
private EtdService mEtdService; private EtdService mEtdService;
private Handler mHandler = new Handler();
private boolean mBound = false; private boolean mBound = false;
@Override @Override
@ -189,6 +200,70 @@ public class ViewDeparturesActivity extends SherlockFragmentActivity implements
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (intent.getBooleanExtra("soundAlarm", false)) {
soundTheAlarm();
}
}
private MediaPlayer mMediaPlayer;
private void soundTheAlarm() {
Uri alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alertSound == null || !tryToPlayRingtone(alertSound)) {
alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alertSound == null || !tryToPlayRingtone(alertSound)) {
alertSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
if (mMediaPlayer == null) {
tryToPlayRingtone(alertSound);
}
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
silenceAlarm();
}
}, 20000);
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your train is leaving soon!")
.setCancelable(false)
.setNeutralButton("Silence alarm",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
silenceAlarm();
dialog.dismiss();
}
}).show();
}
private boolean tryToPlayRingtone(Uri alertSound) {
mMediaPlayer = MediaPlayer.create(this, alertSound);
if (mMediaPlayer == null)
return false;
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
return true;
}
private void silenceAlarm() {
try {
if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
}
} catch (IllegalStateException e) {
Log.e(Constants.TAG,
"Couldn't stop media player; It was in an invalid state", e);
}
} }
private void setListTitle() { private void setListTitle() {