android - 如何更新我的通知以显示我的时间计数器发生变化?

标签 android android-intent android-service android-3.0-honeycomb android-notifications

我即将完成我正在制作的这个玩具应用游戏。

问题: 我的通知总是显示我的计数器为 30000。为什么它没有计时?

我做了什么: 我已经实现了一个简单的服务类和一个自定义计时器来计时。最后,一旦我确定计时器正常工作,我就会退出整个游戏。

这是我的代码:

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);

    }

}

它运行时所做的一切就是显示“A New Notification”,然后更改为“herp counter: 30000”。但是,此通知永远不会更改。它只是保持 30000。为什么?我以为我通过让旗帜持续存在来解决这个问题?

最佳答案

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

Show the when field as a stopwatch. Instead of presenting when as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call). The counter can also be set to count down to when when using 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");

关于android - 如何更新我的通知以显示我的时间计数器发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783540/

相关文章:

java - 将手指滑入按钮 android

android - 如何在 Android 动态壁纸上添加文字

编辑器中的 Android 布局奇怪的 TextView 行为

Java extras 包返回空值

android - 在 android 中获取 4.1 及更高版本的通知

android - Flutter - 如何使用包含表单的可扩展面板构建 ListView ?

android - 如何通过使用 Intent 导航到 'android.intent.action.MAIN' Activity ?

android-intent - Netflix Android TV 应用程序 (com.netflix.ninja) 的电影深度链接

android - 在 KeyguardRestrictedInputMode() 中返回失败结果

android - Android 服务可以提供两个接口(interface)进行通信吗?