I am having a hard time figuring out how to display the current time every fixed time interval in the status bar using service with its own thread. The following code allows me to display the current time when I click the start service button (there is also a stop service button) but it does not display the current time again. I suspect some sort of loop or timer is needed or perhaps my thread sleep mechanism is not correct. Please hint or suggest a solution. The run method is where help is needed (first). Thanks much in advance!
public class MyOwnService extends Service {
// Use a layout id for a unique identifier
private static int TIME_NOTIFICATIONS = R.layout.status_bar_notifications;
// variable which controls the notification thread
private ConditionVariable mCondition;
private NotificationManager mNM;
// Create Runnable object
private Runnable mTask = new Runnable() {
public void run() {
try{
SimpleDateFormat sdfDate = new SimpleDateFormat("hh:mm:ss");
Date now = new Date();
String strDate = sdfDate.format(now);
showNotification(R.drawable.icon, strDate);
Thread.sleep(30000);
} catch (Exception e) {
}
// Show happy face for 5 seconds
//showNotification(R.drawable.icon, strDate);
//mCondition.block(5 * 1000);
// Done with our work... stop the service!
//MyOwnService.this.stopSelf();
}
};
@Override
public void onCreate() {
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
/// Start up the thread running the service. Note that we create a
/// separate thread because the service normally runs in the process's
/// main thread, which we don't want to block.
Thread notifyingThread = new Thread(
null, // Thread group
mTask, // Runnable object
"NotifyingService"); // Thread name
mCondition = new ConditionVariable(false);
notifyingThread.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
// Cancel the persistent notification.
mNM.cancel(MOOD_NOTIFICATIONS);
// Stop the thread from generating further notifications
mCondition.open();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private void showNotification(int timeId, String strTime) {
// In this sample, we'll use the same text for the ticker and the
// expanded notification
CharSequence text = strTime;
// Set the icon, scrolling text and time stamp.
// Note that in this example, we pass null for tickerText. We update the
// icon enough that
// it is distracting to show the ticker text every time it changes. We
// strongly suggest
// that you do this as well. (Think of of the "New hardware found" or
// "Network connection
// changed" messages that always pop up)
Notification notification = new Notification(timeId, text, System
.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ServiceLauncher.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this,
getText(R.string.status_bar_notification_title), text,
contentIntent);
// Send the notification.
// We use a layout id because it is a unique number. We use it later to
// cancel.
mNM.notify(TIME_NOTIFICATIONS, notification);
}
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new Binder() {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
return super.onTransact(code, data, reply, flags);
}
};
}
Run() is not a loop. You need to wrap your code in a while loop.
You also want to add a way to stop that thread gracefully so in your while loop you should check a variable that can be set from the main thread. ie
then add this to your onDestory()
mTask.stopped = true;