demo闹钟

 

        最近做闹钟,所以自己写了个Demo版本,这个程序是用listview单独的类来实现的,和activity类分开来实现的!这个是用数据库进行更新的,当闹钟设置后,闹钟图片变成闹钟的样子,闹钟取消后,图片变成灰色的闹钟,这个是用ListView来实现数据库更新数据的!然后弹对话框来实现时间的设置和周几重复的功能,这个功能能实现,我测试了!正确无误!需要注意的地方有两个:listview每次滑动的时候,记得设置背景为透明, mAlarmListView.setCacheColorHint(0);另一个是利用Intent传递广播的Intent的时候,传参数的时候,记得设置PendingIntent  sender=PendingIntent.getBroadcast(context,0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);的PendingIntent.FLAG_UPDATE_CURRENT,为了让每次启动PendingIntent进行更新!这两点要注意一下,我在编写的时候就是遇到这两个问题了!另外就是算法的判断!感觉好可以赞一个,支持我的原创!

有问题的可以留言,想要源码的可以留言,或者在我的csdn资源上下载:

http://download.csdn.net/source/3572215

转载请标明出处:

http://blog.csdn.net/wdaming1986/article/details/6745655

              

               程序进入的开始界面:                                              点击每个闹钟后弹出的dialog界面:

                                                            

 

                                   点击设置时间后弹出的dialog界面:                          点击设置重复后弹出的界面:

                                                            

 

                                  闹钟时间到了,会弹出提醒Dialog:               点击是否开启闹钟按钮,不选中确定的界面:

                                                           

 

   下面看代码:

一、MainActivity。java类,程序入口类:

  1. package com.cn.daming;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.LinearLayout;  
  6.   
  7. public class MainActivity extends Activity {  
  8.   
  9.     private LinearLayout mLinearLayout;  
  10.       
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         this.setContentView(R.layout.main);  
  15.         mLinearLayout = (LinearLayout)findViewById(R.id.box);  
  16.         new AlarmClockView(this,this,mLinearLayout);  
  17.     }  
  18. }  


 

