测试提交5

sue_branch
sue 3 months ago
parent 41a7585452
commit 54b783e1ae

@ -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();

Loading…
Cancel
Save