Change ChronometerNotificationService to be a foreground service
This commit is contained in:
parent
90bdbf2505
commit
ee689cbb3c
@ -48,10 +48,20 @@ public abstract class ChronometerNotificationService extends Service {
|
||||
* @return a tag associated with the notification. The default implementation returns the
|
||||
* name of this class.
|
||||
*/
|
||||
@Deprecated
|
||||
protected String getNoteTag() {
|
||||
return getClass().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the id for the foreground notification
|
||||
*/
|
||||
protected int getNoteId() {
|
||||
// TODO: Abstract this and override in TimerNotificationService. Currently only
|
||||
// StopwatchNotificationService implements this.
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The intent received in {@link #onStartCommand(Intent, int, int)}
|
||||
* has no {@link Intent#getAction() action} set. At this point, you
|
||||
@ -83,6 +93,7 @@ public abstract class ChronometerNotificationService extends Service {
|
||||
.setShowWhen(false)
|
||||
.setOngoing(true)
|
||||
.setContentIntent(getContentIntent());
|
||||
startForeground(getNoteId(), mNoteBuilder.build());
|
||||
mDelegate.init();
|
||||
mDelegate.setCountDown(isCountDown());
|
||||
}
|
||||
@ -149,7 +160,7 @@ public abstract class ChronometerNotificationService extends Service {
|
||||
* @param noteId the id with which the thread created here will be posting notifications.
|
||||
* @param base the new base time of the chronometer
|
||||
*/
|
||||
public void startNewThread(int noteId, long base) {
|
||||
public void startNewThread(int noteId/*TODO remove*/, long base) {
|
||||
// An instance of Thread cannot be started more than once. You must create
|
||||
// a new instance if you want to start the Thread's work again.
|
||||
mThread = new ChronometerNotificationThread(
|
||||
@ -158,7 +169,7 @@ public abstract class ChronometerNotificationService extends Service {
|
||||
mNoteBuilder,
|
||||
getResources(),
|
||||
getNoteTag(),
|
||||
noteId);
|
||||
getNoteId());
|
||||
// Initializes this thread as a looper. HandlerThread.run() will be executed
|
||||
// in this thread.
|
||||
// This gives you a chance to create handlers that then reference this looper,
|
||||
@ -237,10 +248,10 @@ public abstract class ChronometerNotificationService extends Service {
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the notification with the pair ({@link #getNoteTag() tag}, id)
|
||||
* Cancels the foreground notification.
|
||||
*/
|
||||
protected final void cancelNotification(int id) {
|
||||
mNotificationManager.cancel(getNoteTag(), id);
|
||||
protected final void cancelNotification(int id/*TODO remove*/) {
|
||||
mNotificationManager.cancel(getNoteId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -52,7 +52,7 @@ public class ChronometerNotificationThread extends HandlerThread {
|
||||
@NonNull NotificationManager manager,
|
||||
@NonNull NotificationCompat.Builder builder,
|
||||
@Nullable Resources resources,
|
||||
@NonNull String noteTag,
|
||||
@NonNull String noteTag, // TODO: remove param
|
||||
int noteId) {
|
||||
super(TAG);
|
||||
mDelegate = delegate;
|
||||
@ -93,7 +93,7 @@ public class ChronometerNotificationThread extends HandlerThread {
|
||||
CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources);
|
||||
mNoteBuilder.setContentText(text);
|
||||
}
|
||||
mNotificationManager.notify(mNoteTag, mNoteId, mNoteBuilder.build());
|
||||
mNotificationManager.notify(mNoteId, mNoteBuilder.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -30,13 +30,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
setContentTitle(getString(R.string.stopwatch));
|
||||
mUpdateHandler = new AsyncLapsTableUpdateHandler(this, null);
|
||||
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
mDelegate.init();
|
||||
mDelegate.setShowCentiseconds(true, false);
|
||||
setContentTitle(getString(R.string.stopwatch));
|
||||
// TODO: I think we can make this a foreground service so even
|
||||
// if the process is killed, this service remains alive.
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -82,7 +80,7 @@ public class StopwatchNotificationService extends ChronometerNotificationService
|
||||
// our thread has enough leeway to sneak in a final call to post the notification before it
|
||||
// is actually quit().
|
||||
// As such, try cancelling the notification with this (tag, id) pair again.
|
||||
cancelNotification(0);
|
||||
// cancelNotification(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -103,6 +101,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getNoteId() {
|
||||
return R.id.stopwatch_notification_service;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleDefaultAction(Intent intent, int flags, long startId) {
|
||||
// TODO: Why do we need this check? Won't KEY_START_TIME always have a value of 0 here?
|
||||
@ -197,17 +200,8 @@ public class StopwatchNotificationService extends ChronometerNotificationService
|
||||
|
||||
quitCurrentThread();
|
||||
if (running) {
|
||||
// startChronometer();
|
||||
long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime());
|
||||
startNewThread(0, startTime);
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Reads the value of KEY_START_TIME and passes it to {@link #startNewThread(int, long)} for you.
|
||||
// */
|
||||
// private void startChronometer() {
|
||||
// long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime());
|
||||
// startNewThread(0, startTime);
|
||||
// }
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<item type="id" name="ringtone_service_notification"/>
|
||||
<item type="id" name="stopwatch_notification_service"/>
|
||||
<item type="id" name="timer_notification_service"/>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue
Block a user