一、闹铃功能介绍:
1、为每条备忘添加闹铃
2、备忘内容和闹铃信息存在SQL中
3、可删除每天记录和闹铃或者删除所有
4、到点闹铃启动,包括锁屏时和重开机
5、在桌面的创建一个widget类似便签那样的东西显示记录
二、看下效果图:
三、关于SQL部分不再细写,可查看Android应用开发一栏:
添加闹铃和备忘:
1、设置日期:
dateBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCalendar = Calendar.getInstance();
// TODO Auto-generated method stub
new DatePickerDialog(EditTime.this,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,
int year, int monthOfYear,
int dayOfMonth) {
mAlarmCalendar.set(Calendar.YEAR, year);
mAlarmCalendar.set(Calendar.MONTH,
monthOfYear);
mAlarmCalendar.set(Calendar.DAY_OF_MONTH,
dayOfMonth);
EditTime.this.year = year;
EditTime.this.month = monthOfYear;
EditTime.this.day = dayOfMonth;
dateBtn.setText(year + "年" + (month + 1)
+ "月" + day + "日");
System.out.println(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
}
}, year, month, day).show();
}
});
timeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCalendar = Calendar.getInstance();
new TimePickerDialog(EditTime.this,
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view,
int hourOfDay, int minute) {
mAlarmCalendar.set(Calendar.HOUR_OF_DAY,
hourOfDay);
mAlarmCalendar.set(Calendar.MINUTE, minute);
mAlarmCalendar.set(Calendar.SECOND, 0);
mAlarmCalendar.set(Calendar.MILLISECOND, 0);
EditTime.this.hour = hourOfDay;
EditTime.this.minute = minute;
timeBtn.setText(hour + "时" + minute + "分");
System.out.println(hourOfDay+"时"+minute+"分");
}
}, hour, minute, false).show();
}
});
setBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//写进数据库
addData();
/* 获取闹钟管理的实例 */
mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
Intent intent = new Intent(EditTime.this,
AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
EditTime.this, AlarmIndex, intent, 0);// 第二个参数为区别不同闹铃的唯一标识
/* 设置闹钟 */
mAlarm.set(AlarmManager.RTC_WAKEUP,
mAlarmCalendar.getTimeInMillis(), pendingIntent);
finish();
}
});
4、广播接收BroadcastReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, Alarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这一句很重要,不然会出错
context.startActivity(i);
}
}
记得注册:
public class BootReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, BootAlarm.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这一句很重要,不然会出错
context.startActivity(i);
}
}
}
同样需要注册:
dbHelper = new DatabaseHelper(BootAlarm.this, "idea.sql");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", null, null, null, null, null, null);
btAlarmCalendar = Calendar.getInstance();
while (cursor.moveToNext()) {
btAlarmCalendar = Calendar.getInstance();
System.out.println("cursor.moveToNext()-->cursor.getInt(0)-->"
+ cursor.getInt(0));
time = cursor.getString(cursor.getColumnIndex("mtime"));
year = time.substring(0, time.indexOf("年"));
myear = Integer.valueOf(year).intValue();
month = time.substring(time.indexOf("年")+1, time.indexOf("月"));
mmonth = Integer.valueOf(month).intValue()-1;
day = time.substring(time.indexOf("月")+1, time.indexOf("日"));
mday = Integer.valueOf(day).intValue();
hour = time.substring(time.indexOf("日")+1, time.indexOf("时"));
mhour = Integer.valueOf(hour).intValue();
minute = time.substring(time.indexOf("时")+1, time.indexOf("分"));
mminute = Integer.valueOf(minute).intValue();
System.out.println(myear + "年" + mmonth + "月" + mday + "日"
+ mhour + "时" + mminute);
btAlarmCalendar.set(Calendar.YEAR, myear);
btAlarmCalendar.set(Calendar.MONTH, mmonth);
btAlarmCalendar.set(Calendar.DAY_OF_MONTH, mday);
btAlarmCalendar.set(Calendar.HOUR_OF_DAY, mhour);
btAlarmCalendar.set(Calendar.MINUTE, mminute);
btAlarmCalendar.set(Calendar.SECOND, 0);
btAlarmCalendar.set(Calendar.MILLISECOND, 0);
mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
intent = new Intent(BootAlarm.this, AlarmReceiver.class);
pendingIntent= PendingIntent.getBroadcast(
BootAlarm.this, cursor.getInt(0), intent, 0);// 第二个参数为区别不同闹铃的唯一标识
/* 设置闹钟 */
mAlarm.set(AlarmManager.RTC_WAKEUP,
btAlarmCalendar.getTimeInMillis(), pendingIntent);
}
intent = new Intent(BootAlarm.this, AlarmReceiver.class);
pendingIntent= PendingIntent.getBroadcast(
BootAlarm.this, cursor.getInt(0), intent, 0);
7、到点闹铃响,这是由AlarmReceiver(继承BroadcastReceiver)启动的:
dbHelper = new DatabaseHelper(Alarm.this, "ideal.sql");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", null, "_id=?",
new String[] { NoteActivity.Alarming_index + "" }, null, null,
null);
if (cursor.moveToFirst()) {
mtime = cursor.getString(cursor.getColumnIndex("mtime"));
mtext = cursor.getString(cursor.getColumnIndex("mtext"));
}
db.close();
addData();//写进历史记录表中
deleteData(NoteActivity.Alarming_index);//删除原来记录
new AlertDialog.Builder(Alarm.this)
.setIcon(R.drawable.bell)
.setTitle("It's time to do something.")
.setMessage(mtext)
.setPositiveButton("Done",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
deleteData(NoteActivity.Alarming_index);//再次删除确认
//关闭闹铃声
Intent intentSV = new Intent(Alarm.this, MyService.class);
stopService(intentSV);
Alarm.this.finish();
}
}).show();
//下面是为了在锁屏时也能启动闹铃
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
8、置于闹铃音乐可以写一个Service,这个可查看Android学习笔记一栏
10、由于是闹铃,所以必须每一秒中进行更新一下倒计时,这个可以查看Android学习笔记一栏。
11、附:让title每秒显示时间:
public class TimeThread extends Thread {
@Override
public void run() {
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
}
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
currentTime = new Date();
// long sysTime = System.currentTimeMillis();
CharSequence sysTimeStr = DateFormat.format(
"yyyy年MM月dd日hh:mm:ss", currentTime.getTime());
EditTime.this.setTitle(sysTimeStr);
break;
default:
break;
}
}
};
------------------(修改版)----------------------
12、是否为记录添加闹铃:
isAlarmBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isAlarmNum++;
if (isAlarmNum % 2 == 0) {
isAlarmBtn.setBackgroundResource(R.drawable.checknull);
isAlarm = 0;
} else {
isAlarmBtn.setBackgroundResource(R.drawable.check);
isAlarm = 1;
}
}
});
currentTime = new Date();
if (beginTime.getTime() < currentTime.getTime()) {
Toast.makeText(EditTime.this, "设置时间不能小于当前时间!",
Toast.LENGTH_SHORT).show();
}
AppWidget.java
public class AppWidget extends AppWidgetProvider {
private DatabaseHelper dbHelper;
String[] desk_text;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
//读取数据库中的记录
dbHelper = new DatabaseHelper(context, "ideal.sql");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", null, null, null, null, null, null);
int desk_num=0;
//桌面的便签只显示6条记录
desk_text = new String[6];
while(cursor.moveToNext()){
if(desk_num == 6){
break;
}
String temp_text = cursor.getString(cursor.getColumnIndex("mtext"));
//控制每条记录显示的长度
if(temp_text.length()>7){
temp_text=temp_text.substring(0, 7)+"...";
}
desk_text[desk_num]=temp_text;
System.out.println("desk_text[desk_num]"+desk_text[desk_num]);
desk_num++;
}
db.close();
final int Num = appWidgetIds.length;
for (int i = 0; i < Num; i++) {
int[] mAppWidgetId = appWidgetIds;
RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);
mRemoteViews.setTextViewText(R.id.desktop_text, array_to_string(desk_text));
Intent clickIntent = new Intent(context, NoteActivity.class);
PendingIntent pdIntent = PendingIntent.getActivity(context, 0,
clickIntent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.widget_layout, pdIntent);
appWidgetManager.updateAppWidget(mAppWidgetId, mRemoteViews);
}
}
//onReceive方法用来接收广播,以便更新桌面的便签
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if(intent.getAction().equals("com.ideal.note.widget")){
dbHelper = new DatabaseHelper(context, "ideal.sql");
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", null, null, null, null, null, null);
int desk_num=0;
desk_text = new String[6];
while(cursor.moveToNext()){
if(desk_num == 6){
break;
}
String temp_text = cursor.getString(cursor.getColumnIndex("mtext"));
if(temp_text.length()>7){
temp_text=temp_text.substring(0, 7)+"...";
}
desk_text[desk_num]=temp_text;
System.out.println("desk_text[desk_num]"+desk_text[desk_num]);
desk_num++;
}
db.close();
}
RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
mRemoteViews.setTextViewText(R.id.desktop_text, array_to_string(desk_text));
AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, AppWidget.class), mRemoteViews);
}
//数组转化为字符显示
public String array_to_string(String[] array){
String temp_str = "";
for(int i=0;i
Intent mWidgetIntent = new Intent();
mWidgetIntent.setAction("com.ideal.note.widget");
NoteActivity.this.sendBroadcast(mWidgetIntent);
附源码下载地址:http://download.csdn.net/detail/moruna/7969393
有疑问的可加入这个qq群聊一起讨论:431922622(奋斗的Android青年 )