二、AlarmClockView。java类,listview的类:

  1. package com.cn.daming;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Calendar;  
  5. import java.util.List;  
  6.   
  7. import android.app.AlarmManager;  
  8. import android.app.AlertDialog;  
  9. import android.app.PendingIntent;  
  10. import android.app.TimePickerDialog;  
  11. import android.content.Context;  
  12. import android.content.DialogInterface;  
  13. import android.content.Intent;  
  14. import android.database.Cursor;  
  15. import android.util.Log;  
  16. import android.view.LayoutInflater;  
  17. import android.view.View;  
  18. import android.widget.AdapterView;  
  19. import android.widget.Button;  
  20. import android.widget.CheckBox;  
  21. import android.widget.CompoundButton;  
  22. import android.widget.CompoundButton.OnCheckedChangeListener;  
  23. import android.widget.LinearLayout;  
  24. import android.widget.ListView;  
  25. import android.widget.TextView;  
  26. import android.widget.TimePicker;  
  27. import android.widget.Toast;  
  28.   
  29. public class AlarmClockView extends View{  
  30.       
  31.     private MainActivity activity;  
  32.     private LinearLayout mLinearLayout;  
  33.     private Context context;  
  34.   
  35.     private DataBaseHelper dbHelper;  
  36.     private Cursor cursor;  
  37.     private ListView mAlarmListView;  
  38.       
  39.     private List<String> ids;  
  40.     private List<String> times;  
  41.     private List<String> repeats;  
  42.     private List<String> isopens;  
  43.     private List<String> kinds;  
  44.       
  45.     private AlarmClockAdapter alarmAdapter;  
  46.       
  47.     Calendar c=Calendar.getInstance();  
  48.     TextView time1TextView = null;  
  49.     TextView time2TextView = null;  
  50.     Button setTimeButton1 = null;  
  51.     Button setTimeButton2 = null;  
  52.     TextView repeat1TextView = null;  
  53.     TextView repeat2TextView = null;  
  54.     Button repeatButton1 = null;  
  55.     Button repeatButton2 = null;  
  56.     CheckBox time1CheckBox = null;  
  57.     CheckBox time2CheckBox = null;  
  58.       
  59.     String tmpS1 = "目前无设置";  
  60.     String repeatString1 = "目前无设置";  
  61.     String tmpS2 = "目前无设置";  
  62.     String repeatString2 = "目前无设置";  
  63.     boolean isOpenInt1 = false;  
  64.     boolean isOpenInt2 = false;  
  65.     String isOpentime1 = null;  
  66.     String isOpentime2 = null;  
  67.       
  68.     boolean isOpenAlarm = false;  
  69.     CheckBox isOpenCheckBox;  
  70.       
  71.     int[] repeatArray1 = {0,0,0,0,0,0,0};  
  72.     int[] repeatArray2 = {0,0,0,0,0,0,0};  
  73.       
  74.     boolean repeat_1_1 = false;  
  75.     boolean repeat_1_2 = false;  
  76.     boolean repeat_1_3 = false;  
  77.     boolean repeat_1_4 = false;  
  78.     boolean repeat_1_5 = false;  
  79.     boolean repeat_1_6 = false;  
  80.     boolean repeat_1_7 = false;  
  81.       
  82.     boolean repeat_2_1 = false;  
  83.     boolean repeat_2_2 = false;  
  84.     boolean repeat_2_3 = false;  
  85.     boolean repeat_2_4 = false;  
  86.     boolean repeat_2_5 = false;  
  87.     boolean repeat_2_6 = false;  
  88.     boolean repeat_2_7 = false;  
  89.       
  90.       
  91.     public AlarmClockView(final Context context,MainActivity activity, LinearLayout linearLayout) {  
  92.         super(context);  
  93.             this.context = context;  
  94.             this.activity = activity;  
  95.             this.mLinearLayout = linearLayout;  
  96.             View alarmView = View.inflate(context, R.layout.alarm_listview, null);  
  97.             mLinearLayout.addView(alarmView);    
  98.             mAlarmListView = (ListView)alarmView.findViewById(R.id.alarm_list);  
  99.             mAlarmListView.setCacheColorHint(0);  
  100.             dbHelper = new DataBaseHelper(context);  
  101.             refreshDBHelper();  
  102.               
  103.             mAlarmListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){  
  104.                 public void onItemClick(AdapterView<?> arg0, View v, int position,  
  105.                         long arg3) {  
  106.                       
  107.                     showAlarmDialog(position);  
  108.                 }  
  109.             });  
  110.     }  
  111.       
  112.     public void refreshDBHelper()  
  113.     {  
  114.         cursor = dbHelper.selectAlarmColock();  
  115.         ids = new ArrayList<String>();  
  116.         times = new ArrayList<String>();  
  117.         repeats = new ArrayList<String>();  
  118.         isopens = new ArrayList<String>();  
  119.         kinds = new ArrayList<String>();  
  120.           
  121.         int count = cursor.getCount();  
  122.         if(count==0){  
  123.             String[] tempStr = new String[4];  
  124.             for(int i=1;i<=2;i++){  
  125.                 dbHelper.insertAlarmColock(tempStr);  
  126.             }  
  127.             int count2 = cursor.getCount();  
  128.             if(count2>0){  
  129.                 for(int i=0;i<count2;i++){  
  130.                     cursor.moveToPosition(i);  
  131.                     ids.add(cursor.getString(0));  
  132.                     times.add(cursor.getString(1));  
  133.                     repeats.add(cursor.getString(2));  
  134.                     isopens.add(cursor.getString(3));  
  135.                     kinds.add(cursor.getString(4));  
  136.                 }  
  137.             }  
  138.             cursor.close();  
  139.             dbHelper.close();  
  140.             refreshDBHelper();  
  141.             Toast.makeText(context, R.string.not_dbcursor_values, Toast.LENGTH_SHORT).show();  
  142.         }  
  143.         else if(count>0){  
  144.             for(int i=0;i<count;i++){  
  145.                 cursor.moveToPosition(i);  
  146.                 ids.add(cursor.getString(0));  
  147.                 times.add(cursor.getString(1));  
  148.                 repeats.add(cursor.getString(2));  
  149.                 isopens.add(cursor.getString(3));  
  150.                 kinds.add(cursor.getString(4));  
  151.             }  
  152.         }  
  153.         alarmAdapter = new AlarmClockAdapter(context,ids,times,repeats,isopens,kinds);    
  154.         mAlarmListView.setAdapter(alarmAdapter);  
  155.         cursor.close();  
  156.         dbHelper.close();  
  157.     }  
  158.   
  159.     public void showAlarmDialog(int position){  
  160.           
  161.         dbHelper = new DataBaseHelper(context);  
  162.         cursor = dbHelper.selectAlarmColock();  
  163.         int count = cursor.getCount();  
  164.           
  165.         if(position == 0){            
  166.             LayoutInflater factory = LayoutInflater.from(context);  
  167.             final View alarm1View = factory.inflate(R.layout.time_repeat_dialog, null);  
  168.             time1TextView = (TextView)alarm1View.findViewById(R.id.text);  
  169.             setTimeButton1 = (Button)alarm1View.findViewById(R.id.mButton);  
  170.             repeat1TextView = (TextView)alarm1View.findViewById(R.id.repeattext);  
  171.             repeatButton1 = (Button)alarm1View.findViewById(R.id.repeatButton);  
  172.             time1CheckBox = (CheckBox)alarm1View.findViewById(R.id.isopen_check);  
  173.             String isOpen1 = "关";  
  174.               
  175.             if(count==2){  
  176.                 for(int i=0;i<count;i++){  
  177.                   if(i == 0){  
  178.                       cursor.moveToPosition(i);  
  179.                       if(cursor.getString(1)==null){  
  180.                           time1TextView.setText(tmpS1);  
  181.                       }else{  
  182.                           time1TextView.setText(cursor.getString(1));  
  183.                           tmpS1 = cursor.getString(1);  
  184.                       }  
  185.                       if((cursor.getString(2))==null){  
  186.                           repeat1TextView.setText(repeatString1);  
  187.                       }else{  
  188.                           repeat1TextView.setText(cursor.getString(2));  
  189.                           repeatString1 = cursor.getString(2);  
  190.                       }  
  191.                       isOpen1 = cursor.getString(3);  
  192.                       if(isOpen1!=null){  
  193.                           if(isOpen1.equals("开")){  
  194.                               time1CheckBox.setChecked(true);  
  195.                               isOpenInt1 = true;  
  196.                           }else{  
  197.                               time1CheckBox.setChecked(false);  
  198.                               isOpenInt1 = false;  
  199.                           }  
  200.                       }  
  201.                   }  
  202.                 }  
  203.             }  
  204.               
  205.             time1CheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){  
  206.                 public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  207.                     if(arg0.isChecked()){  
  208.                         isOpenInt1 = true;  
  209.                     }else{  
  210.                         isOpenInt1 = false;  
  211.                     }  
  212.                 }  
  213.             });  
  214.               
  215.             setTimeButton1.setOnClickListener(new OnClickListener(){  
  216.                 public void onClick(View arg0) {  
  217.                       c.setTimeInMillis(System.currentTimeMillis());  
  218.                       int mHour=c.get(Calendar.HOUR_OF_DAY);  
  219.                       int mMinute=c.get(Calendar.MINUTE);  
  220.                       int mDay=c.get(Calendar.DAY_OF_WEEK);  
  221.                         
  222.                       new TimePickerDialog(context,  
  223.                         new TimePickerDialog.OnTimeSetListener()  
  224.                         {                  
  225.                           public void onTimeSet(TimePicker view,int hourOfDay,  
  226.                                                 int minute)  
  227.                           {  
  228.                             c.setTimeInMillis(System.currentTimeMillis());  
  229.                             c.set(Calendar.HOUR_OF_DAY,hourOfDay);  
  230.                             c.set(Calendar.MINUTE,minute);  
  231.                             c.set(Calendar.SECOND,0);  
  232.                             c.set(Calendar.MILLISECOND,0);  
  233.                               
  234.                             tmpS1=format(hourOfDay)+":"+format(minute);  
  235.                             time1TextView.setText(tmpS1);  
  236.                           
  237.                             Toast.makeText(context,  
  238.                                     "设置闹钟时间为" + tmpS1,  
  239.                                     Toast.LENGTH_SHORT).show();  
  240.                           }            
  241.                         },mHour,mMinute,true).show();  
  242.                 }  
  243.             });  
  244.               
  245.             repeatButton1.setOnClickListener(new OnClickListener(){  
  246.                 public void onClick(View arg0) {  
  247.                     // TODO Auto-generated method stub   
  248.                     repeatString1 = "";  
  249.                     new AlertDialog.Builder(context)  
  250.                     .setTitle(R.string.alert_dialog_multi_choice)  
  251.                     .setMultiChoiceItems(R.array.select_dialog_items,  
  252.                             new boolean[]{falsefalsefalsefalsefalsefalsefalse},  
  253.                             new DialogInterface.OnMultiChoiceClickListener() {  
  254.                                 public void onClick(DialogInterface dialog, int whichButton,  
  255.                                         boolean isChecked) {  
  256.                                     /* User clicked on a check box do some stuff */  
  257.                                     switch(whichButton){  
  258.                                        case 0if(isChecked){  
  259.                                                   repeatArray1[0] = 1;  
  260.                                                }else{  
  261.                                                   repeatArray1[0] = 0;  
  262.                                                }  
  263.                                                break;  
  264.                                        case 1:if(isChecked){  
  265.                                                  repeatArray1[1] = 1;  
  266.                                               }else{  
  267.                                                  repeatArray1[1] = 0;  
  268.                                               }  
  269.                                                break;  
  270.                                        case 2:if(isChecked){  
  271.                                                  repeatArray1[2] = 1;  
  272.                                               }else{  
  273.                                                  repeatArray1[2] = 0;  
  274.                                               }  
  275.                                                break;  
  276.                                        case 3:if(isChecked){  
  277.                                                  repeatArray1[3] = 1;  
  278.                                               }else{  
  279.                                                  repeatArray1[3] = 0;  
  280.                                               }  
  281.                                                break;  
  282.                                        case 4:if(isChecked){  
  283.                                                  repeatArray1[4] = 1;  
  284.                                               }else{  
  285.                                                  repeatArray1[4] = 0;  
  286.                                               }  
  287.                                                break;  
  288.                                        case 5:if(isChecked){  
  289.                                                  repeatArray1[5] = 1;  
  290.                                               }else{  
  291.                                                  repeatArray1[5] = 0;  
  292.                                               }  
  293.                                                break;  
  294.                                        case 6:if(isChecked){  
  295.                                                  repeatArray1[6] = 1;  
  296.                                               }else{  
  297.                                                  repeatArray1[6] = 0;  
  298.                                               }  
  299.                                                break;  
  300.                                        defaultbreak;  
  301.                                     }                             
  302.                                 }  
  303.                             })  
  304.                     .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  305.                         public void onClick(DialogInterface dialog, int whichButton) {  
  306.                               
  307.                             if(repeatArray1[0] == 1){  
  308.                                 repeatString1 += "周一"+",";  
  309.                                 repeat_1_1 = true;  
  310.                             }else{  
  311.                                 repeat_1_1 = false;  
  312.                             }  
  313.                               
  314.                             if(repeatArray1[1] == 1){  
  315.                                 repeatString1 += "周二"+",";  
  316.                                 repeat_1_2 = true;  
  317.                             }else{  
  318.                                 repeat_1_2 = false;  
  319.                             }  
  320.                               
  321.                             if(repeatArray1[2] == 1){  
  322.                                 repeatString1 += "周三"+",";  
  323.                                 repeat_1_3 = true;  
  324.                             }else{  
  325.                                 repeat_1_3 = false;  
  326.                             }  
  327.                             
  328.                             if(repeatArray1[3] == 1){  
  329.                                 repeatString1 += "周四"+",";  
  330.                                 repeat_1_4 = true;  
  331.                             }else{  
  332.                                 repeat_1_4 = false;  
  333.                             }  
  334.                               
  335.                             if(repeatArray1[4] == 1){  
  336.                                 repeatString1 += "周五"+",";  
  337.                                 repeat_1_5 = true;  
  338.                             }else{  
  339.                                 repeat_1_5 = false;  
  340.                             }  
  341.                               
  342.                             if(repeatArray1[5] == 1){  
  343.                                 repeatString1 += "周六"+",";  
  344.                                 repeat_1_6 = true;  
  345.                             }else{  
  346.                                 repeat_1_6 = false;  
  347.                             }  
  348.                               
  349.                             if(repeatArray1[6] == 1){  
  350.                                 repeatString1 += "周日";  
  351.                                 repeat_1_7 = true;  
  352.                             }else{  
  353.                                 repeat_1_7 = false;  
  354.                             }  
  355.                               
  356.                             if(!(repeat_1_1 || repeat_1_2 || repeat_1_3 || repeat_1_4 ||  
  357.                                     repeat_1_5 || repeat_1_6 || repeat_1_7)){  
  358.                                 repeatString1 = ("无重复");  
  359.                             }  
  360.                               
  361.                             repeat1TextView.setText(repeatString1);  
  362.   
  363.                             for(int i=0;i<repeatArray1.length;i++){  
  364.                                 repeatArray1[i] = 0;  
  365.                             }   
  366.                         }  
  367.                     })  
  368.                     .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  369.                         public void onClick(DialogInterface dialog, int whichButton) {  
  370.                             /* User clicked No so do some stuff */  
  371.   
  372.                         }  
  373.                     })  
  374.                    .show();  
  375.                 }  
  376.             });  
  377.               
  378.             //show AlertDialog   
  379.             new AlertDialog.Builder(context)  
  380.             .setIcon(R.drawable.alarm_dialog)  
  381.             .setTitle(R.string.alarm_dialog_title)  
  382.             .setView(alarm1View)  
  383.             .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  384.                 public void onClick(DialogInterface dialog, int whichButton) {  
  385.                     if(isOpenInt1){  
  386.                         isOpentime1 = "开";  
  387.                         Intent intent1 = new Intent(context, CallAlarm.class);  
  388.                         intent1.putExtra("RESULT""alarm1");  
  389.                           
  390.                         PendingIntent sender=PendingIntent.getBroadcast(  
  391.                                 context,0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);  
  392.                         AlarmManager am;  
  393.                         am = (AlarmManager)activity.getSystemService(context.ALARM_SERVICE);  
  394.                         int nowDay = Contants.getNowWeek();  
  395.                         int setDay = 0;  
  396.                         if(repeatString1.equals("目前无设置"))  
  397.                         {  
  398.                             if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){  
  399.                                 am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);  
  400.                             }  
  401.                             else{  
  402.                                 Toast.makeText(context, R.string.not_time_right, Toast.LENGTH_SHORT);  
  403.                             }  
  404.                         }  
  405.                         if(!(repeatString1.equals("目前无设置"))){  
  406.                             String[] setStr = repeatString1.split(",");  
  407.                             int[] dayOfNum = Contants.getDayOfNum(setStr);  
  408.                             setDay = Contants.getResultDifferDay(dayOfNum, nowDay);  
  409.                             int differDay = Contants.compareDayNowToNext(nowDay, setDay);  
  410.                             if(differDay == 0){  
  411.                                 if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){  
  412.                                     am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);  
  413.                                 }else{  
  414.                                     am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(7),sender);  
  415.                                 }  
  416.                             }else{  
  417.                                 am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(differDay),sender);    
  418.                             }  
  419.                         }  
  420.                     }else{  
  421.                         isOpentime1 = "关";  
  422.                         Intent intent = new Intent(context, CallAlarm.class);  
  423.                         PendingIntent sender=PendingIntent.getBroadcast(  
  424.                                                context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  425.                         AlarmManager am;  
  426.                         am =(AlarmManager)activity.getSystemService(context.ALARM_SERVICE);  
  427.                         am.cancel(sender);  
  428.                         Toast.makeText(context,R.string.alarm_delete1,  
  429.                                        Toast.LENGTH_SHORT).show();  
  430.                     }  
  431.                     String[] temStr = new String[7];  
  432.                     temStr[0] = tmpS1;  
  433.                     temStr[1] = repeatString1;  
  434.                     temStr[2] = isOpentime1;  
  435.                     dbHelper.updateAlarmColock(1+"", temStr);  
  436.                     refreshDBHelper();  
  437.                 }  
  438.             })  
  439.             .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  440.                 public void onClick(DialogInterface dialog, int whichButton) {  
  441.                     refreshDBHelper();  
  442.                 }  
  443.             })  
  444.             .create().show();  
  445.             Log.v("wangxianming""this is click Item1");  
  446.         }  
  447.         else if(position == 1){  
  448.             LayoutInflater factory = LayoutInflater.from(context);  
  449.             final View alarm1View = factory.inflate(R.layout.time_repeat_dialog, null);  
  450.             time2TextView = (TextView)alarm1View.findViewById(R.id.text);  
  451.             setTimeButton2 = (Button)alarm1View.findViewById(R.id.mButton);  
  452.             repeat2TextView = (TextView)alarm1View.findViewById(R.id.repeattext);  
  453.             repeatButton2 = (Button)alarm1View.findViewById(R.id.repeatButton);  
  454.             time2CheckBox = (CheckBox)alarm1View.findViewById(R.id.isopen_check);  
  455.              
  456.             String isOpen2 = "关";  
  457.               
  458.             if(count==2){  
  459.                 for(int i=0;i<count;i++){  
  460.                   if(i == 1){  
  461.                       cursor.moveToPosition(i);  
  462.                       if(cursor.getString(1)==null){  
  463.                           time2TextView.setText(tmpS2);  
  464.                       }else{  
  465.                           time2TextView.setText(cursor.getString(1));  
  466.                           tmpS2 = cursor.getString(1);  
  467.                       }  
  468.                       if((cursor.getString(2))==null){  
  469.                           repeat2TextView.setText(repeatString2);  
  470.                       }else{  
  471.                           repeat2TextView.setText(cursor.getString(2));  
  472.                           repeatString2 = cursor.getString(2);  
  473.                       }  
  474.                       isOpen2 = cursor.getString(3);  
  475.                       if(isOpen2!=null){  
  476.                           if(isOpen2.equals("开")){  
  477.                               time2CheckBox.setChecked(true);  
  478.                               isOpenInt2 = true;  
  479.                           }else{  
  480.                               time2CheckBox.setChecked(false);  
  481.                               isOpenInt2 = false;  
  482.                           }  
  483.                       }  
  484.                   }  
  485.                 }  
  486.             }  
  487.               
  488.             time2CheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){  
  489.                 public void onCheckedChanged(CompoundButton arg0, boolean arg1) {  
  490.                     // TODO Auto-generated method stub   
  491.                     if(arg0.isChecked()){  
  492.                         isOpenInt2 = true;  
  493.                     }else{  
  494.                         isOpenInt2 = false;  
  495.                     }  
  496.                 }  
  497.             });  
  498.               
  499.             setTimeButton2.setOnClickListener(new OnClickListener(){  
  500.   
  501.                 public void onClick(View arg0) {  
  502.                     // TODO Auto-generated method stub   
  503.                       c.setTimeInMillis(System.currentTimeMillis());  
  504.                       int mHour=c.get(Calendar.HOUR_OF_DAY);  
  505.                       int mMinute=c.get(Calendar.MINUTE);  
  506.                       int mDay=c.get(Calendar.DAY_OF_WEEK);  
  507.                         
  508.                       new TimePickerDialog(context,  
  509.                         new TimePickerDialog.OnTimeSetListener()  
  510.                         {                  
  511.                           public void onTimeSet(TimePicker view,int hourOfDay,  
  512.                                                 int minute)  
  513.                           {  
  514.                             c.setTimeInMillis(System.currentTimeMillis());  
  515.                             c.set(Calendar.HOUR_OF_DAY,hourOfDay);  
  516.                             c.set(Calendar.MINUTE,minute);  
  517.                             c.set(Calendar.SECOND,0);  
  518.                             c.set(Calendar.MILLISECOND,0);  
  519.                                   
  520.                             tmpS2=format(hourOfDay)+":"+format(minute);  
  521.                             time2TextView.setText(tmpS2);  
  522.                               
  523.                             Toast.makeText(context,R.string.alarm_set2+tmpS2,Toast.LENGTH_SHORT).show();  
  524.                           }            
  525.                         },mHour,mMinute,true).show();  
  526.                 }  
  527.             });  
  528.               
  529.             repeatButton2.setOnClickListener(new OnClickListener(){  
  530.   
  531.                 public void onClick(View arg0) {  
  532.                     repeatString2 = "";  
  533.                     new AlertDialog.Builder(context)  
  534.                     .setTitle(R.string.alert_dialog_multi_choice)  
  535.                     .setMultiChoiceItems(R.array.select_dialog_items,  
  536.                             new boolean[]{falsefalsefalsefalsefalsefalsefalse},  
  537.                             new DialogInterface.OnMultiChoiceClickListener() {  
  538.                                 public void onClick(DialogInterface dialog, int whichButton,  
  539.                                         boolean isChecked) {  
  540.                                     /* User clicked on a check box do some stuff */  
  541.                                     switch(whichButton){  
  542.                                        case 0if(isChecked){  
  543.                                                   repeatArray2[0] = 1;  
  544.                                                }else{  
  545.                                                   repeatArray2[0] = 0;  
  546.                                                }  
  547.                                                break;  
  548.                                        case 1:if(isChecked){  
  549.                                                  repeatArray2[1] = 1;  
  550.                                               }else{  
  551.                                                  repeatArray2[1] = 0;  
  552.                                               }  
  553.                                                break;  
  554.                                        case 2:if(isChecked){  
  555.                                                  repeatArray2[2] = 1;  
  556.                                               }else{  
  557.                                                  repeatArray2[2] = 0;  
  558.                                               }  
  559.                                                break;  
  560.                                        case 3:if(isChecked){  
  561.                                                  repeatArray2[3] = 1;  
  562.                                               }else{  
  563.                                                  repeatArray2[3] = 0;  
  564.                                               }  
  565.                                                break;  
  566.                                        case 4:if(isChecked){  
  567.                                                  repeatArray2[4] = 1;  
  568.                                               }else{  
  569.                                                  repeatArray2[4] = 0;  
  570.                                               }  
  571.                                                break;  
  572.                                        case 5:if(isChecked){  
  573.                                                  repeatArray2[5] = 1;  
  574.                                               }else{  
  575.                                                  repeatArray2[5] = 0;  
  576.                                               }  
  577.                                                break;  
  578.                                        case 6:if(isChecked){  
  579.                                                  repeatArray2[6] = 1;  
  580.                                               }else{  
  581.                                                  repeatArray2[6] = 0;  
  582.                                               }  
  583.                                                break;  
  584.                                        defaultbreak;  
  585.                                     }                             
  586.                                 }  
  587.                             })  
  588.                     .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  589.                         public void onClick(DialogInterface dialog, int whichButton) {  
  590.                               
  591.                             if(repeatArray2[0] == 1){  
  592.                                 repeatString2 += "周一"+",";  
  593.                                 repeat_2_1 = true;  
  594.                             }else{  
  595.                                 repeat_2_1 = false;  
  596.                             }  
  597.                               
  598.                             if(repeatArray2[1] == 1){  
  599.                                 repeatString2 += "周二"+",";  
  600.                                 repeat_2_2 = true;  
  601.                             }else{  
  602.                                 repeat_2_2 = false;  
  603.                             }  
  604.                               
  605.                             if(repeatArray2[2] == 1){  
  606.                                 repeatString2 += "周三"+",";  
  607.                                 repeat_2_3 = true;  
  608.                             }else{  
  609.                                 repeat_2_3 = false;  
  610.                             }  
  611.                             
  612.                             if(repeatArray2[3] == 1){  
  613.                                 repeatString2 += "周四"+",";  
  614.                                 repeat_2_4 = true;  
  615.                             }else{  
  616.                                 repeat_2_4 = false;  
  617.                             }  
  618.                               
  619.                             if(repeatArray2[4] == 1){  
  620.                                 repeatString2 += "周五"+",";  
  621.                                 repeat_2_5 = true;  
  622.                             }else{  
  623.                                 repeat_1_5 = false;  
  624.                             }  
  625.                               
  626.                             if(repeatArray2[5] == 1){  
  627.                                 repeatString2 += "周六"+",";  
  628.                                 repeat_2_6 = true;  
  629.                             }else{  
  630.                                 repeat_2_6 = false;  
  631.                             }  
  632.                               
  633.                             if(repeatArray2[6] == 1){  
  634.                                 repeatString2 += "周日";  
  635.                                 repeat_2_7 = true;  
  636.                             }else{  
  637.                                 repeat_2_7 = false;  
  638.                             }  
  639.                               
  640.                             if(!(repeat_2_1 || repeat_2_2 || repeat_2_3 || repeat_2_4 ||  
  641.                                     repeat_2_5 || repeat_2_6 || repeat_2_7)){  
  642.                                 repeatString2 = ("无设置");  
  643.                             }  
  644.                               
  645.                             repeat2TextView.setText(repeatString2);  
  646.                               
  647.                             for(int i=0;i<repeatArray1.length;i++){  
  648.                                 repeatArray2[i] = 0;  
  649.                             }   
  650.                         }  
  651.                     })  
  652.                     .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  653.                         public void onClick(DialogInterface dialog, int whichButton) {  
  654.                             /* User clicked No so do some stuff */  
  655.   
  656.                         }  
  657.                     })  
  658.                     .show();                  
  659.                 }  
  660.             });  
  661.               
  662.             // show Dialog    
  663.             new AlertDialog.Builder(context)  
  664.             .setIcon(R.drawable.alarm_dialog)  
  665.             .setTitle(R.string.alarm_dialog_title)  
  666.             .setView(alarm1View)  
  667.             .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {  
  668.                 public void onClick(DialogInterface dialog, int whichButton) {  
  669.      
  670.                     if(isOpenInt2){  
  671.                         isOpentime2 = "开";  
  672.                         Intent intent2 = new Intent(context, CallAlarm.class);  
  673.                         intent2.putExtra("RESULT""alarm2");  
  674.                         PendingIntent sender=PendingIntent.getBroadcast(  
  675.                                 context,1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);  
  676.                         AlarmManager am;  
  677.                         am = (AlarmManager)activity.getSystemService(context.ALARM_SERVICE);  
  678.                         int nowDay = Contants.getNowWeek();  
  679.                         int setDay = 0;  
  680.                         if(repeatString2.equals("目前无设置"))  
  681.                         {  
  682.                             if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){  
  683.                                 am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);  
  684.                             }  
  685.                             else{  
  686.                                 Toast.makeText(context, R.string.not_time_right, Toast.LENGTH_SHORT);  
  687.                             }  
  688.                         }  
  689.                         if(!(repeatString2.equals("目前无设置"))){  
  690.                             String[] setStr = repeatString2.split(",");  
  691.                             int[] dayOfNum = Contants.getDayOfNum(setStr);  
  692.                             setDay = Contants.getResultDifferDay(dayOfNum, nowDay);  
  693.                             int differDay = Contants.compareDayNowToNext(nowDay, setDay);  
  694.                             if(differDay == 0){  
  695.                                 if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){  
  696.                                     am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);  
  697.                                 }else{  
  698.                                     am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(7),sender);  
  699.                                 }  
  700.                             }else{  
  701.                                 am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(differDay),sender);    
  702.                             }  
  703.                         }  
  704.                     }else{  
  705.                         isOpentime2 = "关";  
  706.                         Intent intent = new Intent(context, CallAlarm.class);  
  707.                         intent.putExtra("RESULT""cancel");  
  708.                         PendingIntent sender=PendingIntent.getBroadcast(  
  709.                                                context,1, intent, PendingIntent.FLAG_UPDATE_CURRENT);  
  710.                         AlarmManager am;  
  711.                         am =(AlarmManager)activity.getSystemService(context.ALARM_SERVICE);  
  712.                         am.cancel(sender);  
  713.                         Toast.makeText(context,R.string.alarm_delete2,  
  714.                                        Toast.LENGTH_SHORT).show();  
  715.                     }  
  716.                       
  717.                     String[] temStr = new String[7];  
  718.                     temStr[0] = tmpS2;  
  719.                     temStr[1] = repeatString2;  
  720.                     temStr[2] = isOpentime2;  
  721.                     dbHelper.updateAlarmColock(2+"", temStr);  
  722.                     refreshDBHelper();  
  723.                 }  
  724.             })  
  725.             .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {  
  726.                 public void onClick(DialogInterface dialog, int whichButton) {  
  727.                     refreshDBHelper();  
  728.                 }  
  729.             })  
  730.             .create().show();  
  731.         }  
  732.         cursor.close();  
  733.         dbHelper.close();  
  734.     }  
  735.       
  736.     private String format(int x)  
  737.     {  
  738.         String s=""+x;  
  739.         if(s.length()==1) s="0"+s;  
  740.         return s;  
  741.     }  
  742. }  


 

