Вопрос

I have a class extends countdowntimer

i want to make a toast in onfinish(),which shows up every time the onfinish() function called from any other class in any object instantiated from it

how can i do that ?

package com.fawzyx.exams_countdowntimer;

import android.os.CountDownTimer;
import android.text.InputFilter.LengthFilter;
import android.widget.Toast;

public class CountDown extends CountDownTimer {

    public CountDown(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
/*
millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
countDownInterval : The interval along the way to receive onTick(long) callbacks.

*/


    }

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

        Toast.makeText(getApplicationContext() ,"Done", Toast.LENGTH_LONG);

    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    }

}
Это было полезно?

Решение

You need to pass the context to the CounteDown class

new CountDown(MyActivityName.this,otherparams);

Now use the context passed to display the toast

Context context;
public CountDown(Context context,long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.context = context;
}

Then in onFinish()

Toast.makeText(context," Message", Toast.LENGTH_SHORT).show();

Другие советы

Try this:

public class CountDown extends CountDownTimer {

    private Context context;

    public CountDown(Context context, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.context = context;
    }
    @Override
    public void onFinish() {
        Toast.makeText(context ,"Done", Toast.LENGTH_LONG).show();

    }
}

Two issues:

  1. You need to pass in a Context e.g. as a constructor arg:

    private Context mContext;
    
    public CountDown(Context context, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        mContext = context;
    

    Pass in this mContext to the Toast.

  2. After Toast.makeText(), call show() to actually display it.

Use it like this:

public class CountDown extends CountDownTimer {

   Context context;
    public CountDown(long millisInFuture, long countDownInterval , Context ctx) {
        super(millisInFuture, countDownInterval);

  this.context = ctx;

/*
millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
countDownInterval : The interval along the way to receive onTick(long) callbacks.

*/
 }

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

        Toast.makeText(context ,"Done", Toast.LENGTH_LONG);

    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub

    }

}

Call its construtor while the calling Activitycontext wherever we want countdown class

A simpler way to do this is to overwrite the onStop and use getApplicationContext(). Then the toast will appear when the activity closes.

@Override
protected void onStop() {
    super.onStop();
    Toast toast = Toast.makeText(getApplicationContext(),"Toast message here",Toast.LENGTH_SHORT);
    toast.show();
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top