我很难弄清楚如何使用具有自己线程的服务在状态栏中显示每个固定时间间隔的当前时间。以下代码允许我显示单击开始服务按钮时的当前时间(还有一个停止服务按钮),但它不会再次显示当前时间。我怀疑需要某种循环或计时器,或者我的线程睡眠机制可能不正确。请提示或建议解决方案。 run方法是需要帮助的地方(第一)。非常感谢!
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);
}
};
}
答案 0 :(得分:1)
Run()不是循环。您需要将代码包装在while循环中。
您还想添加一种方法来优雅地停止该线程,因此在您的while循环中,您应该检查可以从主线程设置的变量。即
while(!stopped){ ... }
然后将此添加到onDestory()
mTask.stopped = true;