Domanda

I am almost finished with this toy app game I am making.

Problem: My notification is always showing my counter to be 30000. Why isn't it timing down?

What I have done: I have implemented a simple Service class and a custom timer to tick down. Eventually once I am sure the timer is working I will exit the entire game.

Here is my 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 this does when it runs is shows "A New Notification", and then changes to "herp counter: 30000". However, this notification never changes. It just stays 30000. Why? I thought I fixed this with making the flag ongoing?

È stato utile?

Soluzione 2

counter is not a reference; the notification will not update with its new value until you explicitly tell it to.

Have a look at the documentation on updating an existing notification. Your ID is 1337 here, so you can use that to update it.

In fact, you may just be able to call declareNotification() again from your onTick() method... If this doesn't work, however, I would suggest keeping a reference to the Notification object (as a member variable), then updating it, and use nm.notify(1337, /* your notification object */);.

Altri suggerimenti

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

No updating required, works off the setWhen() value

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

I don't know why you want to use a notification. But you need to keep updating your notification. For a simple fix add

declareNotification();

underneath this line:

counter = millisUntilFinished / 1000;

Note that this isn't a great way to code it. Really you should pass a method only updating the notification rather than "creating" a new one. However as long as they have the same ID, one will replace the other. Also just to use a more up to date way of managing notifications, use

NotificationCompat.builder b = new NotificationCompat.Builder(context);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top