三、AlarmClockAdapter。java类,适配器的类:

  1. package com.cn.daming;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.ImageView;  
  11. import android.widget.TextView;  
  12.   
  13. public class AlarmClockAdapter extends BaseAdapter{  
  14.   
  15.     private LayoutInflater layoutInflater;    
  16.     private Context context;   
  17.     private List<String> alarm_ids;  
  18.     private List<String> alarm_times;  
  19.     private List<String> alarm_repeats;  
  20.     private List<String> alarm_isopens;  
  21.     private List<String> alarm_kinds;  
  22.     ZuJian zuJian;  
  23.       
  24.     public AlarmClockAdapter(Context context,List<String> ids,List<String> times,List<String> repeats,  
  25.             List<String> isopens,List<String> kinds) {    
  26.         this.context = context;    
  27.         this.alarm_ids = ids;  
  28.         this.alarm_times = times;  
  29.         this.alarm_repeats = repeats;  
  30.         this.alarm_isopens = isopens;  
  31.         this.alarm_kinds = kinds;  
  32.         this.layoutInflater = LayoutInflater.from(context);    
  33.     }    
  34.       
  35.     public int getCount() {  
  36.         return alarm_times.size();   
  37.     }  
  38.   
  39.     public Object getItem(int position) {  
  40.         return alarm_times.get(position);  
  41.     }  
  42.   
  43.     public long getItemId(int position) {  
  44.         return position;  
  45.     }  
  46.   
  47.     public View getView(int position, View convertView, ViewGroup parent) {  
  48.         zuJian =  new ZuJian();  
  49.         if (convertView == null) {  
  50.             convertView = layoutInflater.inflate(R.layout.alarm_clock, null);  
  51.             zuJian.alarmTitle = (TextView) convertView.findViewById(R.id.alarm_title);  
  52.             zuJian.alarmTimeView = (TextView) convertView.findViewById(R.id.alarm_time);  
  53.             zuJian.repeatAlarmView = (TextView) convertView.findViewById(R.id.alarm_repeat_time);  
  54.             zuJian.showISOpenView = (ImageView) convertView.findViewById(R.id.show_open_close);  
  55.             convertView.setTag(zuJian);  
  56.         } else {  
  57.             zuJian = (ZuJian) convertView.getTag();  
  58.         }  
  59.         if (alarm_kinds.get(position) == null || alarm_kinds.get(position) == "") {  
  60.             if(position == 0){  
  61.                 zuJian.alarmTitle.setText("大明闹钟一");  
  62.             }  
  63.             if(position == 1){  
  64.                 zuJian.alarmTitle.setText("大明闹钟二");  
  65.             }  
  66.         } else {  
  67.             zuJian.alarmTitle.setText(alarm_kinds.get(position));  
  68.         }  
  69.   
  70.         if (alarm_times.get(position) == null || alarm_times.get(position) == "") {  
  71.             zuJian.alarmTimeView.setText("目前无设置");  
  72.         } else {  
  73.             zuJian.alarmTimeView.setText(alarm_times.get(position));  
  74.         }  
  75.   
  76.         if (alarm_repeats.get(position) == null || alarm_repeats.get(position) == "") {  
  77.             zuJian.repeatAlarmView.setText("目前无设置");  
  78.         } else {  
  79.             zuJian.repeatAlarmView.setText(alarm_repeats.get(position));  
  80.         }  
  81.   
  82.         if (alarm_isopens.get(position) == null || alarm_isopens.get(position) == "") {  
  83.            ((ImageView) zuJian.showISOpenView).setBackgroundResource(R.drawable.alarm_dialog);  
  84.         } else {  
  85.             if(alarm_isopens.get(position).equals("开")){  
  86.                 ((ImageView) zuJian.showISOpenView).setBackgroundResource(R.drawable.clock);  
  87.             }  
  88.             if(alarm_isopens.get(position).equals("关")){  
  89.                 ((ImageView) zuJian.showISOpenView).setBackgroundResource(R.drawable.alarm_dialog);  
  90.             }  
  91.         }  
  92.           
  93.         return convertView;  
  94.     }  
  95.   
  96.     final class ZuJian {  
  97.         public TextView alarmTitle;  
  98.         public TextView alarmTimeView;  
  99.         public TextView repeatAlarmView;  
  100.         public ImageView showISOpenView;  
  101.           
  102.     }  
  103. }  


 

