代码标注

dev
宋经纬 3 years ago
parent b7a75059c2
commit 1ff4ae830a

@ -1,20 +1,20 @@
/*
* 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;
* 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;
@ -39,26 +39,33 @@ import net.micode.notes.tool.DataUtils;
import java.io.IOException;
//主要用于提醒用户处理备忘录、日程等事务,以便用户可以更好地管理自己的时间和事务
public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener {
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);
//Bundle类型的数据与Map类型的数据类似均以Key-Value的方式进行数据存储
//设置该活动的特性(无标题栏),并获取窗口对象
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 intent = getIntent();
@ -66,9 +73,13 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
try {
mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1));
mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId);
//根据ID从数据库中获取标签的内容
//getContentResolver()实现数据共享+实例存储
mSnippet = mSnippet.length() > SNIPPET_PREW_MAX_LEN ? mSnippet.substring(0,
SNIPPET_PREW_MAX_LEN) + getResources().getString(R.string.notelist_string_info)
: mSnippet;
//判断获取的标签片段是否达到符合长度
//如果代码异常则进行catch内的异常处理
} catch (IllegalArgumentException e) {
e.printStackTrace();
return;
@ -76,21 +87,27 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
mPlayer = new MediaPlayer();
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);
@ -102,11 +119,15 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
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();
@ -121,38 +142,52 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD
private void showActionDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
//新建dialog
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);
}
public void onClick(DialogInterface dialog, int which) {
switch (which) {
//用which选择click后下一步的操作
case DialogInterface.BUTTON_NEGATIVE:
//取消操作
Intent intent = new Intent(this, NoteEditActivity.class);
//实现Intent类和NoteEditActivity类之间的数据传输
intent.setAction(Intent.ACTION_VIEW);
//设置动作属性
intent.putExtra(Intent.EXTRA_UID, mNoteId);
startActivity(intent);
//开始动作
break;
default:
//确定操作
break;
}
}
public void onDismiss(DialogInterface dialog) {
//忽略
stopAlarmSound();
//停止闹钟声音
finish();
}
private void stopAlarmSound() {
if (mPlayer != null) {
mPlayer.stop();
//停止播放闹钟铃声
mPlayer.release();
//释放MediaPlayer对象
mPlayer = null;
}
}
}
}

@ -31,22 +31,23 @@ import net.micode.notes.data.Notes.NoteColumns;
public class AlarmInitReceiver extends BroadcastReceiver {
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
//从数据库中调用标签ID和闹钟日期
private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1;
@Override
public void onReceive(Context context, Intent intent) {
long currentDate = System.currentTimeMillis();
long currentDate = System.currentTimeMillis(); //产生一个当前时间的毫秒
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) },
null);
//将long变量currentDate转化为字符串
//Cursor通过查找数据库中的标签内容从而找到和当前系统时间相等的标签
if (c != null) {
if (c.moveToFirst()) {
do {
@ -61,5 +62,8 @@ public class AlarmInitReceiver extends BroadcastReceiver {
}
c.close();
}
//闹钟的启动需要以下步骤
//新建Intent、PendingIntent以及AlarmManager等
//这里就是根据数据库中的闹钟时间创建一个闹钟
}
}
}

@ -16,15 +16,54 @@
package net.micode.notes.ui;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
public class AlarmInitReceiver extends BroadcastReceiver {
private static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE
};
//从数据库中调用标签ID和闹钟日期
private static final int COLUMN_ID = 0;
private static final int COLUMN_ALERTED_DATE = 1;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, AlarmAlertActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
long currentDate = System.currentTimeMillis(); //产生一个当前时间的毫秒
Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI,
PROJECTION,
NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,
new String[] { String.valueOf(currentDate) },
null);
//将long变量currentDate转化为字符串
//Cursor通过查找数据库中的标签内容从而找到和当前系统时间相等的标签
if (c != null) {
if (c.moveToFirst()) {
do {
long alertDate = c.getLong(COLUMN_ALERTED_DATE);
Intent sender = new Intent(context, AlarmReceiver.class);
sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID)));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);
AlarmManager alermManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);
} while (c.moveToNext());
}
c.close();
}
//闹钟的启动需要以下步骤
//新建Intent、PendingIntent以及AlarmManager等
//这里就是根据数据库中的闹钟时间创建一个闹钟
}
}
}
Loading…
Cancel
Save