我幾乎完成了我正在製作的這個玩具應用程序遊戲。如何更新我的通知以顯示我的時間計數器更改?
問題: 我的通知總是顯示我的計數器是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.爲什麼?我想我已經解決了這個問題,讓國旗正在進行中?
不這樣做 - >'android.os.Process.killProcess(I);'是在'Android'非常糟糕的做法。另外'0'不是'PendingIntent.getActivity(...)'''flags'參數的有效值' – Squonk
謝謝。我這樣做的唯一原因是因爲當我使用activity.finish()時,應用程序給我一個錯誤,說它不知道應用程序關閉的原因。我知道有很多理由不這樣做。這只是一個toyapp,我正在玩弄想法。 – GeekyOmega