一共用时四天
主要目的是让自己的生活更规律
良好的作息是复习的最大帮助
<!-- AndroidManifest.xml --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.mortal"> <uses-permission android:name="android.permission.SET_ALARM" /> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@drawable/charleen" android:label="@string/app_name" android:roundIcon="@drawable/charleen" android:supportsRtl="true" android:theme="@style/Theme.Mortal" tools:targetApi="31"> <service android:name=".RingtoneTimer" android:enabled="true" android:exported="false" /> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.mortal; import android.content.Intent; import android.os.Bundle; import android.os.CountDownTimer; import android.text.method.ScrollingMovementMethod; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity { private RingtoneTimer ringtoneTimer; private CountDownTimer countDownTimer; private TextView textView_time; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView_time = findViewById(R.id.textView_time); // 创建一个倒计时定时器 // 倒计时99999999秒,每隔500毫秒更新一次时间 countDownTimer = new CountDownTimer(99999999, 500) { @Override public void onTick(long millisUntilFinished) { // 更新剩余时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); final String currentTime = sdf.format(new Date()); runOnUiThread(new Runnable() { @Override public void run() { textView_time.setText(currentTime); } }); } @Override public void onFinish() { // 定时器结束时的操作 runOnUiThread(new Runnable() { @Override public void run() { textView_time.setText("倒计时结束"); } }); } }; // 启动倒计时定时器 countDownTimer.start(); TextView textView2 = findViewById(R.id.textView2); // 假设你的 TextView 的 id 是 textView2 TextView textView_show = findViewById(R.id.textView_show); textView_show.setMovementMethod(ScrollingMovementMethod.getInstance()); ringtoneTimer = new RingtoneTimer(textView2, textView_show); ringtoneTimer.startTimer(); startService(new Intent(this, RingtoneTimer.class)); // 启动后台服务 } }
package com.example.mortal; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class RingtoneTimer extends Service { private TextView textView2; // 用于显示最近广播信息的TextView private TextView textView_show; // 用于显示播报时间的TextView private Timer timer; // 计时器 private Date executionTime0715; // 执行铃声的时间 private MediaPlayer mediaPlayer0715; // 铃声播放器 public MediaPlayer mediaPlayer0930;//break public MediaPlayer mediaPlayer1120;//kitchen help public MediaPlayer mediaPlayer1300;//play a lit public MediaPlayer mediaPlayer1400;//noon break public MediaPlayer mediaPlayer1530;//gte up and play public MediaPlayer mediaPlayer2100;//relax public MediaPlayer mediaPlayer2200;//go to bed public MediaPlayer mediaPlayer2300;//sleep private Handler mainHandler; // 用于在主线程更新UI @Override public IBinder onBind(Intent intent) { return null; } public RingtoneTimer() { // 无参数构造函数的实现代码 } public RingtoneTimer(TextView textView2, TextView textView_show) { this.textView2 = textView2; this.textView_show = textView_show; // 创建MediaPlayer并设置音频源为raw/sound0830 mediaPlayer0830 = MediaPlayer.create(textView2.getContext(), R.raw.sound0830); //get up mediaPlayer0830 = MediaPlayer.create(textView2.getContext(), R.raw.sound0830); mediaPlayer0930 = MediaPlayer.create(textView2.getContext(), R.raw.sound0930); //break mediaPlayer0945 = MediaPlayer.create(textView2.getContext(), R.raw.sound0945); //recover mediaPlayer1120 = MediaPlayer.create(textView2.getContext(), R.raw.sound1120); //kitchen help mediaPlayer1300 = MediaPlayer.create(textView2.getContext(), R.raw.sound1300); //play a lit mediaPlayer1400 = MediaPlayer.create(textView2.getContext(), R.raw.sound1400); //noon break mediaPlayer1530 = MediaPlayer.create(textView2.getContext(), R.raw.sound1530); //gte up and play mediaPlayer1600 = MediaPlayer.create(textView2.getContext(), R.raw.sound1600); mediaPlayer1750 = MediaPlayer.create(textView2.getContext(), R.raw.sound1750); //break mediaPlayer1800 = MediaPlayer.create(textView2.getContext(), R.raw.sound1800); //downstairs mediaPlayer1900 = MediaPlayer.create(textView2.getContext(), R.raw.sound1900); mediaPlayer2100 = MediaPlayer.create(textView2.getContext(), R.raw.sound2100); //relax mediaPlayer2200 = MediaPlayer.create(textView2.getContext(), R.raw.sound2200); mediaPlayer2300 = MediaPlayer.create(textView2.getContext(), R.raw.sound2300); // 创建主线程Handler mainHandler = new Handler(Looper.getMainLooper()); } /** * 启动定时器 */ public void startTimer() { timer = new Timer(); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 7); // 设置为每天7点钟 calendar.set(Calendar.MINUTE, 15); calendar.set(Calendar.SECOND, 0); Date executionTime0715 = calendar.getTime(); // 设置执行时间为当天7:15 calendar.set(Calendar.HOUR_OF_DAY, 8); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); Date executionTime0830 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 9); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); Date executionTime0930 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 9); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 45); calendar.set(Calendar.SECOND, 0); Date executionTime0945 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 11); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 20); calendar.set(Calendar.SECOND, 0); Date executionTime1120 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 13); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime1300 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 14); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime1400 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 15); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); Date executionTime1530 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 16); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime1600 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 17); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 50); calendar.set(Calendar.SECOND, 0); Date executionTime1750 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 18); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime1800 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 19); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime1900 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 21); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime2100 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 22); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime2200 = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, 23); // 设置为每天点钟 calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date executionTime2300 = calendar.getTime(); TimerTask task0715 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer0715.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "起床"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); // 使用System.currentTimeMillis()代替new Date() textView_show.append("07:15已播报 " + currentTime); } }; TimerTask task0830 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer0830.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + ""); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("08:30已播报 " + currentTime); } }; TimerTask task0930 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer0930.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "休息"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("09:30已播报 " + currentTime); } }; TimerTask task0945 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer0945.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + ""); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("09:45已播报 " + currentTime); } }; TimerTask task1120 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1120.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "厨房帮忙"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("11:20已播报 " + currentTime); } }; TimerTask task1300 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1300.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "休息"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("13:00已播报 " + currentTime); } }; TimerTask task1400 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1400.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "午觉"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("14:00已播报 " + currentTime); } }; TimerTask task1530 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1530.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "起床休息一会儿"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("15:30已播报 " + currentTime); } }; TimerTask task1600 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1600.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + ""); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("16:00已播报 " + currentTime); } }; TimerTask task1750 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1750.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "休息"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("17:50已播报 " + currentTime); } }; TimerTask task1800 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1800.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "下"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("18:00已播报 " + currentTime); } }; TimerTask task1900 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer1900.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "英语"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("19:00已播报 " + currentTime); } }; TimerTask task2100 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer2100.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "放松休息"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("21:00已播报 " + currentTime); } }; TimerTask task2200 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer2200.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "上床"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("22:00已播报 " + currentTime); } }; TimerTask task2300 = new TimerTask() { @Override public void run() { // 触发铃声动作 mediaPlayer2300.start(); // 更新UI mainHandler.post(new Runnable() { @Override public void run() { textView2.setText("最近广播:" + "熄灯"); } }); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = sdf.format(System.currentTimeMillis()); textView_show.append("23:00已播报 " + currentTime); } }; long interval = 24 * 60 * 60 * 1000; // 一天的毫秒数 long delay0715 = executionTime0715.getTime() - System.currentTimeMillis(); if (delay0715 < 0) { delay0715 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay0830 = executionTime0830.getTime() - System.currentTimeMillis(); if (delay0830 < 0) { delay0830 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay0930 = executionTime0930.getTime() - System.currentTimeMillis(); if (delay0930 < 0) { delay0930 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay0945 = executionTime0945.getTime() - System.currentTimeMillis(); if (delay0945 < 0) { delay0945 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1120 = executionTime1120.getTime() - System.currentTimeMillis(); if (delay1120 < 0) { delay1120 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1300 = executionTime1300.getTime() - System.currentTimeMillis(); if (delay1300 < 0) { delay1300 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1400 = executionTime1400.getTime() - System.currentTimeMillis(); if (delay1400 < 0) { delay1400 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1530 = executionTime1530.getTime() - System.currentTimeMillis(); if (delay1530 < 0) { delay1530 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1600 = executionTime1600.getTime() - System.currentTimeMillis(); if (delay1600 < 0) { delay1600 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1750 = executionTime1750.getTime() - System.currentTimeMillis(); if (delay1750 < 0) { delay1750 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1800 = executionTime1800.getTime() - System.currentTimeMillis(); if (delay0715 < 0) { delay0715 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay1900 = executionTime1900.getTime() - System.currentTimeMillis(); if (delay1900 < 0) { delay1900 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay2100 = executionTime2100.getTime() - System.currentTimeMillis(); if (delay2100 < 0) { delay2100 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay2200 = executionTime2200.getTime() - System.currentTimeMillis(); if (delay2200 < 0) { delay2200 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } long delay2300 = executionTime2300.getTime() - System.currentTimeMillis(); if (delay2300 < 0) { delay2300 += interval; // 如果当前时间已经过了执行时间,则延迟到下一天的执行时间 } timer.scheduleAtFixedRate(task0715, delay0715, interval); timer.scheduleAtFixedRate(task0830, delay0830, interval); timer.scheduleAtFixedRate(task0930, delay0930, interval); timer.scheduleAtFixedRate(task0945, delay0945, interval); timer.scheduleAtFixedRate(task1120, delay1120, interval); timer.scheduleAtFixedRate(task1300, delay1300, interval); timer.scheduleAtFixedRate(task1400, delay1400, interval); timer.scheduleAtFixedRate(task1530, delay1530, interval); timer.scheduleAtFixedRate(task1600, delay1600, interval); timer.scheduleAtFixedRate(task1750, delay1750, interval); timer.scheduleAtFixedRate(task1800, delay1800, interval); timer.scheduleAtFixedRate(task1900, delay1900, interval); timer.scheduleAtFixedRate(task2100, delay2100, interval); timer.scheduleAtFixedRate(task2200, delay2200, interval); timer.scheduleAtFixedRate(task2300, delay2300, interval); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="136dp" android:layout_marginTop="16dp" android:layout_marginEnd="137dp" android:text="等待首次播报..." android:textColor="#673AB7" android:textSize="20sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView4" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="125dp" android:layout_marginTop="16dp" android:layout_marginEnd="118dp" android:text="已启动广播程序" android:textColor="#673AB7" android:textSize="24sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView_time" android:layout_width="134dp" android:layout_height="85dp" android:layout_marginStart="136dp" android:layout_marginEnd="136dp" android:layout_marginBottom="24dp" android:text="时间" android:textAlignment="center" android:textColor="#673AB7" android:textSize="16sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" /> <TextView android:id="@+id/textView_show" android:layout_width="318dp" android:layout_height="395dp" android:layout_marginStart="44dp" android:layout_marginTop="144dp" android:layout_marginEnd="44dp" android:maxLines="20" android:textAlignment="center" android:textColor="#673AB7" android:textSize="14sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView4" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="159dp" android:layout_marginTop="39dp" android:layout_marginEnd="156dp" android:text="播报记录" android:textAlignment="textStart" android:textColor="#673AB7" android:textSize="24sp" android:textStyle="bold" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" /> </androidx.constraintlayout.widget.ConstraintLayout>
plugins { id("com.android.application") } android { namespace = "com.example.mortal" compileSdk = 33 defaultConfig { applicationId = "com.example.mortal" minSdk = 24 targetSdk = 33 versionCode = 1 versionName = "1.0" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { isMinifyEnabled = false proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } } dependencies { implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.8.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") testImplementation("junit:junit:4.13.2") androidTestImplementation("androidx.test.ext:junit:1.1.5") androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1") }