四、CallAlarm。java类,接受广播BroadcastReceiver的类:

  1. package com.cn.daming;  
  2.   
  3. /* import class */  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.util.Log;  
  9.   
  10. /*AlarmReceiver */  
  11. public class CallAlarm extends BroadcastReceiver  
  12. {  
  13.   @Override  
  14.   public void onReceive(final Context context, Intent intent)  
  15.   {     
  16.         String getStr = intent.getExtras().getString("RESULT");  
  17.         Log.v("wangxianming""RESULT = "+getStr);  
  18.         Intent alaramIntent = new Intent(context, AlarmAgainSetting.class);  
  19.         Bundle bundleRet = new Bundle();  
  20.         bundleRet.putString("STR_RESULT", getStr);  
  21.         alaramIntent.putExtras(bundleRet);  
  22.         alaramIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  23.         context.startActivity(alaramIntent);  
  24.   }  
  25. }  


 

五、Contants。java类,工具类:

  1. package com.cn.daming;  
  2.   
  3. import java.text.SimpleDateFormat;  
  4. import java.util.ArrayList;  
  5. import java.util.Calendar;  
  6. import java.util.Date;  
  7. import java.util.HashMap;  
  8. import java.util.Map;  
  9. import java.util.Random;  
  10.   
  11. public class Contants {  
  12.       
  13.     //repeated day of week define   
  14.     public static Map<String, Integer> mapWeek = new HashMap<String,Integer>();  
  15.       
  16.     public static void addMapWeek(){  
  17.         mapWeek.put("周一"1);  
  18.         mapWeek.put("周二"2);  
  19.         mapWeek.put("周三"3);  
  20.         mapWeek.put("周四"4);  
  21.         mapWeek.put("周五"5);  
  22.         mapWeek.put("周六"6);  
  23.         mapWeek.put("周日"7);  
  24.         mapWeek.put("无重复"0);  
  25.     }  
  26.       
  27.     public static int getMapWeek(String str){  
  28.         Contants.addMapWeek();  
  29.         int dayOfMapWeek = 0;  
  30.         if(str != null){  
  31.             dayOfMapWeek = mapWeek.get(str);  
  32.         }  
  33.         return dayOfMapWeek;  
  34.     }  
  35.       
  36.     public static String[] getDatetimeString(){  
  37.         Date date = new Date();  
  38.         String[] tempStr = new String[2];  
  39.         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  40.         String str = format.format(date);  
  41.         tempStr[0] = str.substring(010);  
  42.         tempStr[1] = str.substring(11, str.length());  
  43.         return tempStr;  
  44.     }  
  45.       
  46.     public static long getNowTimeMinuties()  
  47.     {  
  48.         return System.currentTimeMillis();  
  49.     }  
  50.       
  51.     public static boolean differSetTimeAndNowTime(long setTime)  
  52.     {  
  53.         if(setTime >= getNowTimeMinuties()){  
  54.             return true;  
  55.         }else{  
  56.             return false;  
  57.         }  
  58.     }  
  59.       
  60.     public static long getDifferMillis(int differDays)  
  61.     {  
  62.         return differDays * 24 * 60 * 60 * 1000;  
  63.     }  
  64.       
  65.     //compare nowDay to nextDay   
  66.     public static int compareDayNowToNext(int nowDay,int nextDay){  
  67.         if(nextDay > nowDay){  
  68.             return (nextDay-nowDay);  
  69.         }else if(nextDay == nowDay){  
  70.             return 0;     
  71.         }else{  
  72.             return (7-(nowDay-nextDay));  
  73.         }  
  74.     }  
  75.       
  76.     //turn the nowday to China`s day of Week   
  77.     public static Map<String, Integer> nowWeek = new HashMap<String,Integer>();  
  78.       
  79.     public static void addNowWeek()  
  80.     {  
  81.         nowWeek.put("1"7);  
  82.         nowWeek.put("2"1);  
  83.         nowWeek.put("3"2);  
  84.         nowWeek.put("4"3);  
  85.         nowWeek.put("5"4);  
  86.         nowWeek.put("6"5);  
  87.         nowWeek.put("7"6);  
  88.     }  
  89.       
  90.     public static int getNowWeek()  
  91.     {  
  92.         Calendar nowCal = Calendar.getInstance();  
  93.         Date nowDate = new Date(System.currentTimeMillis());  
  94.         nowCal.setTime(nowDate);  
  95.         int nowNum = nowCal.get(nowCal.DAY_OF_WEEK);  
  96.         String nowNumStr = String.valueOf(nowNum);  
  97.         Contants.addNowWeek();  
  98.         int nowDayOfWeek = 0;  
  99.         if(nowNumStr != null){  
  100.             nowDayOfWeek = nowWeek.get(nowNumStr);  
  101.         }  
  102.         return nowDayOfWeek;  
  103.     }  
  104.       
  105.     public static int getSetDay(String str)  
  106.     {  
  107.             if(str.equals("周一")){  
  108.                 return 1;  
  109.             }  
  110.             if(str.equals("周二")){  
  111.                 return 2;  
  112.             }  
  113.             if(str.equals("周三")){  
  114.                 return 3;  
  115.             }  
  116.             if(str.equals("周四")){  
  117.                 return 4;  
  118.             }  
  119.             if(str.equals("周五")){  
  120.                 return 5;  
  121.             }  
  122.             if(str.equals("周六")){  
  123.                 return 6;  
  124.             }  
  125.             if(str.equals("周日")){  
  126.                 return 7;  
  127.             }  
  128.             return 0;  
  129.     }  
  130.       
  131.     public static int[] getDayOfNum(String[] str)  
  132.     {  
  133.         int[] dayOfInt = new int[str.length];  
  134.         for(int i=0;i<str.length;i++){  
  135.             dayOfInt[i] = getSetDay(str[i]);  
  136.         }  
  137.         return dayOfInt;  
  138.     }  
  139.       
  140.     public static int getResultDifferDay(int[] in,int nowDay)  
  141.     {  
  142.          int result = 0;  
  143.          for(int i=0;i<in.length;i++){  
  144.              if(in[i] >= nowDay){  
  145.                  result = in[i];  
  146.                  break;  
  147.              }  
  148.          }  
  149.            
  150.          if(result == 0){  
  151.              result = in[0];  
  152.          }  
  153.          return result;  
  154.     }  
  155.       
  156.     public static int getResultDifferDay1(int[] in,int nowDay)  
  157.     {  
  158.          int result = 0;  
  159.          for(int i=0;i<in.length;i++){  
  160.              if(in[i] > nowDay){  
  161.                  result = in[i];  
  162.                  break;  
  163.              }  
  164.          }  
  165.            
  166.          if(result == 0){  
  167.              result = in[0];  
  168.          }  
  169.          return result;  
  170.     }  
  171. }  


 

