How to send notification with a delay when a condition is met in Android Studio


I’m creating an app which I want me to send a notification when I’m a certain amount of time awake.

I have done this by calculation the of acceleration the phone and adding this to in total number. This should be some kind of threshold to activate the timer.

I was beable to sent a notification when a button is pressed.

But now I would want to sent this notification when ‘accelerationTotalValue > 1000’ for example.

Since this is my first project in Android Studio and using java I’m fairly new to all of this.

My java activity

public class calibration extends AppCompatActivity {

    TextView txt_currentAccel, txt_prevAccel, txt_acceleration;
    TextView txt_totalAccel;
    ProgressBar prog_shakeMeter;

    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private double accelerationPreviousValue;
    private double accelerationTotalValue;

    Button notifyBtn;

    protected void onCreate(Bundle savedInstanceState) {

        // get layout ID's acceleration
        txt_acceleration = findViewById(;
        txt_prevAccel = findViewById(;
        txt_currentAccel = findViewById(;
        txt_totalAccel = findViewById(;
        prog_shakeMeter = findViewById(;

        // get info sensors
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        // notification
            // open main page when clicking on notification
        Intent resultIntent = new Intent(this, MainActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

            // find button to activate notification
        notifyBtn = findViewById(;

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("My Notification","My Notification", NotificationManager.IMPORTANCE_DEFAULT);
            NotificationManager manager = getSystemService(NotificationManager.class);

        notifyBtn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(calibration.this,"My Notification");
                builder.setContentTitle("My Title");
                builder.setContentText("This is a simple notification");

                NotificationManagerCompat managerCompat = NotificationManagerCompat.from(calibration.this);



    // calculation acceleration
    private final SensorEventListener sensorEventListener = new SensorEventListener() {
        public void onSensorChanged(SensorEvent sensorEvent) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];

            double accelerationCurrentValue = Math.sqrt((x * x + y * y + z * z));

            double changeIntAcceleration = Math.abs(accelerationCurrentValue - accelerationPreviousValue);
            accelerationPreviousValue = accelerationCurrentValue;
            accelerationTotalValue = accelerationTotalValue + changeIntAcceleration;

            txt_currentAccel.setText("Current = "+ accelerationCurrentValue);
            txt_prevAccel.setText("Prev = "+ accelerationCurrentValue);
            txt_acceleration.setText("Acceleration change = "+ changeIntAcceleration);
            txt_totalAccel.setText("Total = "+ accelerationTotalValue);


        public void onAccuracyChanged(Sensor sensor, int i) {


    protected void onResume() {
        mSensorManager.registerListener(sensorEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);

    protected void onPause() {


You could just put all the stuff that you have in your notifyBtn onClick to a separate function, let’s call it notify, and then add one line in your code:

public void onSensorChanged(SensorEvent sensorEvent) {
    // Your previous code
    accelerationTotalValue = accelerationTotalValue + changeIntAcceleration;
    if (accelerationTotalValue > 1000) {
    // Rest of your code

Do bear in mind that your current code will only run when your application is in the foreground. I’d suggest reading up on Android’s Activity Lifecycle here.

Answered By – Jorn Rigter

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published