当前位置:   article > 正文

Android项目小结---闹铃备忘录小开发知识点(附有:源码下载)_安卓移动应用开发闹铃备忘录

安卓移动应用开发闹铃备忘录

一、闹铃功能介绍:

1、为每条备忘添加闹铃

2、备忘内容和闹铃信息存在SQL中

3、可删除每天记录和闹铃或者删除所有

4、到点闹铃启动,包括锁屏时和重开机

5、在桌面的创建一个widget类似便签那样的东西显示记录

二、看下效果图:



三、关于SQL部分不再细写,可查看Android应用开发一栏:

添加闹铃和备忘:

1、设置日期:

  1. dateBtn.setOnClickListener(new OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. mCalendar = Calendar.getInstance();
  5. // TODO Auto-generated method stub
  6. new DatePickerDialog(EditTime.this,
  7. new DatePickerDialog.OnDateSetListener() {
  8. public void onDateSet(DatePicker view,
  9. int year, int monthOfYear,
  10. int dayOfMonth) {
  11. mAlarmCalendar.set(Calendar.YEAR, year);
  12. mAlarmCalendar.set(Calendar.MONTH,
  13. monthOfYear);
  14. mAlarmCalendar.set(Calendar.DAY_OF_MONTH,
  15. dayOfMonth);
  16. EditTime.this.year = year;
  17. EditTime.this.month = monthOfYear;
  18. EditTime.this.day = dayOfMonth;
  19. dateBtn.setText(year + "年" + (month + 1)
  20. + "月" + day + "日");
  21. System.out.println(year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
  22. }
  23. }, year, month, day).show();
  24. }
  25. });

2、设置时间:

  1. timeBtn.setOnClickListener(new OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. mCalendar = Calendar.getInstance();
  5. new TimePickerDialog(EditTime.this,
  6. new TimePickerDialog.OnTimeSetListener() {
  7. public void onTimeSet(TimePicker view,
  8. int hourOfDay, int minute) {
  9. mAlarmCalendar.set(Calendar.HOUR_OF_DAY,
  10. hourOfDay);
  11. mAlarmCalendar.set(Calendar.MINUTE, minute);
  12. mAlarmCalendar.set(Calendar.SECOND, 0);
  13. mAlarmCalendar.set(Calendar.MILLISECOND, 0);
  14. EditTime.this.hour = hourOfDay;
  15. EditTime.this.minute = minute;
  16. timeBtn.setText(hour + "时" + minute + "分");
  17. System.out.println(hourOfDay+"时"+minute+"分");
  18. }
  19. }, hour, minute, false).show();
  20. }
  21. });

3、广播闹铃:

  1. setBtn.setOnClickListener(new OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // TODO Auto-generated method stub
  5. //写进数据库
  6. addData();
  7. /* 获取闹钟管理的实例 */
  8. mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
  9. Intent intent = new Intent(EditTime.this,
  10. AlarmReceiver.class);
  11. PendingIntent pendingIntent = PendingIntent.getBroadcast(
  12. EditTime.this, AlarmIndex, intent, 0);// 第二个参数为区别不同闹铃的唯一标识
  13. /* 设置闹钟 */
  14. mAlarm.set(AlarmManager.RTC_WAKEUP,
  15. mAlarmCalendar.getTimeInMillis(), pendingIntent);
  16. finish();
  17. }
  18. });

4、广播接收BroadcastReceiver:

  1. public class AlarmReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. // TODO Auto-generated method stub
  5. Intent i = new Intent(context, Alarm.class);
  6. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这一句很重要,不然会出错
  7. context.startActivity(i);
  8. }
  9. }

记得注册:

  1. <receiver
  2. android:name=".AlarmReceiver"
  3. android:process=":remote" />

