Question

I am working on an application which would start a countDownTimer after receiving "ACTION_SCREEN_ON". I have the following Working Code. I just want to implement the countdown timer code in the place indicated.

package com.example.myapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Receiver extends BroadcastReceiver {
    public static final String TAG = "Receiver Tutorial";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
            Toast.makeText(context, "Screen is turned ON", Toast.LENGTH_LONG).show();
            // Place for CountDown Timer Code
        }
    }
}

Following is the CountDownTimer code I got from Android Developers Page.

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

But I don't know how to implement it into the first code.

Was it helpful?

Solution

Try following way,

public class Receiver extends BroadcastReceiver 
{
    public static final String TAG = "Receiver Tutorial";
    private CountDownTimer countDownTimer;
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

    countDownTimer = new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {

        }

        public void onFinish() {

        }
    };    

        if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
            Toast.makeText(context, "Screen is turned ON", Toast.LENGTH_LONG).show();
            countDownTimer.start();
        }
    }
}

OTHER TIPS

public class Receiver extends BroadcastReceiver {
    public static final String TAG = "Receiver Tutorial";
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null && Intent.ACTION_SCREEN_ON.equals(action)) {
            Toast.makeText(context, "Screen is turned ON", Toast.LENGTH_LONG).show();

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");

       }
      }.start();
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top