六、DataBaseHelper。java类,数据库sqlite类:

  1. package com.cn.daming;  
  2.   
  3. import android.content.ContentValues;  
  4. import android.content.Context;  
  5. import android.database.Cursor;  
  6. import android.database.sqlite.SQLiteDatabase;  
  7. import android.database.sqlite.SQLiteOpenHelper;  
  8. import android.util.Log;  
  9.   
  10. public class DataBaseHelper extends SQLiteOpenHelper {  
  11.   
  12.     private final static String DATABASE_NAME = "alarm_db";  
  13.     private final static int DATABASE_VERSION = 1;  
  14.       
  15.     private final static String ALARM_COLOCK_TABLE = "alarmcolock";  
  16.     public final static String ALARM_ID = "_id";  
  17.     public final static String ALARM_TIME = "alarmtime"//alarm time   
  18.     public final static String ALARM_REPEAT = "alarmrepeat";//alarm repeate is or not   
  19.     public final static String ALARM_ISOPEN = "alarmisopen";//alarm open is 0r not   
  20.     public final static String ALARM_KIND = "alarmkind"//alarm is kind(1 is shuangseqiu ,2 is da le tou )   
  21.     public final static String ALARM_SPARE1 = "alarmspare1";//spare1   
  22.     public final static String ALARM_SPARE2 = "alarmspare2";//spare2   
  23.     public final static String ALARM_SPARE3 = "alarmspare3";//spare3   
  24.       
  25.     public DataBaseHelper(Context context) {  
  26.         super(context, DATABASE_NAME, null, DATABASE_VERSION);  
  27.     }  
  28.   
  29.     @Override  
  30.     public void onCreate(SQLiteDatabase db) {  
  31.   
  32.         String  sql = "create table "+ALARM_COLOCK_TABLE+" ("  
  33.                         +ALARM_ID+" integer primary key autoincrement, "  
  34.                         +ALARM_TIME+" text, "  
  35.                         +ALARM_REPEAT+" text, "  
  36.                         +ALARM_ISOPEN+" text, "  
  37.                         +ALARM_KIND+" text, "  
  38.                         +ALARM_SPARE1+" text, "  
  39.                         +ALARM_SPARE2+" text, "  
  40.                         +ALARM_SPARE3+" text )";  
  41.                db.execSQL(sql);  
  42.     }  
  43.   
  44.     @Override  
  45.     public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {  
  46.           
  47.         String sql = "drop table if exists "+ALARM_COLOCK_TABLE;  
  48.         db.execSQL(sql);  
  49.     }  
  50.   
  51.       
  52.     //the action in AlarmColock table    
  53.     public long insertAlarmColock(String[] strArray)  
  54.     {  
  55.         SQLiteDatabase db = this.getWritableDatabase();  
  56.         ContentValues conv = new ContentValues();  
  57.         conv.put(ALARM_TIME, strArray[0]);  
  58.         conv.put(ALARM_REPEAT, strArray[1]);  
  59.         conv.put(ALARM_ISOPEN, strArray[2]);  
  60.         conv.put(ALARM_KIND, strArray[3]);    
  61.         return db.insert(ALARM_COLOCK_TABLE, null, conv);  
  62.     }  
  63.       
  64.     public Cursor selectAlarmColock(){  
  65.         SQLiteDatabase db = this.getReadableDatabase();  
  66.         Cursor cursor = db.query(ALARM_COLOCK_TABLE, nullnullnullnullnullnull);  
  67.         return cursor;  
  68.     }  
  69.       
  70.     public Cursor getAlarmColock(String id){  
  71.         SQLiteDatabase db = this.getReadableDatabase();  
  72.         String where = ALARM_ID+"=?";  
  73.         String[] whereValues = {id};  
  74.         Cursor cursor = db.query(ALARM_COLOCK_TABLE, null, where, whereValues, nullnullnull);  
  75.         return cursor;  
  76.     }  
  77.       
  78.     public void deleteAlarmColock(String id){  
  79.         SQLiteDatabase db = this.getWritableDatabase();  
  80.         String where = ALARM_ID+"=?";  
  81.         String[] whereValues = {id};  
  82.         db.delete(ALARM_COLOCK_TABLE, where, whereValues);  
  83.     }  
  84.       
  85.     public int updateAlarmColock(String id,String[] strArray){  
  86.         SQLiteDatabase db = this.getWritableDatabase();  
  87.         String where = ALARM_ID+"=?";  
  88.         String[] whereValues = {id};  
  89.         ContentValues cv = new ContentValues();  
  90.         cv.put(ALARM_TIME, strArray[0]);  
  91.         cv.put(ALARM_REPEAT, strArray[1]);  
  92.         cv.put(ALARM_ISOPEN, strArray[2]);  
  93.         cv.put(ALARM_KIND, strArray[3]);      
  94.         Log.v("wangxianming""cv : "+cv.get("ALARM_TIME")+cv.get("ALARM_REPEAT")+cv.get("ALARM_ISOPEN")+cv.get("ALARM_KIND"));  
  95.         return db.update(ALARM_COLOCK_TABLE, cv, where, whereValues);  
  96.     }  
  97. }  


 