5、重启之后重写闹铃,BootReceiver:

  1. public class BootReceiver extends BroadcastReceiver{
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. // TODO Auto-generated method stub
  5. String action = intent.getAction();
  6. if(action.equals(Intent.ACTION_BOOT_COMPLETED)){
  7. Intent i = new Intent(context, BootAlarm.class);
  8. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这一句很重要,不然会出错
  9. context.startActivity(i);
  10. }
  11. }
  12. }
同样需要注册:

  1. <receiver android:name="BootReceiver" >
  2. <intent-filter>
  3. <action android:name="android.intent.action.BOOT_COMPLETED" />
  4. </intent-filter>
  5. </receiver>

6、开始重写:

  1. dbHelper = new DatabaseHelper(BootAlarm.this, "idea.sql");
  2. SQLiteDatabase db = dbHelper.getReadableDatabase();
  3. Cursor cursor = db.query("user", null, null, null, null, null, null);
  4. btAlarmCalendar = Calendar.getInstance();
  5. while (cursor.moveToNext()) {
  6. btAlarmCalendar = Calendar.getInstance();
  7. System.out.println("cursor.moveToNext()-->cursor.getInt(0)-->"
  8. + cursor.getInt(0));
  9. time = cursor.getString(cursor.getColumnIndex("mtime"));
  10. year = time.substring(0, time.indexOf("年"));
  11. myear = Integer.valueOf(year).intValue();
  12. month = time.substring(time.indexOf("年")+1, time.indexOf("月"));
  13. mmonth = Integer.valueOf(month).intValue()-1;
  14. day = time.substring(time.indexOf("月")+1, time.indexOf("日"));
  15. mday = Integer.valueOf(day).intValue();
  16. hour = time.substring(time.indexOf("日")+1, time.indexOf("时"));
  17. mhour = Integer.valueOf(hour).intValue();
  18. minute = time.substring(time.indexOf("时")+1, time.indexOf("分"));
  19. mminute = Integer.valueOf(minute).intValue();
  20. System.out.println(myear + "年" + mmonth + "月" + mday + "日"
  21. + mhour + "时" + mminute);
  22. btAlarmCalendar.set(Calendar.YEAR, myear);
  23. btAlarmCalendar.set(Calendar.MONTH, mmonth);
  24. btAlarmCalendar.set(Calendar.DAY_OF_MONTH, mday);
  25. btAlarmCalendar.set(Calendar.HOUR_OF_DAY, mhour);
  26. btAlarmCalendar.set(Calendar.MINUTE, mminute);
  27. btAlarmCalendar.set(Calendar.SECOND, 0);
  28. btAlarmCalendar.set(Calendar.MILLISECOND, 0);
  29. mAlarm = (AlarmManager) getSystemService(Service.ALARM_SERVICE);
  30. intent = new Intent(BootAlarm.this, AlarmReceiver.class);
  31. pendingIntent= PendingIntent.getBroadcast(
  32. BootAlarm.this, cursor.getInt(0), intent, 0);// 第二个参数为区别不同闹铃的唯一标识
  33. /* 设置闹钟 */
  34. mAlarm.set(AlarmManager.RTC_WAKEUP,
  35. btAlarmCalendar.getTimeInMillis(), pendingIntent);
  36. }

这里有个很需要注意的:

  1. intent = new Intent(BootAlarm.this, AlarmReceiver.class);
  2. pendingIntent= PendingIntent.getBroadcast(
  3. BootAlarm.this, cursor.getInt(0), intent, 0);

若是PendingIntent.getBroadcas,则intent必须为启动的是BroadcastReceiver,不然重写不会成功的。


