补上王润泽上次误传的代码

pull/9/head
夏彦博 2 years ago
parent a7733f05de
commit 6b3a15d3b9

@ -39,35 +39,50 @@ import net.micode.notes.tool.DataUtils;
import java.io.IOException; import java.io.IOException;
/**
* @classname: AlarmAlertActivity
* @description:
* @date: 2023/12/28 11:18
* @author: wangrunze
*/
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
private long mNoteId; private long mNoteId;//文本在数据库存储中的ID号
private String mSnippet; private String mSnippet;//闹钟提示时出现的文本片段
private static final int SNIPPET_PREW_MAX_LEN = 60; private static final int SNIPPET_PREW_MAX_LEN = 60;
MediaPlayer mPlayer; MediaPlayer mPlayer;
//Bundle类型的数据与Map类型的数据相似都是以key-value的形式存储数据的
//onsaveInstanceState方法是用来保存Activity的状态的,能从onCreate的参数savedInsanceState中获得状态数据
/**
* @classname: AlarmAlertActivity
* @methodname onCreate
* @description:
* @date: 2023/12/28 11:27
* @author: wangrunze
* @param: Bundle savedInstanceState
*/
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); requestWindowFeature(Window.FEATURE_NO_TITLE);//界面显示——无标题
final Window win = getWindow(); 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 win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON//保持窗体点亮
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON//将窗体点亮
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON//允许窗体点亮时锁屏
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);//在手机锁屏后如果到了闹钟提示时间,点亮屏幕
} }
Intent intent = getIntent(); Intent intent = getIntent();
try { try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);//根据ID从数据库中获取标签的内容
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0, mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info) SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)//判断标签片段是否达到符合长度
: mSnippet; : mSnippet;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
@ -76,23 +91,39 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
mPlayer = new MediaPlayer(); mPlayer = new MediaPlayer();
if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) {
showActionDialog(); showActionDialog();//弹出对话框
playAlarmSound(); playAlarmSound();//闹钟提示音激发
} else { } else {
finish(); finish();
} }
} }
/**
* @classname: AlarmAlertActivity
* @methodname isScreenOn
* @description:
* @date: 2023/12/28 11:28
* @author: wangrunze
* @return: pm.isScreenOn()
*/
private boolean isScreenOn() { private boolean isScreenOn() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn(); return pm.isScreenOn();
} }
/**
* @classname: AlarmAlertActivity
* @methodname playAlarmSound
* @description:
* @date: 2023/12/28 11:29
* @author: wangrunze
*/
private void playAlarmSound() { private void playAlarmSound() {
Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM);
int silentModeStreams = Settings.System.getInt(getContentResolver(), int silentModeStreams = Settings.System.getInt(getContentResolver(),
Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); //调用系统的铃声管理URI得到闹钟提示音
if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) {
mPlayer.setAudioStreamType(silentModeStreams); mPlayer.setAudioStreamType(silentModeStreams);
@ -100,13 +131,13 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
} }
try { try {
mPlayer.setDataSource(this, url); mPlayer.setDataSource(this, url);//无返回值,设置多媒体数据来源
mPlayer.prepare(); mPlayer.prepare();//准备同步
mPlayer.setLooping(true); mPlayer.setLooping(true);//设置是否循环播放
mPlayer.start(); mPlayer.start();//开始播放
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();//抛出异常, 还将显示出更深的调用信息
} catch (SecurityException e) { } catch (SecurityException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
@ -119,22 +150,31 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
} }
} }
/**
* @classname: AlarmAlertActivity
* @methodname showActionDialog
* @description:
* @date: 2023/12/28 11:30
* @author: wangrunze
*/
private void showActionDialog() { private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this); AlertDialog.Builder dialog = new AlertDialog.Builder(this);//用到AlertDialog.Builder中的create()新建了一个AlertDialog
dialog.setTitle(R.string.app_name); dialog.setTitle(R.string.app_name);//为对话框设置标题
dialog.setMessage(mSnippet); dialog.setMessage(mSnippet);//为对话框设置内容
dialog.setPositiveButton(R.string.notealert_ok, this); dialog.setPositiveButton(R.string.notealert_ok, this);//给对话框添加"Yes"按钮
if (isScreenOn()) { if (isScreenOn()) {
dialog.setNegativeButton(R.string.notealert_enter, this); dialog.setNegativeButton(R.string.notealert_enter, this);//给对话框添加"no"按钮
} }
dialog.show().setOnDismissListener(this); dialog.show().setOnDismissListener(this);
} }
//DialogInterface dialog为对话框which为选择按钮,功能为选择各种操作
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_NEGATIVE: case DialogInterface.BUTTON_NEGATIVE://这是取消操作
Intent intent = new Intent(this, NoteEditActivity.class); Intent intent = new Intent(this, NoteEditActivity.class);//实现两个类间的数据传输
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);//设置动作属性
intent.putExtra(Intent.EXTRA_UID, mNoteId); intent.putExtra(Intent.EXTRA_UID, mNoteId);
startActivity(intent); startActivity(intent);
break; break;
@ -143,11 +183,25 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
} }
} }
/**
* @classname: AlarmAlertActivity
* @methodname onDismiss
* @description:
* @date: 2023/12/28 11:30
* @author: wangrunze
* @param:DialogInterface dialog
*/
public void onDismiss(DialogInterface dialog) { public void onDismiss(DialogInterface dialog) {
stopAlarmSound(); stopAlarmSound();
finish(); finish();
} }
/**
* @classname: AlarmAlertActivity
* @methodname: stopAlarmSound
* @description:
* @date: 2023/12/28 11:31
* @author: wangrunze
*/
private void stopAlarmSound() { private void stopAlarmSound() {
if (mPlayer != null) { if (mPlayer != null) {
mPlayer.stop(); mPlayer.stop();
@ -156,3 +210,4 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
} }
} }
} }
Loading…
Cancel
Save