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

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

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