七、AlarmAgainSetting。java类,闹钟重复设置的类:

  1. package com.cn.daming;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlarmManager;  
  5. import android.app.AlertDialog;  
  6. import android.app.PendingIntent;  
  7. import android.content.DialogInterface;  
  8. import android.content.Intent;  
  9. import android.database.Cursor;  
  10. import android.os.Bundle;  
  11. import android.widget.Toast;  
  12.   
  13. public class AlarmAgainSetting extends Activity{  
  14.   
  15.     private DataBaseHelper dbHelper;  
  16.     private Cursor cursor;  
  17.     Intent getAlarmAgainSetting;  
  18.       
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.           
  23.         getAlarmAgainSetting = AlarmAgainSetting.this.getIntent();  
  24.         String getAlarmAgainSettingStr = getAlarmAgainSetting.getExtras().getString("STR_RESULT");  
  25.           
  26.         dbHelper = new DataBaseHelper(this);  
  27.           
  28.         if(getAlarmAgainSettingStr.equals("alarm1")){  
  29.             Intent againIntent = new Intent(AlarmAgainSetting.this, CallAlarm.class);  
  30.             againIntent.putExtra("RESULT""alarm1");  
  31.             PendingIntent sender=PendingIntent.getBroadcast(  
  32.                     AlarmAgainSetting.this,0, againIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  33.             AlarmManager am;  
  34.             am = (AlarmManager)getSystemService(ALARM_SERVICE);  
  35.             int nowDay = Contants.getNowWeek();  
  36.             int setDay = 0;  
  37.               
  38.             cursor = dbHelper.selectAlarmColock();  
  39.             String str1 = null;  
  40.             int count1 = cursor.getCount();  
  41.             if(count1 > 0){  
  42.                 for(int i=0;i<count1;i++){  
  43.                       if(i == 0){  
  44.                           cursor.moveToPosition(i);  
  45.                           str1 = cursor.getString(2);  
  46.                       }  
  47.                 }  
  48.             }  
  49.             if(!(str1.equals("目前无设置"))){  
  50.                 String[] setStr = str1.split(",");  
  51.                 int[] dayOfNum = Contants.getDayOfNum(setStr);  
  52.                 setDay = Contants.getResultDifferDay1(dayOfNum, nowDay);  
  53.                 int differDay = Contants.compareDayNowToNext(nowDay, setDay);  
  54.                 if(differDay == 0){  
  55.                    am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(7),sender);  
  56.                 }else{  
  57.                     am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(differDay),sender);    
  58.                 }  
  59.             }  
  60.         }  
  61.           
  62.         if(getAlarmAgainSettingStr.equals("alarm2")){  
  63.             /* allAlarm.class */  
  64.             Intent againIntent = new Intent(AlarmAgainSetting.this, CallAlarm.class);  
  65.             againIntent.putExtra("RESULT""alarm2");  
  66.             /* PendingIntent */  
  67.             PendingIntent sender=PendingIntent.getBroadcast(  
  68.                     AlarmAgainSetting.this,1, againIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
  69.             /* AlarmManager.RTC_WAKEUP 
  70.              * */  
  71.             AlarmManager am;  
  72.             am = (AlarmManager)getSystemService(ALARM_SERVICE);  
  73.             int nowDay = Contants.getNowWeek();  
  74.             int setDay = 0;  
  75.               
  76.             cursor = dbHelper.selectAlarmColock();  
  77.             String str2 = null;  
  78.             int count2 = cursor.getCount();  
  79.             if(count2 > 0){  
  80.                 for(int i=0;i<count2;i++){  
  81.                       if(i == 1){  
  82.                           cursor.moveToPosition(i);  
  83.                           str2 = cursor.getString(2);  
  84.                       }  
  85.                 }  
  86.             }  
  87.             if(!(str2.equals("目前无设置"))){  
  88.                 String[] setStr = str2.split(",");  
  89.                 int[] dayOfNum = Contants.getDayOfNum(setStr);  
  90.                 setDay = Contants.getResultDifferDay1(dayOfNum, nowDay);  
  91.                 int differDay = Contants.compareDayNowToNext(nowDay, setDay);  
  92.                 if(differDay == 0){  
  93.                    am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(7),sender);  
  94.                 }else{  
  95.                     am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(differDay),sender);    
  96.                 }  
  97.             }  
  98.         }  
  99.         cursor.close();  
  100.         dbHelper.close();  
  101.           
  102.         Toast.makeText(this, R.string.alarm_time_come, Toast.LENGTH_SHORT).show();  
  103.             
  104.          new AlertDialog.Builder(this)  
  105.          .setIcon(R.drawable.clock)  
  106.          .setTitle("大明闹钟时间到了!!")  
  107.          .setMessage("你使用大明闹钟时间到了!!!")  
  108.          .setPositiveButton("确定",  
  109.           new DialogInterface.OnClickListener()  
  110.          {  
  111.            public void onClick(DialogInterface dialog, int whichButton)  
  112.            {  
  113.              /*AlarmAlertActivity */  
  114.              finish();  
  115.            }  
  116.          }).setNegativeButton("取消",   
  117.                  new DialogInterface.OnClickListener()  
  118.          {  
  119.            public void onClick(DialogInterface dialog, int whichButton)  
  120.            {  
  121.              /*AlarmAlertActivity */  
  122.                finish();  
  123.            }  
  124.          }).show();  
  125.     }  
  126.   
  127. }  


 

