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
|
* @return a tag associated with the notification. The default implementation returns the
|
||||||
* name of this class.
|
* name of this class.
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
protected String getNoteTag() {
|
protected String getNoteTag() {
|
||||||
return getClass().getName();
|
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)}
|
* The intent received in {@link #onStartCommand(Intent, int, int)}
|
||||||
* has no {@link Intent#getAction() action} set. At this point, you
|
* has no {@link Intent#getAction() action} set. At this point, you
|
||||||
@ -83,6 +93,7 @@ public abstract class ChronometerNotificationService extends Service {
|
|||||||
.setShowWhen(false)
|
.setShowWhen(false)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setContentIntent(getContentIntent());
|
.setContentIntent(getContentIntent());
|
||||||
|
startForeground(getNoteId(), mNoteBuilder.build());
|
||||||
mDelegate.init();
|
mDelegate.init();
|
||||||
mDelegate.setCountDown(isCountDown());
|
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 noteId the id with which the thread created here will be posting notifications.
|
||||||
* @param base the new base time of the chronometer
|
* @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
|
// 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.
|
// a new instance if you want to start the Thread's work again.
|
||||||
mThread = new ChronometerNotificationThread(
|
mThread = new ChronometerNotificationThread(
|
||||||
@ -158,7 +169,7 @@ public abstract class ChronometerNotificationService extends Service {
|
|||||||
mNoteBuilder,
|
mNoteBuilder,
|
||||||
getResources(),
|
getResources(),
|
||||||
getNoteTag(),
|
getNoteTag(),
|
||||||
noteId);
|
getNoteId());
|
||||||
// Initializes this thread as a looper. HandlerThread.run() will be executed
|
// Initializes this thread as a looper. HandlerThread.run() will be executed
|
||||||
// in this thread.
|
// in this thread.
|
||||||
// This gives you a chance to create handlers that then reference this looper,
|
// 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) {
|
protected final void cancelNotification(int id/*TODO remove*/) {
|
||||||
mNotificationManager.cancel(getNoteTag(), id);
|
mNotificationManager.cancel(getNoteId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -52,7 +52,7 @@ public class ChronometerNotificationThread extends HandlerThread {
|
|||||||
@NonNull NotificationManager manager,
|
@NonNull NotificationManager manager,
|
||||||
@NonNull NotificationCompat.Builder builder,
|
@NonNull NotificationCompat.Builder builder,
|
||||||
@Nullable Resources resources,
|
@Nullable Resources resources,
|
||||||
@NonNull String noteTag,
|
@NonNull String noteTag, // TODO: remove param
|
||||||
int noteId) {
|
int noteId) {
|
||||||
super(TAG);
|
super(TAG);
|
||||||
mDelegate = delegate;
|
mDelegate = delegate;
|
||||||
@ -93,7 +93,7 @@ public class ChronometerNotificationThread extends HandlerThread {
|
|||||||
CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources);
|
CharSequence text = mDelegate.formatElapsedTime(SystemClock.elapsedRealtime(), mResources);
|
||||||
mNoteBuilder.setContentText(text);
|
mNoteBuilder.setContentText(text);
|
||||||
}
|
}
|
||||||
mNotificationManager.notify(mNoteTag, mNoteId, mNoteBuilder.build());
|
mNotificationManager.notify(mNoteId, mNoteBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -30,13 +30,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService
|
|||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
setContentTitle(getString(R.string.stopwatch));
|
||||||
mUpdateHandler = new AsyncLapsTableUpdateHandler(this, null);
|
mUpdateHandler = new AsyncLapsTableUpdateHandler(this, null);
|
||||||
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
mDelegate.init();
|
mDelegate.init();
|
||||||
mDelegate.setShowCentiseconds(true, false);
|
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
|
@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
|
// our thread has enough leeway to sneak in a final call to post the notification before it
|
||||||
// is actually quit().
|
// is actually quit().
|
||||||
// As such, try cancelling the notification with this (tag, id) pair again.
|
// As such, try cancelling the notification with this (tag, id) pair again.
|
||||||
cancelNotification(0);
|
// cancelNotification(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -103,6 +101,11 @@ public class StopwatchNotificationService extends ChronometerNotificationService
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getNoteId() {
|
||||||
|
return R.id.stopwatch_notification_service;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void handleDefaultAction(Intent intent, int flags, long startId) {
|
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?
|
// 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();
|
quitCurrentThread();
|
||||||
if (running) {
|
if (running) {
|
||||||
// startChronometer();
|
|
||||||
long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime());
|
long startTime = mPrefs.getLong(StopwatchFragment.KEY_START_TIME, SystemClock.elapsedRealtime());
|
||||||
startNewThread(0, startTime);
|
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"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<resources>
|
<resources>
|
||||||
<item type="id" name="ringtone_service_notification"/>
|
<item type="id" name="ringtone_service_notification"/>
|
||||||
|
<item type="id" name="stopwatch_notification_service"/>
|
||||||
|
<item type="id" name="timer_notification_service"/>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in New Issue
Block a user