0

我幾乎完成了我正在製作的這個玩具應用程序遊戲。如何更新我的通知以顯示我的時間計數器更改?

問題: 我的通知總是顯示我的計數器是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); 

    } 

} 

當它運行是顯示「新的通知」,再變爲「HERP櫃檯:30000」這一切呢。但是,這個通知不會改變。它只保持30000.爲什麼?我想我已經解決了這個問題,讓國旗正在進行中?

+1

不這樣做 - >'android.os.Process.killProcess(I);'是在'Android'非常糟糕的做法。另外'0'不是'PendingIntent.getActivity(...)'''flags'參數的有效值' – Squonk

+0

謝謝。我這樣做的唯一原因是因爲當我使用activity.finish()時,應用程序給我一個錯誤,說它不知道應用程序關閉的原因。我知道有很多理由不這樣做。這只是一個toyapp,我正在玩弄想法。 – GeekyOmega

回答

3

counter不是一個參考;在您明確告訴它之前,通知不會更新其新值。

看一看the documentation on updating an existing notification。您的ID是1337,因此您可以使用它來更新它。

實際上,您可能只需要再次從onTick()方法中調用declareNotification()即可......但是,如果這不起作用,我會建議保留對對象(作爲成員變量)的引用,然後更新它,並使用nm.notify(1337, /* your notification object */);

+0

謝謝。是啊。我只是決定學習如何使用通知。我只給兩個。一個告訴用戶遊戲在計時器上。它在關閉之前更新一次。 – GeekyOmega

1

我不知道你爲什麼要使用通知。但是你需要不斷更新你的通知。對於一個簡單的解決增加

declareNotification(); 

這條線之下:

counter = millisUntilFinished/1000; 

請注意,這不是一個偉大的方式來編寫的。真的,你應該通過一種方法只更新通知,而不是「創建」一個新的。但只要他們具有相同的ID,就可以取代另一個ID。 也只是用更高達管理通知的日期的方式,使用

NotificationCompat.builder b = new NotificationCompat.Builder(context); 
+0

謝謝。我upvoted並給了一些代表。感謝你的幫助。 – GeekyOmega

7

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

秀場時,作爲一個秒錶。而不是呈現何時作爲 時間戳,通知將顯示從何時開始自動更新 分鐘和秒數。顯示 已用時間(如正在進行的電話)時很有用。當使用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"); 
+0

是否可以通過這種方式在**通知標題**中顯示時間戳?我沒有很好的手動更新它的感覺。 – cuddlecheek