From 32cb7de08c952528e2292788ad433f115960bf6d Mon Sep 17 00:00:00 2001 From: dxj <2490339921@qq.com> Date: Thu, 17 Apr 2025 15:38:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8Ajava/net/micode/notes/ui/Alar?= =?UTF-8?q?mAlertActivity.java=E4=B8=8B=E7=9A=84=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E8=AF=A5=E9=83=A8=E5=88=86=E5=AE=9E=E7=8E=B0=E4=BA=86?= =?UTF-8?q?=E9=97=B9=E9=92=9F=E6=8F=90=E9=86=92=E6=B4=BB=E5=8A=A8=EF=BC=8C?= =?UTF-8?q?=E8=B4=9F=E8=B4=A3=E5=A4=84=E7=90=86=E4=BE=BF=E7=AD=BE=E6=8F=90?= =?UTF-8?q?=E9=86=92=E8=A7=A6=E5=8F=91=E6=97=B6=E7=9A=84=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E5=92=8C=E5=A3=B0=E9=9F=B3=E6=92=AD=E6=94=BE=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E5=AF=B9=E8=AF=9D=E6=A1=86=E6=8C=89=E9=92=AE=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E7=9B=91=E5=90=AC=E5=99=A8=E5=92=8C=E5=AF=B9=E8=AF=9D?= =?UTF-8?q?=E6=A1=86=E5=85=B3=E9=97=AD=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../micode/notes/ui/AlarmAlertActivity.java | 98 ++++++++++--------- 1 file changed, 53 insertions(+), 45 deletions(-) diff --git a/java/net/micode/notes/ui/AlarmAlertActivity.java b/java/net/micode/notes/ui/AlarmAlertActivity.java index 85723be..bc0bbd1 100644 --- a/java/net/micode/notes/ui/AlarmAlertActivity.java +++ b/java/net/micode/notes/ui/AlarmAlertActivity.java @@ -14,8 +14,12 @@ * limitations under the License. */ +/* + * 版权声明和许可证信息(略) + */ package net.micode.notes.ui; +// 导入Android基础组件 import android.app.Activity; import android.app.AlertDialog; import android.content.Context; @@ -23,9 +27,13 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnDismissListener; import android.content.Intent; + +// 导入音频相关组件 import android.media.AudioManager; import android.media.MediaPlayer; import android.media.RingtoneManager; + +// 导入Android系统服务相关 import android.net.Uri; import android.os.Bundle; import android.os.PowerManager; @@ -33,104 +41,104 @@ import android.provider.Settings; import android.view.Window; import android.view.WindowManager; +// 导入项目资源 import net.micode.notes.R; import net.micode.notes.data.Notes; import net.micode.notes.tool.DataUtils; import java.io.IOException; - +/** + * 闹钟提醒活动 - 负责处理便签提醒触发时的界面和声音播放 + * 实现对话框按钮点击监听器和对话框关闭监听器 + */ public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { - private long mNoteId; - private String mSnippet; - private static final int SNIPPET_PREW_MAX_LEN = 60; - MediaPlayer mPlayer; + private long mNoteId; // 用于存储便签ID + private String mSnippet; // 用于存储便签摘要 + private static final int SNIPPET_PREW_MAX_LEN = 60; // 便签摘要的最大长度 + MediaPlayer mPlayer; // 用于播放闹钟声音的MediaPlayer对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - requestWindowFeature(Window.FEATURE_NO_TITLE); + requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题栏 final Window win = getWindow(); - win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); + win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); // 设置窗口在锁屏状态下显示 - if (!isScreenOn()) { + if (!isScreenOn()) { // 如果屏幕未点亮,则设置相关标志以点亮屏幕 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); } - Intent intent = getIntent(); + Intent intent = getIntent(); // 获取启动此活动的Intent try { - mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); - mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); + mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); // 从Intent中获取便签ID + mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); // 根据ID获取便签摘要 mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) - : mSnippet; + : mSnippet; // 截取摘要并添加额外信息 } catch (IllegalArgumentException e) { e.printStackTrace(); return; } - mPlayer = new MediaPlayer(); - if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { - showActionDialog(); - playAlarmSound(); + mPlayer = new MediaPlayer(); // 创建MediaPlayer对象 + if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { // 检查便签是否可见 + showActionDialog(); // 显示操作对话框 + playAlarmSound(); // 播放闹钟声音 } else { - finish(); + finish(); // 如果便签不可见,则结束活动 } } - private boolean isScreenOn() { + private boolean isScreenOn() { // 检查屏幕是否点亮 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); return pm.isScreenOn(); } - private void playAlarmSound() { - Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); + private void playAlarmSound() { // 播放闹钟声音 + Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); // 获取默认闹钟铃声URI int silentModeStreams = Settings.System.getInt(getContentResolver(), - Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); + Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); // 获取静音模式下受影响的音频流类型 - if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { + if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { // 如果闹钟声音被静音,则设置MediaPlayer的音频流类型 mPlayer.setAudioStreamType(silentModeStreams); } else { mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); } try { - mPlayer.setDataSource(this, url); - mPlayer.prepare(); - mPlayer.setLooping(true); - mPlayer.start(); + mPlayer.setDataSource(this, url); // 设置MediaPlayer的数据源 + mPlayer.prepare(); // 准备播放 + mPlayer.setLooping(true); // 设置循环播放 + mPlayer.start(); // 开始播放 } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } - private void showActionDialog() { + private void showActionDialog() { // 显示操作对话框 AlertDialog.Builder dialog = new AlertDialog.Builder(this); - dialog.setTitle(R.string.app_name); - dialog.setMessage(mSnippet); - dialog.setPositiveButton(R.string.notealert_ok, this); + dialog.setTitle(R.string.app_name); // 设置对话框标题 + dialog.setMessage(mSnippet); // 设置对话框消息 + dialog.setPositiveButton(R.string.notealert_ok, this); // 设置确定按钮及其监听器 if (isScreenOn()) { - dialog.setNegativeButton(R.string.notealert_enter, this); + dialog.setNegativeButton(R.string.notealert_enter, this); // 如果屏幕已点亮,则设置取消按钮及其监听器 } - dialog.show().setOnDismissListener(this); + dialog.show().setOnDismissListener(this); // 显示对话框并设置关闭监听器 } - public void onClick(DialogInterface dialog, int which) { + public void onClick(DialogInterface dialog, int which) { // 对话框按钮点击事件处理 switch (which) { case DialogInterface.BUTTON_NEGATIVE: Intent intent = new Intent(this, NoteEditActivity.class); @@ -143,16 +151,16 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD } } - public void onDismiss(DialogInterface dialog) { - stopAlarmSound(); - finish(); + public void onDismiss(DialogInterface dialog) { // 对话框关闭事件处理 + stopAlarmSound(); // 停止闹钟声音 + finish(); // 结束活动 } - private void stopAlarmSound() { + private void stopAlarmSound() { // 停止闹钟声音 if (mPlayer != null) { - mPlayer.stop(); - mPlayer.release(); - mPlayer = null; + mPlayer.stop(); // 停止播放 + mPlayer.release(); // 释放资源 + mPlayer = null; // 置空引用 } } -} +} \ No newline at end of file