diff --git a/src/net/micode/notes/ui/AlarmAlertActivity.java b/src/net/micode/notes/ui/AlarmAlertActivity.java index 85723be..b0aef34 100644 --- a/src/net/micode/notes/ui/AlarmAlertActivity.java +++ b/src/net/micode/notes/ui/AlarmAlertActivity.java @@ -38,21 +38,34 @@ import net.micode.notes.data.Notes; import net.micode.notes.tool.DataUtils; import java.io.IOException; +/** + * 闹钟提醒Activity - 当便签的提醒时间到达时显示 + * 主要功能: + * 1. 在锁屏状态下唤醒屏幕并显示提醒对话框 + * 2. 播放默认闹钟铃声 + * 3. 提供确认和进入便签编辑的操作选项 + */ public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { + // 便签ID和摘要信息 private long mNoteId; + // 摘要最大显示长度 private String mSnippet; private static final int SNIPPET_PREW_MAX_LEN = 60; + // 媒体播放器,用于播放闹钟声音 MediaPlayer mPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // 无标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); final Window win = getWindow(); + // 关键标志:即使锁屏也显示Activity win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); + // 如果屏幕处于关闭状态,自动点亮屏幕 if (!isScreenOn()) { win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON @@ -60,36 +73,55 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); } - + // 从Intent中获取便签ID和内容 Intent intent = getIntent(); try { + // 从URI路径中解析便签ID mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); + // 获取便签内容并截取摘要 mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) : mSnippet; } catch (IllegalArgumentException e) { + // 处理无效ID异常 e.printStackTrace(); return; } + // 初始化媒体播放器并检查便签有效性 mPlayer = new MediaPlayer(); if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { + // 便签存在且有效,显示提醒对话框并播放闹钟 showActionDialog(); playAlarmSound(); } else { + // 便签不存在,直接关闭Activity finish(); } } + /** + * 检查屏幕是否处于点亮状态 + */ private boolean isScreenOn() { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + // 注意:在Android 6.0及以上版本应使用isInteractive()方法 return pm.isScreenOn(); } + /** + * 播放闹钟声音 + * 处理逻辑: + * 1. 获取系统默认闹钟铃声URI + * 2. 根据系统设置决定使用的音频流 + * 3. 配置并启动MediaPlayer循环播放 + */ private void playAlarmSound() { + // 获取默认闹钟铃声URI Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); + // 获取系统设置中受静音模式影响的音频流 int silentModeStreams = Settings.System.getInt(getContentResolver(), Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); @@ -100,10 +132,13 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); } try { + // 设置数据源并准备播放 mPlayer.setDataSource(this, url); mPlayer.prepare(); + // 设置循环播放 mPlayer.setLooping(true); mPlayer.start(); + // 统一处理各种异常 } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -119,35 +154,58 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD } } + /** + * 显示提醒操作对话框 + * 包含: + * - 便签摘要内容 + * - 确认按钮(关闭提醒) + * - 进入按钮(如果屏幕已点亮,可直接进入便签编辑) + */ 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); + // 只有屏幕已点亮时才显示"进入"按钮 if (isScreenOn()) { dialog.setNegativeButton(R.string.notealert_enter, this); } + // 设置对话框关闭监听器并显示 dialog.show().setOnDismissListener(this); } + /** + * 处理对话框按钮点击事件 + */ public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_NEGATIVE: + // 进入便签编辑界面 Intent intent = new Intent(this, NoteEditActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.putExtra(Intent.EXTRA_UID, mNoteId); startActivity(intent); break; default: + // 默认处理(通常是确认按钮),在onDismiss中处理关闭逻辑 break; } } + + /** + * 对话框关闭时回调 + * 停止闹钟并关闭Activity + */ public void onDismiss(DialogInterface dialog) { stopAlarmSound(); finish(); } + + /** + * 停止闹钟声音播放并释放资源 + */ private void stopAlarmSound() { if (mPlayer != null) { mPlayer.stop();