如何更新我的通知以显示我的时间计数器更改?

Gee*_*ega 3 android android-intent android-service android-notifications android-3.0-honeycomb

我差不多完成了我正在制作的玩具应用游戏.

问题: 我的通知总是显示我的计数器是30000.为什么不是时间减少?

我做了什么: 我已经实现了一个简单的Service类和一个自定义计时器来记下来.最终,一旦我确定计时器正在运行,我将退出整个游戏.

这是我的代码:

package com.example.redshirtmarblez;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.widget.Toast;

public class TimingService extends Service{

    public long counter = 30000;
    private Context ctx;
    private Activity localActivity;


    private NotificationManager nm;

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

    @Override
    public void onCreate() 
    {
          super.onCreate();
          nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
          declareNotification();
          timer.start();
          //Toast.makeText(this, "Timer is :" + counter, Toast.LENGTH_LONG).show();
          //showNotification();
    }


    public void getActivity(Activity activity)
    {
        localActivity = activity;
    }

    //count to end the game
    public CountDownTimer timer = new CountDownTimer(30000, 1000){

        public void onTick(long millisUntilFinished){
            counter = millisUntilFinished / 1000;
        }

        public void onFinish(){
             counter = 0;


 //Kill the game 
             int i = android.os.Process.myPid();
             android.os.Process.killProcess(i);

        }

    };

    /*
     * Show a notification while this service is running 
     */
    public void declareNotification()
    {
        //Declare a new notification 
        Notification notification = new Notification(R.drawable.ic_launcher, "A New Notification", System.currentTimeMillis());

        notification.flags |= Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

        Intent intent = new Intent(this, TimingService.class);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        notification.setLatestEventInfo(this, "herp", "counter: " + counter, activity);


  //This is clearly not 1337, but a good joke
            startForeground(1337, notification);

    }

}
Run Code Online (Sandbox Code Playgroud)

所有这一切都在运行时显示"新通知",然后更改为"herp counter:30000".但是,此通知永远不会更改.它只剩下30000.为什么?我以为我修正了这个标志正在进行中?

beh*_*lit 15

https://developer.android.com/reference/android/app/Notification.Builder.html#setUsesChronometer(boolean)

将when字段显示为秒表.通知将显示自何时开始的分钟和秒的自动更新显示,而不是在何时作为时间戳显示.显示已用时间(如正在进行的电话呼叫)时非常有用.计数器也可以设置为倒计时时使用setChronometerCountDown(boolean).

不需要更新,取消了setWhen()价值

NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_default_notification)
        .setTicker("Ticker Text")  
        .setWhen(when)  // the time stamp, you will probably use System.currentTimeMillis() for most scenarios
        .setUsesChronometer(true)
        .setContentTitle("Title Text")
        .setContentText("Content Text");
Run Code Online (Sandbox Code Playgroud)

  • 是否可以通过这种方式在**通知标题**中显示时间戳?我对手动更新感觉不太好。 (2认同)