Мне трудно понять, как отображать текущее время каждый фиксированный интервал времени в строке состояния, используя сервис со своим собственным потоком.Следующий код позволяет мне отображать текущее время, когда я нажимаю кнопку запуска службы (есть также кнопка остановки службы), но оно не отображает текущее время снова.Я подозреваю, что необходим какой-то цикл или таймер, или, возможно, мой механизм ожидания потока не верен.Пожалуйста, намекните или предложите решение.Метод запуска - это то место, где нужна помощь (сначала).Большое спасибо заранее!
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);
}
};
}