/* * Copyright (c) 2010-2011, The MiCode Open Source Community (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 * * 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; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnDismissListener; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.os.PowerManager; import android.provider.Settings; import android.support.v7.app.AppCompatActivity; import android.view.Window; import android.view.WindowManager; import net.micode.notes.R; import net.micode.notes.data.Notes; import net.micode.notes.tool.DataUtils; import java.io.IOException; //闹钟提醒 //OnDismissListener 接口是对话消失监听器 public class AlarmAlertActivity extends AppCompatActivity implements OnClickListener, OnDismissListener { private long mNoteId; private String mSnippet; private static final int SNIPPET_PREW_MAX_LEN = 60; MediaPlayer mPlayer; //OnCreate是Android中的一个特别的函数,用来"表示一个窗口正在生成"。其不产生窗口,只是在窗口显示前设置窗口的属性如风格、位置颜色等。 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 //息屏显示 final Window win = getWindow(); 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); } //获得Intent用于开启Activity Intent intent = getIntent(); //try-catch异常处理 try { //获取便签的id mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); //通过该id获取所对应便签的摘要 mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); //判断mSnippet长度,过长则将其削减到合适长度 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(); return; } //实例化mplayer,新建一个媒体播放器 mPlayer = new MediaPlayer(); //通过id从数据库中获取其类型,判断是否需要提醒 if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { showActionDialog(); playAlarmSound(); } else { 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); 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.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(); } } //界面设计 //新建对话框,设置标题,设置内容,设置ok按钮,设置enter按钮 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);//选择不确定界面 } //实现监听器OnClickListener具体功能实现 //dialog对话框,negative一般用于取消 //onClick方法,处理点击事件 public void onClick(DialogInterface dialog, int which) { switch (which) {//获取输入框中内容 //取消按钮 case DialogInterface.BUTTON_NEGATIVE: //新建Intent,指向NoteEditActivity;设置VIEW动作:显示用户数据;附加上便签的id;启动 Intent intent = new Intent(this, NoteEditActivity.class);////启动小组件或者是绑定关系,这里是绑定关系,参数分别为上下文要跳转的Activity类 intent.setAction(Intent.ACTION_VIEW);//自动以最合适的方式显示Data(执行动作类别) intent.putExtra(Intent.EXTRA_UID, mNoteId);//扩展信息,要传递的值赋值到intent startActivity(intent);//启动intent实现跳转 break; default: break; } } public void onDismiss(DialogInterface dialog) { stopAlarmSound();//停止报警声音 finish();//结束运行 } //停止闹钟声音 private void stopAlarmSound() { if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); mPlayer = null; } } }