Wie aktualisiere ich meine Benachrichtigung zu zeigen, dass meine Zeit counter ändern?

Ich bin fast fertig mit diesem Spielzeug app Spiel, das ich mache.

Problem:
Meine Meldung ist immer zeigt mein counter zu 30000. Warum ist es nicht das timing nach unten?

, Was ich getan habe:
Ich habe implementiert eine einfache Service-Klasse und ein custom-timer zu ticken runter. Irgendwann einmal bin ich sicher, dass der timer funktioniert, werde ich beenden das gesamte Spiel.

Hier ist mein code:

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

    }

}

All dies nicht, wenn es ausgeführt wird, zeigt "Eine Neue Mitteilung" und dann ändert sich zu "herp-Zähler: 30000". Jedoch, diese Meldung ändert sich nie. Es bleibt nur 30000. Warum? Ich dachte, ich fixe das mit der Flagge Laufenden?

  • Dies nicht tun -> android.os.Process.killProcess(i); es ist wirklich eine schlechte Praxis in Android. Auch 0 ist kein zulässiger Wert für die flags parameter der PendingIntent.getActivity(...)
  • Danke. Der einzige Grund, warum ich es Tat, das ist, weil, wenn ich Aktivität.finish(), die Anwendung gibt mir eine Fehlermeldung, die sagen, es weiß nicht, warum die app Herunterfahren. Ich weiß, es gibt Tonnen von Gründen, dies nicht zu tun, im Allgemeinen. Dies ist nur ein toyapp und ich bin Herumspielen mit Ideen.
InformationsquelleAutor GeekyOmega | 2012-12-09
Schreibe einen Kommentar