布局文件

一、main。xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="@drawable/alarm_bg"  
  7.     >  
  8.    <LinearLayout android:id="@+id/box"    
  9.         android:layout_width="fill_parent"    
  10.         android:layout_height="fill_parent"    
  11.         >        
  12.     </LinearLayout>    
  13. </LinearLayout>  

二、alarm_clock.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.   xmlns:android="http://schemas.android.com/apk/res/android"  
  5.   android:layout_width="320dip"  
  6.   android:layout_height="wrap_content"  
  7.   android:orientation="vertical">  
  8.           <TextView    
  9.             android:id="@+id/alarm_title"    
  10.             android:layout_width="320dip"     
  11.             android:layout_height="wrap_content"   
  12.             android:textColor="#FF000000"     
  13.             android:textSize="23px"     
  14.             android:layout_margin="5px"  
  15.           />   
  16.           <LinearLayout  
  17.               xmlns:android="http://schemas.android.com/apk/res/android"  
  18.               android:layout_width="320dip"  
  19.               android:layout_height="wrap_content"  
  20.               android:orientation="horizontal"  
  21.               >  
  22.               <LinearLayout  
  23.                   xmlns:android="http://schemas.android.com/apk/res/android"  
  24.                   android:layout_width="250dip"  
  25.                   android:layout_height="wrap_content"  
  26.                   android:orientation="horizontal">     
  27.                   <TextView  
  28.                      android:id="@+id/alarm_image"  
  29.                      android:scaleType="centerCrop"  
  30.                      android:layout_width="20dip"  
  31.                      android:layout_height="wrap_content"  
  32.                      android:layout_margin="8px"  
  33.                   />  
  34.                   <LinearLayout    
  35.                       android:layout_width="222dip"    
  36.                       android:layout_height="wrap_content"    
  37.                       android:orientation="vertical"    
  38.                    >    
  39.                         <TextView    
  40.                             android:id="@+id/alarm_time"    
  41.                             android:layout_width="222dip"     
  42.                             android:layout_height="wrap_content"   
  43.                             android:paddingLeft="8dip"  
  44.                             android:layout_marginBottom="3dip"     
  45.                             android:textColor="#FF000000"     
  46.                             android:textSize="18px"     
  47.                         />   
  48.                          <TextView  
  49.                                 android:id="@+id/alarm_repeat_time"  
  50.                                 android:layout_width="222dip"  
  51.                                 android:layout_height="wrap_content"  
  52.                                 android:paddingLeft="8dip"  
  53.                                 android:layout_gravity="left"  
  54.                                 android:textColor="#FF000000"  
  55.                                 android:textSize="10px"  
  56.                           />   
  57.                      </LinearLayout>   
  58.                </LinearLayout>  
  59.                <View  
  60.                   android:layout_width="2dip"  
  61.                   android:layout_height="match_parent"  
  62.                   android:textColor="#FF000000"  
  63.               />  
  64.                <ImageView  
  65.                   android:id="@+id/show_open_close"  
  66.                   android:layout_width="38dip"  
  67.                   android:layout_height="wrap_content"  
  68.                   android:paddingTop="35dip"  
  69.                   android:scaleType="centerCrop"  
  70.                   android:layout_gravity="bottom"  
  71.                   android:gravity="bottom"  
  72.                   android:textColor="#FF000000"  
  73.               />  
  74.               <TextView  
  75.                   android:layout_width="30dip"  
  76.                   android:layout_height="wrap_content"  
  77.               />  
  78.      </LinearLayout>  
  79. </LinearLayout>  

