fish
1 week ago
parent f5dd6eebf6
commit 641bcc5bd1

@ -1,17 +1,15 @@
/*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* (c) 2010-2011MiCode (www.micode.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Apache License, Version 2.0
* 使
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
*/
package net.micode.notes.ui;
@ -39,120 +37,132 @@ 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; // 笔记的唯一标识符
private String mSnippet; // 笔记的预览文本
private static final int SNIPPET_PREW_MAX_LEN = 60; // 定义预览文本的最大长度
private MediaPlayer mPlayer; // 用于播放提醒音
// 活动创建时调用
@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()) {
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);
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(); // 获取启动此活动的意图
try {
// 从意图中提取笔记 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) {
e.printStackTrace();
e.printStackTrace(); // 打印异常堆栈信息
return;
}
mPlayer = new MediaPlayer();
mPlayer = new MediaPlayer(); // 创建媒体播放器实例
// 检查笔记是否存在于数据库中并为笔记类型
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog();
playAlarmSound();
showActionDialog(); // 显示操作对话框
playAlarmSound(); // 播放提醒音
} else {
finish();
finish(); // 如果笔记不存在,结束活动
}
}
// 检查屏幕是否亮起
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
// 播放报警声音
private void playAlarmSound() {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); // 获取默认闹钟铃声 URI
int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
int silentModeStreams = 0; // 定义静音模式下受影响的音频流类型
try {
// 尝试获取设置的静音模式下受影响的音频流类型
silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
// 根据静音模式设置,决定播放的音频流类型
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams);
} else {
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();
} 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();
mPlayer.setDataSource(this, url); // 设置播放源
mPlayer.prepare(); // 准备播放
mPlayer.setLooping(true); // 设置循环播放
mPlayer.start(); // 开始播放
} catch (Exception e) {
e.printStackTrace(); // 捕获并打印异常
}
}
// 显示操作对话框
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()) {
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);
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);
case DialogInterface.BUTTON_NEGATIVE: // 如果点击了“进入”按钮
Intent intent = new Intent(this, NoteEditActivity.class); // 创建进入编辑活动的意图
intent.setAction(Intent.ACTION_VIEW); // 设置意图动作
intent.putExtra(Intent.EXTRA_UID, mNoteId); // 传递笔记 ID
startActivity(intent); // 启动活动
break;
default:
break;
}
}
// 对话框消失时的处理
public void onDismiss(DialogInterface dialog) {
stopAlarmSound();
finish();
stopAlarmSound(); // 停止提醒音
finish(); // 结束活动
}
// 停止提醒音
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop();
mPlayer.release();
mPlayer = null;
if (mPlayer != null) { // 如果播放器已初始化
mPlayer.stop(); // 停止播放
mPlayer.release(); // 释放资源
mPlayer = null; // 清空播放器引用
}
}
}
}
Loading…
Cancel
Save