4

私が作っているこのおもちゃのアプリゲームはほぼ完成しています。

問題: 通知で常にカウンターが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);

    }

}

これは、実行時に「A New Notification」を表示し、「herp counter:30000」に変更するだけです。ただし、この通知は変更されません。それはちょうど30000のままです。なぜですか?フラグを継続させることでこれを修正したと思いましたか?

4

3 に答える 3

21

https://developer.android.com/reference/android/app/Notification.Builder.html#setUsesChronometer(ブール値)

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");
于 2016-07-06T22:37:45.247 に答える
4

counter参照ではありません。明示的に指示するまで、通知は新しい値で更新されません。

既存の通知の更新に関するドキュメントをご覧ください。ID が1337ここにあるので、それを使用して更新できます。

declareNotification()実際、メソッドから再度呼び出すことができる場合がありますonTick()...ただし、これが機能しない場合は、Notificationオブジェクトへの参照を (メンバー変数として) 保持し、それを更新して、nm.notify(1337, /* your notification object */);.

于 2012-12-09T00:56:51.570 に答える
1

通知を使用する理由がわかりません。ただし、通知を更新し続ける必要があります。簡単な修正の追加

declareNotification();

この行の下:

counter = millisUntilFinished / 1000;

これは、コーディングするのに最適な方法ではないことに注意してください。実際には、新しい通知を「作成」するのではなく、通知を更新するだけのメソッドを渡す必要があります。ただし、ID が同じである限り、一方が他方を置き換えます。また、通知を管理する最新の方法を使用するには、次を使用します

NotificationCompat.builder b = new NotificationCompat.Builder(context);
于 2012-12-09T01:15:45.853 に答える