Уведомления о состоянии Android - PullRequest
1 голос
/ 04 марта 2012

Я создал класс BackgroundService, который расширяет класс Service.

У меня есть таймер, который отслеживает продолжительность между проверками. В методе onCreate я установил его на 10 секунд, поэтому я ожидаю, что он отправляет уведомление о состоянии каждые 10 секунд. Проблема, с которой я сталкиваюсь, заключается в том, что каждые 10 секунд я слышу «звуковое» уведомление о состоянии, когда я его включаю, но не вижу текстовое напоминание в верхней части экрана, которое появляется только в первом уведомлении службы уведомлений. Кто-нибудь знает, как это исправить?

Я приложил большое количество исходного кода для этого класса ниже: Заранее спасибо!

таймер таймера;

private TimerTask updateTask = new TimerTask() {
    @Override
    public void run() {
        Log.i(TAG, "Timer task doing work!");   

        // Process junk here:
        sendNotification("Please leave in 5 min!!!");
    }
};

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Log.i(TAG, "Background Service creating");

    timer = new Timer("DurationTimer");
    timer.schedule(updateTask, 1000L, 10*1000L);
}

public void sendNotification(CharSequence message)
{
    // Execute Check and Notify
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    int icon = R.drawable.simon;
    CharSequence tickerText = message;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    Context context = getApplicationContext();
    CharSequence contentTitle = "LEAVE NOW!";
    CharSequence contentText = "LEAVE NOW!!";
    Intent notificationIntent = new Intent(this, BackgroundService.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.defaults |= Notification.DEFAULT_SOUND;
    //notification.defaults |= Notification.DEFAULT_VIBRATE;

    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    int HELLO_ID = 1;
    mNotificationManager.notify(HELLO_ID, notification);
}

1 Ответ

0 голосов
/ 04 марта 2012

Изменение значения contentText с каждой итерацией.

Пример:

CharSequence contentText = "LEAVE NOW!! " + System.currentTimeMillis();

Я думаю, вы обнаружите, что текст сообщения постоянно меняется, потому что у вас уже есть Уведомление с тем же идентификатором, что и Уведомление, которое вы хотите отправить. Так что получается, что текст просто меняется.

Другой способ:

mNotificationManager.clear(HELLO_ID);

Сделайте это, прежде чем создавать новое уведомление.

...