三、alarm_listview.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:id="@+id/note_textview"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"   
  11.         android:layout_gravity="center_horizontal|center_vertical"  
  12.         android:gravity="center_horizontal|center_vertical"  
  13.         android:paddingLeft="10dip"  
  14.         android:text="@string/alarm_list"  
  15.         android:textColor="#FF000000"   
  16.         android:textSize="30dip"  
  17.         android:textStyle="bold"  
  18.         android:paddingTop="6dip"  
  19.         android:paddingBottom="6dip"  
  20.      />  
  21.       
  22.     <ListView  
  23.         android:id="@+id/alarm_list"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="fill_parent"  
  26.     />  
  27. </LinearLayout>  

 

四、time_repeat_dialog.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   >  
  7.   <AbsoluteLayout  
  8.       xmlns:android="http://schemas.android.com/apk/res/android"  
  9.       android:id="@+id/layout1"  
  10.       android:layout_width="fill_parent"  
  11.       android:layout_height="fill_parent"  
  12.       android:background="@drawable/white"  
  13.     >  
  14.         <DigitalClock   
  15.             android:id="@+id/dClock"  
  16.             android:layout_width="wrap_content"   
  17.             android:layout_height="wrap_content"  
  18.             android:textSize="40sp"   
  19.             android:textColor="@drawable/blue"  
  20.             android:layout_x="58px"   
  21.             android:layout_y="5px">  
  22.         </DigitalClock>  
  23.     
  24.         <TextView  
  25.             android:id="@+id/isopen_text"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:text="@string/str_isopen_text"  
  29.             android:textSize="16px"  
  30.             android:textColor="@drawable/black"  
  31.             android:layout_x="10px"  
  32.             android:layout_y="64px"  
  33.           >  
  34.           </TextView>  
  35.           <CheckBox  
  36.             android:id="@+id/isopen_check"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:textSize="20sp"  
  40.             android:textColor="@drawable/black"  
  41.             android:layout_x="160px"  
  42.             android:layout_y="54px"  
  43.           />  
  44.     
  45.          <TextView  
  46.             android:id="@+id/text"  
  47.             android:layout_width="wrap_content"  
  48.             android:layout_height="wrap_content"  
  49.             android:text="@string/str_title1"  
  50.             android:textSize="20sp"  
  51.             android:textColor="@drawable/black"  
  52.             android:layout_x="10px"  
  53.             android:layout_y="104px"  
  54.           >  
  55.           </TextView>  
  56.           <Button  
  57.             android:id="@+id/mButton"  
  58.             android:layout_width="108px"  
  59.             android:layout_height="40px"  
  60.             android:text="@string/str_button1"  
  61.             android:textColor="@drawable/black"  
  62.             android:textSize="18sp"  
  63.             android:layout_x="160px"  
  64.             android:layout_y="102px"  
  65.           >  
  66.           </Button>  
  67.             
  68.           <TextView  
  69.             android:id="@+id/repeattext"  
  70.             android:layout_width="wrap_content"  
  71.             android:layout_height="wrap_content"  
  72.             android:text="@string/str_default"  
  73.             android:textSize="16sp"  
  74.             android:textColor="@drawable/red"  
  75.             android:layout_x="10px"  
  76.             android:layout_y="145px"  
  77.           >  
  78.           </TextView>  
  79.           <Button  
  80.             android:id="@+id/repeatButton"  
  81.             android:layout_width="108px"  
  82.             android:layout_height="40px"   
  83.             android:layout_x="160px"  
  84.             android:layout_y="182px"  
  85.             android:textColor="@drawable/black"  
  86.             android:textSize="18sp"  
  87.             android:text="@string/repeat_button"  
  88.           />  
  89.      </AbsoluteLayout>  
  90. </LinearLayout>  


value目录下的文件

一、arrays.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="select_dialog_items">  
  4.         <item>周一</item>  
  5.         <item>周二</item>  
  6.         <item>周三</item>  
  7.         <item>周四</item>  
  8.         <item>周五</item>  
  9.         <item>周六</item>  
  10.         <item>周日</item>  
  11.     </string-array>     
  12. </resources>  

二、color.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   <drawable name="white">#FFFFFFFF</drawable>  
  4.   <drawable name="black">#000000</drawable>  
  5.   <drawable name="blue">#0000FF</drawable>  
  6.   <drawable name="red">#FF0000</drawable>  
  7.   <drawable name="dackgray">#666666</drawable>  
  8.   <drawable name="yellow">#FFFFFF00</drawable>  
  9. </resources>  

三、string.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, MainActivity!</string>  
  4.     <string name="app_name">AlarmApp</string>  
  5.     <string name="alarm_list">大明闹钟</string>  
  6.     <string name="not_dbcursor_values">数据库中没有值,请设置你的闹钟。。。。</string>  
  7.     <string name="str_isopen_text">是否开启闹钟</string>  
  8.     <string name="str_title1">设置闹钟</string>  
  9.     <string name="str_button1">设置时间</string>  
  10.     <string name="str_default">目前无设置</string>  
  11.     <string name="repeat_button">设置重复</string>  
  12.     <string name="alert_dialog_ok">确定</string>  
  13.     <string name="alert_dialog_cancel">取消</string>  
  14.     <string name="alarm_dialog_title">闹钟设置</string>  
  15.     <string name="alert_dialog_multi_choice">重复闹钟设置</string>  
  16.     <string name="alarm_time_come">您使用大明闹钟设置的闹钟时间到了!!!</string>  
  17.     <string name="alarm_delete1">大明闹钟一删除成功!</string>  
  18.     <string name="alarm_delete2">大明闹钟二删除成功!</string>  
  19.     <string name="alarm_set2">大明闹钟二时间为:</string>  
  20.     <string name="not_time_right">闹钟时间设置不对,请选择周几重复!</string>  
  21. </resources>  
  1. <strong><span style="font-size:24px;color:#660000;">注意:广播一定要在Manifest中注册</span></strong>  

AndroidManifest.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.cn.daming"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.       
  10.     <!-- 註冊receiver CallAlarm -->  
  11.         <receiver android:name=".CallAlarm" android:process=":remote" />  
  12.         <activity android:name=".MainActivity"  
  13.                   android:label="@string/app_name">  
  14.             <intent-filter>  
  15.                 <action android:name="android.intent.action.MAIN" />  
  16.                 <category android:name="android.intent.category.LAUNCHER" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.         <activity android:name=".AlarmAgainSetting"/>  
  20.     </application>  
  21. </manifest>  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值