From 7432a057b311e37dec98dd45fddd44e34eed6fab Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Sun, 5 Jun 2016 21:43:35 -0700 Subject: [PATCH] Ringtone stops immediately after dismissive action --- .../clock2/ringtone/RingtoneActivity.java | 4 ++-- .../clock2/ringtone/RingtoneService.java | 14 ++------------ 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java index ee21a1f..31c38e2 100644 --- a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java @@ -80,8 +80,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi //super.onNewIntent(intent); // Not needed since no fragments hosted? if (mBound) { mBoundService.interrupt(); // prepare to notify the alarm was missed - // Cannot rely on finish() to call onDestroy() on time before the activity is restarted, - // so unbind from the service manually. + // Cannot rely on finish() to call onDestroy() on time before the activity is restarted. unbindService(); // Calling recreate() would recreate this with its current intent, not the new intent passed in here. finish(); @@ -139,6 +138,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi private void dismiss() { // TODO: Do we need to cancel the PendingIntent and the alarm in AlarmManager? + unbindService(); // don't wait for finish() to call onDestroy() finish(); } diff --git a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java index 446158f..b5c6f1f 100644 --- a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java +++ b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java @@ -29,17 +29,6 @@ import static com.philliphsu.clock2.util.Preconditions.checkNotNull; * of the RingtoneService will be tied to that of its RingtoneActivity because users are not likely to * navigate away from the Activity without making an action. But if they do accidentally navigate away, * they have plenty of time to make the desired action via the notification. - * - * This is both a started and bound service. See https://developer.android.com/guide/components/bound-services.html#Lifecycle - * "... the service runs until the service stops itself with stopSelf() or another component - * calls stopService(), regardless of whether it is bound to any clients." - * The regardless phrase didn't work for me. I had to unbind in RingtoneActivity first before calling - * stopSelf() on this service for the ringtone to stop playing. - * TODO: Consider making this purely a bound service, so you don't have to bind/unbind AND start/stop - * manually. Instead of implementing onStartCommand() and calling startService(), you would write a public - * method that the activity calls to start playing the ringtone. When the activity calls its onDestroy(), it unbinds - * itself from this service, and the system will know to destroy this service instead of you manually - * calling stopSelf() or stopService(). */ public class RingtoneService extends Service { // TODO: abstract this, make subclasses private static final String TAG = "RingtoneService"; @@ -57,6 +46,7 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc @Override public void run() { mAutoSilenced = true; + mRingtone.stop(); // don't wait for activity to finish and unbind if (mRingtoneCallback != null) { // Finish the activity, which fires onDestroy() and then unbinds itself from this service. // All clients must be unbound before stopSelf() (and stopService()?) will succeed. @@ -170,6 +160,6 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc /*int minutes = Integer.parseInt(pref.getString( getString(R.string.key_silence_after), "15"));*/ - //mSilenceHandler.postDelayed(mSilenceRunnable, 10000); + mSilenceHandler.postDelayed(mSilenceRunnable, 10000); } }