Update AlarmAlertActivity.java

main
pfmsx5cbu 3 months ago
parent 96fae304d9
commit da5d2cf822

@ -39,21 +39,23 @@ 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;// 媒体播放器,用于播放提醒音
@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
@ -73,36 +75,44 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
e.printStackTrace();
return;
}
// 初始化媒体播放器
mPlayer = new MediaPlayer();
// 检查笔记是否仍然存在
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog();
playAlarmSound();
showActionDialog();// 显示提醒对话框
playAlarmSound();// 播放提醒音
} else {
finish();
finish();// 笔记不存在则直接结束
}
}
/**
*
* @return truefalse
*/
private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
/**
*
*/
private void playAlarmSound() {
// 获取系统默认的闹钟铃声
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
// 检查静音模式设置
int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0);
// 设置音频流类型
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.setLooping(true);// 循环播放
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
@ -118,14 +128,17 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
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);
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);
}
@ -133,6 +146,7 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
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);
@ -144,10 +158,13 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
}
public void onDismiss(DialogInterface dialog) {
// 对话框关闭时停止铃声并结束活动
stopAlarmSound();
finish();
}
/**
*
*/
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop();

Loading…
Cancel
Save