MainActivity.java:

package com.example.myapplication;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity
        implements SensorEventListener {

    TextView textView;
    SensorManager sensorManager;
    Sensor accelerometer;

    // Previous reading (used to detect a shake)
    float lastX, lastY, lastZ;

    // Reference orientation (used to detect a tilt)
    float baseX, baseY, baseZ;

    boolean firstReading = true;
    long lastChangeTime = 0;

    final Random random = new Random();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);

        sensorManager =
                (SensorManager) getSystemService(SENSOR_SERVICE);

        accelerometer =
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        if (accelerometer == null) {
            textView.setText("Accelerometer not available");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        firstReading = true;

        if (accelerometer != null) {
            sensorManager.registerListener(
                    this,
                    accelerometer,
                    SensorManager.SENSOR_DELAY_NORMAL
            );
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        if (firstReading) {
            lastX = x;
            lastY = y;
            lastZ = z;
            baseX = x;
            baseY = y;
            baseZ = z;
            firstReading = false;
            return;
        }

        // Sudden change between two readings = shake
        float shake = Math.abs(x - lastX)
                + Math.abs(y - lastY)
                + Math.abs(z - lastZ);

        // Slow change away from the reference orientation = tilt
        float tilt = Math.abs(x - baseX)
                + Math.abs(y - baseY)
                + Math.abs(z - baseZ);

        long now = System.currentTimeMillis();

        // FIX: detects tilt as well as shake, with a 700 ms debounce
        if ((shake > 15 || tilt > 6) && now - lastChangeTime > 700) {

            changeBackground(shake > 15 ? "Phone Shaken!" : "Phone Tilted!");

            lastChangeTime = now;

            // new reference orientation after each change
            baseX = x;
            baseY = y;
            baseZ = z;
        }

        lastX = x;
        lastY = y;
        lastZ = z;
    }

    private void changeBackground(String message) {

        int color = Color.rgb(
                random.nextInt(256),
                random.nextInt(256),
                random.nextInt(256)
        );

        findViewById(android.R.id.content)
                .setBackgroundColor(color);

        textView.setText(message);
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shake the Phone"
        android:textSize="25sp"/>


</LinearLayout>