7、到点闹铃响,这是由AlarmReceiver(继承BroadcastReceiver)启动的:

  1. dbHelper = new DatabaseHelper(Alarm.this, "ideal.sql");
  2. SQLiteDatabase db = dbHelper.getReadableDatabase();
  3. Cursor cursor = db.query("user", null, "_id=?",
  4. new String[] { NoteActivity.Alarming_index + "" }, null, null,
  5. null);
  6. if (cursor.moveToFirst()) {
  7. mtime = cursor.getString(cursor.getColumnIndex("mtime"));
  8. mtext = cursor.getString(cursor.getColumnIndex("mtext"));
  9. }
  10. db.close();
  11. addData();//写进历史记录表中
  12. deleteData(NoteActivity.Alarming_index);//删除原来记录
  13. new AlertDialog.Builder(Alarm.this)
  14. .setIcon(R.drawable.bell)
  15. .setTitle("It's time to do something.")
  16. .setMessage(mtext)
  17. .setPositiveButton("Done",
  18. new DialogInterface.OnClickListener() {
  19. public void onClick(DialogInterface dialog,
  20. int whichButton) {
  21. deleteData(NoteActivity.Alarming_index);//再次删除确认
  22. //关闭闹铃声
  23. Intent intentSV = new Intent(Alarm.this, MyService.class);
  24. stopService(intentSV);
  25. Alarm.this.finish();
  26. }
  27. }).show();

