0

I am trying to create a countdown timer that works with Android 2.2 (API 8). I found this code but it only works with API 9 and above. How can I make this code work in API 8?

  //API 9 and above
public class CountClass extends CountDownTimer{
    public CountClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }





    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

        txtchronoview.setText("Complete.");
        Toast.makeText(calculator1.this,
                    "Call a judge, your opponent is exceed its 3 minutes limit per turn", Toast.LENGTH_SHORT).show();
        vibrate(3000);

        //call();
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

        long millis = millisUntilFinished;
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), 
                (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis)) / 60000 ), //DISPLAY 3 MINUTE COUNT DOWN 
                //(TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis)) / 60 ), //DISPLAY 1 MINUTE COUNT DOWN CYCLE FOR 3 TIMES
                //(TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toMinutes(millis))  ), //DISPLAY EVERYTHING IN MILLISECONDS FOR 3 MINUTES 
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)) );
        System.out.println(hms);
        txtchronoview.setText(hms);


    }

}
  • Do you need to support the 0.4% of devices that are on API 8 currently? https://developer.android.com/about/dashboards/index.html – weston Jan 22 '15 at 13:55
  • 1
    Well, I understand that it is few phones out there but if I create something from API 8 I think it will work with newer phones as well or am I wrong? – user3128770 Jan 22 '15 at 14:01
  • 1
    It's **not** a `few phones`. 0.4% is still **several hundreds of thousands devices**. – Phantômaxx Jan 22 '15 at 14:03
  • 1
    Remove `@TargetApi(Build.VERSION_CODES.GINGERBREAD)`. http://developer.android.com/reference/android/os/CountDownTimer.html – Phantômaxx Jan 22 '15 at 14:03
  • Yes, I'm saying if you create from API9, it will just exclude 0.4% of devices. – weston Jan 22 '15 at 14:06
  • `Just exclude` **several hundreds thousands devices**. Interesting. – Phantômaxx Jan 22 '15 at 14:11
  • @DerGolem android activations exceed 1 billion, so actually it's > 4 million. Even so, I wouldn't spend any dev effort on getting 0.4% more downloads from a diminishing user base who are too cheap to upgrade. – weston Jan 22 '15 at 14:14
  • @weston If I would earn just as little as **10 cents on each of these 4 millions**, I'd be very happy to support them. – Phantômaxx Jan 22 '15 at 14:17
  • @DerGolem oh I see, 10c on each! Well I'd rather earn 1c on the other 996 million! – weston Jan 22 '15 at 14:21
  • @weston I'm **not excluding** the other 996 millions. I want them **all**! I'm **GREEDY**!! – Phantômaxx Jan 22 '15 at 14:26
  • @DerGolem I know you're not excluding them, just saying wouldn't you rather spend your limited dev resource boosting the revenue in the far larger piece of the pie? That's what a greedy dev does! – weston Jan 22 '15 at 14:36
  • @DerGolem in this case though, I think you are right, they should be able to just remove the attribute. You should put that as answer. – weston Jan 22 '15 at 14:38
  • I understand, but my future goals is that I want to support them because the final goal of my app is to be able to be used my all of them and I would charge for the app. But, before that happens I need to understand more about android dev. You could say that is not a good practice but I feel when I accomplish my goal my target audience will be people that can't afford new devices. If they can afford it will still be good because newer phones can run codes that was design for API 8 and newer. – user3128770 Jan 22 '15 at 16:15
  • Thank you all for your for your input. Its been a great help. – user3128770 Jan 22 '15 at 19:24

1 Answers1

1

use this for min and sec, with little bit change u can add hour.

public String milisecToMinAndSec(long milisec) {
        int sec = (int) milisec / 1000;
        String outString = "";
        if (sec > 60)
            outString = String.format("%02d",(int) (sec / 60)) + ":" + String.format("%02d",(int) (sec % 60));
        else
            outString = "00:" + String.format("%02d",sec);

        return outString;
    }
Sepehr Nozaryian
  • 370
  • 4
  • 15