锁屏时也启动闹铃:

  1. //下面是为了在锁屏时也能启动闹铃
  2. getWindow().addFlags(
  3. WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
  4. | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
  5. getWindow().addFlags(
  6. WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
  7. | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
8、置于闹铃音乐可以写一个Service,这个可查看Android学习笔记一栏
9、点击listview可查看记录内容,长按可进行记录的删除,这里不再多写。

10、由于是闹铃,所以必须每一秒中进行更新一下倒计时,这个可以查看Android学习笔记一栏。

11、附:让title每秒显示时间:

  1. public class TimeThread extends Thread {
  2. @Override
  3. public void run() {
  4. do {
  5. try {
  6. Thread.sleep(1000);
  7. Message msg = new Message();
  8. msg.what = 1;
  9. mHandler.sendMessage(msg);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. } while (true);
  14. }
  15. }
  16. private Handler mHandler = new Handler() {
  17. @Override
  18. public void handleMessage(Message msg) {
  19. super.handleMessage(msg);
  20. switch (msg.what) {
  21. case 1:
  22. currentTime = new Date();
  23. // long sysTime = System.currentTimeMillis();
  24. CharSequence sysTimeStr = DateFormat.format(
  25. "yyyy年MM月dd日hh:mm:ss", currentTime.getTime());
  26. EditTime.this.setTitle(sysTimeStr);
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. };

------------------(修改版)----------------------


12、是否为记录添加闹铃:

  1. isAlarmBtn.setOnClickListener(new OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // TODO Auto-generated method stub
  5. isAlarmNum++;
  6. if (isAlarmNum % 2 == 0) {
  7. isAlarmBtn.setBackgroundResource(R.drawable.checknull);
  8. isAlarm = 0;
  9. } else {
  10. isAlarmBtn.setBackgroundResource(R.drawable.check);
  11. isAlarm = 1;
  12. }
  13. }
  14. });

13、阻止设置时间小于当前时间:

  1. currentTime = new Date();
  2. if (beginTime.getTime() < currentTime.getTime()) {
  3. Toast.makeText(EditTime.this, "设置时间不能小于当前时间!",
  4. Toast.LENGTH_SHORT).show();
  5. }

14、添加widget:

AppWidget.java

  1. public class AppWidget extends AppWidgetProvider {
  2. private DatabaseHelper dbHelper;
  3. String[] desk_text;
  4. @Override
  5. public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  6. int[] appWidgetIds) {
  7. // TODO Auto-generated method stub
  8. super.onUpdate(context, appWidgetManager, appWidgetIds);
  9. //读取数据库中的记录
  10. dbHelper = new DatabaseHelper(context, "ideal.sql");
  11. SQLiteDatabase db = dbHelper.getReadableDatabase();
  12. Cursor cursor = db.query("user", null, null, null, null, null, null);
  13. int desk_num=0;
  14. //桌面的便签只显示6条记录
  15. desk_text = new String[6];
  16. while(cursor.moveToNext()){
  17. if(desk_num == 6){
  18. break;
  19. }
  20. String temp_text = cursor.getString(cursor.getColumnIndex("mtext"));
  21. //控制每条记录显示的长度
  22. if(temp_text.length()>7){
  23. temp_text=temp_text.substring(0, 7)+"...";
  24. }
  25. desk_text[desk_num]=temp_text;
  26. System.out.println("desk_text[desk_num]"+desk_text[desk_num]);
  27. desk_num++;
  28. }
  29. db.close();
  30. final int Num = appWidgetIds.length;
  31. for (int i = 0; i < Num; i++) {
  32. int[] mAppWidgetId = appWidgetIds;
  33. RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(),
  34. R.layout.widget);
  35. mRemoteViews.setTextViewText(R.id.desktop_text, array_to_string(desk_text));
  36. Intent clickIntent = new Intent(context, NoteActivity.class);
  37. PendingIntent pdIntent = PendingIntent.getActivity(context, 0,
  38. clickIntent, 0);
  39. mRemoteViews.setOnClickPendingIntent(R.id.widget_layout, pdIntent);
  40. appWidgetManager.updateAppWidget(mAppWidgetId, mRemoteViews);
  41. }
  42. }
  43. //onReceive方法用来接收广播,以便更新桌面的便签
  44. @Override
  45. public void onReceive(Context context, Intent intent) {
  46. // TODO Auto-generated method stub
  47. super.onReceive(context, intent);
  48. if(intent.getAction().equals("com.ideal.note.widget")){
  49. dbHelper = new DatabaseHelper(context, "ideal.sql");
  50. SQLiteDatabase db = dbHelper.getReadableDatabase();
  51. Cursor cursor = db.query("user", null, null, null, null, null, null);
  52. int desk_num=0;
  53. desk_text = new String[6];
  54. while(cursor.moveToNext()){
  55. if(desk_num == 6){
  56. break;
  57. }
  58. String temp_text = cursor.getString(cursor.getColumnIndex("mtext"));
  59. if(temp_text.length()>7){
  60. temp_text=temp_text.substring(0, 7)+"...";
  61. }
  62. desk_text[desk_num]=temp_text;
  63. System.out.println("desk_text[desk_num]"+desk_text[desk_num]);
  64. desk_num++;
  65. }
  66. db.close();
  67. }
  68. RemoteViews mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
  69. mRemoteViews.setTextViewText(R.id.desktop_text, array_to_string(desk_text));
  70. AppWidgetManager.getInstance(context).updateAppWidget(new ComponentName(context, AppWidget.class), mRemoteViews);
  71. }
  72. //数组转化为字符显示
  73. public String array_to_string(String[] array){
  74. String temp_str = "";
  75. for(int i=0;i<array.length;i++){
  76. if(array[i]==null){
  77. break;
  78. }else {
  79. temp_str = temp_str+"\n* "+array[i];
  80. }
  81. }
  82. return temp_str;
  83. }
  84. }

在其他修改了数据库数据的Activity或者其他地方发送一条广播来让AppWidget执行,如在NoteActivity.java中添加:

  1. Intent mWidgetIntent = new Intent();
  2. mWidgetIntent.setAction("com.ideal.note.widget");
  3. NoteActivity.this.sendBroadcast(mWidgetIntent);

另,还需在AndroidManifest.xml中添加:

  1. <receiver android:name=".AppWidget">
  2. <intent-filter>
  3. <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
  4. <action android:name="com.ideal.note.widget"/>
  5. </intent-filter>
  6. <meta-data android:name="android.appwidget.provider"
  7. android:resource="@xml/appwidget_info"/>
  8. </receiver>

附源码下载地址:http://download.csdn.net/detail/moruna/7969393

有疑问的可加入这个qq群聊一起讨论:431922622(奋斗的Android青年 )


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/675799
推荐阅读
相关标签
  

闽ICP备14008679号