展会信息港展会大全

Android闹钟【复杂版】【大明进化十五】
来源:互联网   发布日期:2015-09-29 10:01:47   浏览:4198次  

导读:最近做闹钟,所以自己写了个Demo版本,这个程序是用listview单独的类来实现的,和activity类分开来实现的!这个是用数据库进行更新的,当闹钟设置后,闹钟图片变成闹钟的样...

最近做闹钟,所以自己写了个Demo版本,这个程序是用listview单独的类来实现的,和activity类分开来实现的!这个是用数据库进行更新的,当闹钟设置后,闹钟图片变成闹钟的样子,闹钟取消后,图片变成灰色的闹钟,这个是用ListView来实现数据库更新数据的!然后弹对话框来实现时间的设置和周几重复的功能,这个功能能实现,我测试了!正确无误!需要注意的地方有两个:listview每次滑动的时候,记得设置背景为透明,mAlarmListView.setCacheColorHint(0);另一个是利用Intent传递广播的Intent的时候,传参数的时候,记得设置PendingIntentsender=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类,程序入口类:

package com.cn.daming;

import android.app.Activity;

import android.os.Bundle;

import android.widget.LinearLayout;

public class MainActivity extends Activity {

private LinearLayout mLinearLayout;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.setContentView(R.layout.main);

mLinearLayout = (LinearLayout)findViewById(R.id.box);

new AlarmClockView(this,this,mLinearLayout);

}

}

二、AlarmClockView。java类,listview的类:

package com.cn.daming;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.List;

import android.app.AlarmManager;

import android.app.AlertDialog;

import android.app.PendingIntent;

import android.app.TimePickerDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.Intent;

import android.database.Cursor;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.AdapterView;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.LinearLayout;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.TimePicker;

import android.widget.Toast;

public class AlarmClockView extends View{

private MainActivity activity;

private LinearLayout mLinearLayout;

private Context context;

private DataBaseHelper dbHelper;

private Cursor cursor;

private ListView mAlarmListView;

private List<String> ids;

private List<String> times;

private List<String> repeats;

private List<String> isopens;

private List<String> kinds;

private AlarmClockAdapter alarmAdapter;

Calendar c=Calendar.getInstance();

TextView time1TextView = null;

TextView time2TextView = null;

Button setTimeButton1 = null;

Button setTimeButton2 = null;

TextView repeat1TextView = null;

TextView repeat2TextView = null;

Button repeatButton1 = null;

Button repeatButton2 = null;

CheckBox time1CheckBox = null;

CheckBox time2CheckBox = null;

String tmpS1 = "目前无设置";

String repeatString1 = "目前无设置";

String tmpS2 = "目前无设置";

String repeatString2 = "目前无设置";

boolean isOpenInt1 = false;

boolean isOpenInt2 = false;

String isOpentime1 = null;

String isOpentime2 = null;

boolean isOpenAlarm = false;

CheckBox isOpenCheckBox;

int[] repeatArray1 = {0,0,0,0,0,0,0};

int[] repeatArray2 = {0,0,0,0,0,0,0};

boolean repeat_1_1 = false;

boolean repeat_1_2 = false;

boolean repeat_1_3 = false;

boolean repeat_1_4 = false;

boolean repeat_1_5 = false;

boolean repeat_1_6 = false;

boolean repeat_1_7 = false;

boolean repeat_2_1 = false;

boolean repeat_2_2 = false;

boolean repeat_2_3 = false;

boolean repeat_2_4 = false;

boolean repeat_2_5 = false;

boolean repeat_2_6 = false;

boolean repeat_2_7 = false;

public AlarmClockView(final Context context,MainActivity activity, LinearLayout linearLayout) {

super(context);

this.context = context;

this.activity = activity;

this.mLinearLayout = linearLayout;

View alarmView = View.inflate(context, R.layout.alarm_listview, null);

mLinearLayout.addView(alarmView);

mAlarmListView = (ListView)alarmView.findViewById(R.id.alarm_list);

mAlarmListView.setCacheColorHint(0);

dbHelper = new DataBaseHelper(context);

refreshDBHelper();

mAlarmListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

public void onItemClick(AdapterView<?> arg0, View v, int position,

long arg3) {

showAlarmDialog(position);

}

});

}

public void refreshDBHelper()

{

cursor = dbHelper.selectAlarmColock();

ids = new ArrayList<String>();

times = new ArrayList<String>();

repeats = new ArrayList<String>();

isopens = new ArrayList<String>();

kinds = new ArrayList<String>();

int count = cursor.getCount();

if(count==0){

String[] tempStr = new String[4];

for(int i=1;i<=2;i++){

dbHelper.insertAlarmColock(tempStr);

}

int count2 = cursor.getCount();

if(count2>0){

for(int i=0;i<count2;i++){

cursor.moveToPosition(i);

ids.add(cursor.getString(0));

times.add(cursor.getString(1));

repeats.add(cursor.getString(2));

isopens.add(cursor.getString(3));

kinds.add(cursor.getString(4));

}

}

cursor.close();

dbHelper.close();

refreshDBHelper();

Toast.makeText(context, R.string.not_dbcursor_values, Toast.LENGTH_SHORT).show();

}

else if(count>0){

for(int i=0;i<count;i++){

cursor.moveToPosition(i);

ids.add(cursor.getString(0));

times.add(cursor.getString(1));

repeats.add(cursor.getString(2));

isopens.add(cursor.getString(3));

kinds.add(cursor.getString(4));

}

}

alarmAdapter = new AlarmClockAdapter(context,ids,times,repeats,isopens,kinds);

mAlarmListView.setAdapter(alarmAdapter);

cursor.close();

dbHelper.close();

}

public void showAlarmDialog(int position){

dbHelper = new DataBaseHelper(context);

cursor = dbHelper.selectAlarmColock();

int count = cursor.getCount();

if(position == 0){

LayoutInflater factory = LayoutInflater.from(context);

final View alarm1View = factory.inflate(R.layout.time_repeat_dialog, null);

time1TextView = (TextView)alarm1View.findViewById(R.id.text);

setTimeButton1 = (Button)alarm1View.findViewById(R.id.mButton);

repeat1TextView = (TextView)alarm1View.findViewById(R.id.repeattext);

repeatButton1 = (Button)alarm1View.findViewById(R.id.repeatButton);

time1CheckBox = (CheckBox)alarm1View.findViewById(R.id.isopen_check);

String isOpen1 = "关";

if(count==2){

for(int i=0;i<count;i++){

if(i == 0){

cursor.moveToPosition(i);

if(cursor.getString(1)==null){

time1TextView.setText(tmpS1);

}else{

time1TextView.setText(cursor.getString(1));

tmpS1 = cursor.getString(1);

}

if((cursor.getString(2))==null){

repeat1TextView.setText(repeatString1);

}else{

repeat1TextView.setText(cursor.getString(2));

repeatString1 = cursor.getString(2);

}

isOpen1 = cursor.getString(3);

if(isOpen1!=null){

if(isOpen1.equals("开")){

time1CheckBox.setChecked(true);

isOpenInt1 = true;

}else{

time1CheckBox.setChecked(false);

isOpenInt1 = false;

}

}

}

}

}

time1CheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

if(arg0.isChecked()){

isOpenInt1 = true;

}else{

isOpenInt1 = false;

}

}

});

setTimeButton1.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {

c.setTimeInMillis(System.currentTimeMillis());

int mHour=c.get(Calendar.HOUR_OF_DAY);

int mMinute=c.get(Calendar.MINUTE);

int mDay=c.get(Calendar.DAY_OF_WEEK);

new TimePickerDialog(context,

new TimePickerDialog.OnTimeSetListener()

{

public void onTimeSet(TimePicker view,int hourOfDay,

int minute)

{

c.setTimeInMillis(System.currentTimeMillis());

c.set(Calendar.HOUR_OF_DAY,hourOfDay);

c.set(Calendar.MINUTE,minute);

c.set(Calendar.SECOND,0);

c.set(Calendar.MILLISECOND,0);

tmpS1=format(hourOfDay)+":"+format(minute);

time1TextView.setText(tmpS1);

Toast.makeText(context,

"设置闹钟时间为" + tmpS1,

Toast.LENGTH_SHORT).show();

}

},mHour,mMinute,true).show();

}

});

repeatButton1.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {

// TODO Auto-generated method stub

repeatString1 = "";

new AlertDialog.Builder(context)

.setTitle(R.string.alert_dialog_multi_choice)

.setMultiChoiceItems(R.array.select_dialog_items,

new boolean[]{false, false, false, false, false, false, false},

new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int whichButton,

boolean isChecked) {

/* User clicked on a check box do some stuff */

switch(whichButton){

case 0: if(isChecked){

repeatArray1[0] = 1;

}else{

repeatArray1[0] = 0;

}

break;

case 1:if(isChecked){

repeatArray1[1] = 1;

}else{

repeatArray1[1] = 0;

}

break;

case 2:if(isChecked){

repeatArray1[2] = 1;

}else{

repeatArray1[2] = 0;

}

break;

case 3:if(isChecked){

repeatArray1[3] = 1;

}else{

repeatArray1[3] = 0;

}

break;

case 4:if(isChecked){

repeatArray1[4] = 1;

}else{

repeatArray1[4] = 0;

}

break;

case 5:if(isChecked){

repeatArray1[5] = 1;

}else{

repeatArray1[5] = 0;

}

break;

case 6:if(isChecked){

repeatArray1[6] = 1;

}else{

repeatArray1[6] = 0;

}

break;

default: break;

}

}

})

.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

if(repeatArray1[0] == 1){

repeatString1 += "周一"+",";

repeat_1_1 = true;

}else{

repeat_1_1 = false;

}

if(repeatArray1[1] == 1){

repeatString1 += "周二"+",";

repeat_1_2 = true;

}else{

repeat_1_2 = false;

}

if(repeatArray1[2] == 1){

repeatString1 += "周三"+",";

repeat_1_3 = true;

}else{

repeat_1_3 = false;

}

if(repeatArray1[3] == 1){

repeatString1 += "周四"+",";

repeat_1_4 = true;

}else{

repeat_1_4 = false;

}

if(repeatArray1[4] == 1){

repeatString1 += "周五"+",";

repeat_1_5 = true;

}else{

repeat_1_5 = false;

}

if(repeatArray1[5] == 1){

repeatString1 += "周六"+",";

repeat_1_6 = true;

}else{

repeat_1_6 = false;

}

if(repeatArray1[6] == 1){

repeatString1 += "周日";

repeat_1_7 = true;

}else{

repeat_1_7 = false;

}

if(!(repeat_1_1 || repeat_1_2 || repeat_1_3 || repeat_1_4 ||

repeat_1_5 || repeat_1_6 || repeat_1_7)){

repeatString1 = ("无重复");

}

repeat1TextView.setText(repeatString1);

for(int i=0;i<repeatArray1.length;i++){

repeatArray1[i] = 0;

}

}

})

.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked No so do some stuff */

}

})

.show();

}

});

//show AlertDialog

new AlertDialog.Builder(context)

.setIcon(R.drawable.alarm_dialog)

.setTitle(R.string.alarm_dialog_title)

.setView(alarm1View)

.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

if(isOpenInt1){

isOpentime1 = "开";

Intent intent1 = new Intent(context, CallAlarm.class);

intent1.putExtra("RESULT", "alarm1");

PendingIntent sender=PendingIntent.getBroadcast(

context,0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am;

am = (AlarmManager)activity.getSystemService(context.ALARM_SERVICE);

int nowDay = Contants.getNowWeek();

int setDay = 0;

if(repeatString1.equals("目前无设置"))

{

if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);

}

else{

Toast.makeText(context, R.string.not_time_right, Toast.LENGTH_SHORT);

}

}

if(!(repeatString1.equals("目前无设置"))){

String[] setStr = repeatString1.split(",");

int[] dayOfNum = Contants.getDayOfNum(setStr);

setDay = Contants.getResultDifferDay(dayOfNum, nowDay);

int differDay = Contants.compareDayNowToNext(nowDay, setDay);

if(differDay == 0){

if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);

}else{

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(7),sender);

}

}else{

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(differDay),sender);

}

}

}else{

isOpentime1 = "关";

Intent intent = new Intent(context, CallAlarm.class);

PendingIntent sender=PendingIntent.getBroadcast(

context,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am;

am =(AlarmManager)activity.getSystemService(context.ALARM_SERVICE);

am.cancel(sender);

Toast.makeText(context,R.string.alarm_delete1,

Toast.LENGTH_SHORT).show();

}

String[] temStr = new String[7];

temStr[0] = tmpS1;

temStr[1] = repeatString1;

temStr[2] = isOpentime1;

dbHelper.updateAlarmColock(1+"", temStr);

refreshDBHelper();www.2cto.com

}

})

.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

refreshDBHelper();

}

})

.create().show();

Log.v("wangxianming", "this is click Item1");

}

else if(position == 1){

LayoutInflater factory = LayoutInflater.from(context);

final View alarm1View = factory.inflate(R.layout.time_repeat_dialog, null);

time2TextView = (TextView)alarm1View.findViewById(R.id.text);

setTimeButton2 = (Button)alarm1View.findViewById(R.id.mButton);

repeat2TextView = (TextView)alarm1View.findViewById(R.id.repeattext);

repeatButton2 = (Button)alarm1View.findViewById(R.id.repeatButton);

time2CheckBox = (CheckBox)alarm1View.findViewById(R.id.isopen_check);

String isOpen2 = "关";

if(count==2){

for(int i=0;i<count;i++){

if(i == 1){

cursor.moveToPosition(i);

if(cursor.getString(1)==null){

time2TextView.setText(tmpS2);

}else{

time2TextView.setText(cursor.getString(1));

tmpS2 = cursor.getString(1);

}

if((cursor.getString(2))==null){

repeat2TextView.setText(repeatString2);

}else{

repeat2TextView.setText(cursor.getString(2));

repeatString2 = cursor.getString(2);

}

isOpen2 = cursor.getString(3);

if(isOpen2!=null){

if(isOpen2.equals("开")){

time2CheckBox.setChecked(true);

isOpenInt2 = true;

}else{

time2CheckBox.setChecked(false);

isOpenInt2 = false;

}

}

}

}

}

time2CheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

if(arg0.isChecked()){

isOpenInt2 = true;

}else{

isOpenInt2 = false;

}

}

});

setTimeButton2.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {

// TODO Auto-generated method stub

c.setTimeInMillis(System.currentTimeMillis());

int mHour=c.get(Calendar.HOUR_OF_DAY);

int mMinute=c.get(Calendar.MINUTE);

int mDay=c.get(Calendar.DAY_OF_WEEK);

new TimePickerDialog(context,

new TimePickerDialog.OnTimeSetListener()

{

public void onTimeSet(TimePicker view,int hourOfDay,

int minute)

{

c.setTimeInMillis(System.currentTimeMillis());

c.set(Calendar.HOUR_OF_DAY,hourOfDay);

c.set(Calendar.MINUTE,minute);

c.set(Calendar.SECOND,0);

c.set(Calendar.MILLISECOND,0);

tmpS2=format(hourOfDay)+":"+format(minute);

time2TextView.setText(tmpS2);

Toast.makeText(context,R.string.alarm_set2+tmpS2,Toast.LENGTH_SHORT).show();

}

},mHour,mMinute,true).show();

}

});

repeatButton2.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {

repeatString2 = "";

new AlertDialog.Builder(context)

.setTitle(R.string.alert_dialog_multi_choice)

.setMultiChoiceItems(R.array.select_dialog_items,

new boolean[]{false, false, false, false, false, false, false},

new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int whichButton,

boolean isChecked) {

/* User clicked on a check box do some stuff */

switch(whichButton){

case 0: if(isChecked){

repeatArray2[0] = 1;

}else{

repeatArray2[0] = 0;

}

break;

case 1:if(isChecked){

repeatArray2[1] = 1;

}else{

repeatArray2[1] = 0;

}

break;

case 2:if(isChecked){

repeatArray2[2] = 1;

}else{

repeatArray2[2] = 0;

}

break;

case 3:if(isChecked){

repeatArray2[3] = 1;

}else{

repeatArray2[3] = 0;

}

break;

case 4:if(isChecked){

repeatArray2[4] = 1;

}else{

repeatArray2[4] = 0;

}

break;

case 5:if(isChecked){

repeatArray2[5] = 1;

}else{

repeatArray2[5] = 0;

}

break;

case 6:if(isChecked){

repeatArray2[6] = 1;

}else{

repeatArray2[6] = 0;

}

break;

default: break;

}

}

})

.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

if(repeatArray2[0] == 1){

repeatString2 += "周一"+",";

repeat_2_1 = true;

}else{

repeat_2_1 = false;

}

if(repeatArray2[1] == 1){

repeatString2 += "周二"+",";

repeat_2_2 = true;

}else{

repeat_2_2 = false;

}

if(repeatArray2[2] == 1){

repeatString2 += "周三"+",";

repeat_2_3 = true;

}else{

repeat_2_3 = false;

}

if(repeatArray2[3] == 1){

repeatString2 += "周四"+",";

repeat_2_4 = true;

}else{

repeat_2_4 = false;

}

if(repeatArray2[4] == 1){

repeatString2 += "周五"+",";

repeat_2_5 = true;

}else{

repeat_1_5 = false;

}

if(repeatArray2[5] == 1){

repeatString2 += "周六"+",";

repeat_2_6 = true;

}else{

repeat_2_6 = false;

}

if(repeatArray2[6] == 1){

repeatString2 += "周日";

repeat_2_7 = true;

}else{

repeat_2_7 = false;

}

if(!(repeat_2_1 || repeat_2_2 || repeat_2_3 || repeat_2_4 ||

repeat_2_5 || repeat_2_6 || repeat_2_7)){

repeatString2 = ("无设置");

}

repeat2TextView.setText(repeatString2);

for(int i=0;i<repeatArray1.length;i++){

repeatArray2[i] = 0;

}

}

})

.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked No so do some stuff */

}

})

.show();

}

});

// show Dialog

new AlertDialog.Builder(context)

.setIcon(R.drawable.alarm_dialog)

.setTitle(R.string.alarm_dialog_title)

.setView(alarm1View)

.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

if(isOpenInt2){

isOpentime2 = "开";

Intent intent2 = new Intent(context, CallAlarm.class);

intent2.putExtra("RESULT", "alarm2");

PendingIntent sender=PendingIntent.getBroadcast(

context,1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am;

am = (AlarmManager)activity.getSystemService(context.ALARM_SERVICE);

int nowDay = Contants.getNowWeek();

int setDay = 0;

if(repeatString2.equals("目前无设置"))

{

if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);

}

else{

Toast.makeText(context, R.string.not_time_right, Toast.LENGTH_SHORT);

}

}

if(!(repeatString2.equals("目前无设置"))){

String[] setStr = repeatString2.split(",");

int[] dayOfNum = Contants.getDayOfNum(setStr);

setDay = Contants.getResultDifferDay(dayOfNum, nowDay);

int differDay = Contants.compareDayNowToNext(nowDay, setDay);

if(differDay == 0){

if(Contants.differSetTimeAndNowTime(c.getTimeInMillis())){

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),sender);

}else{

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(7),sender);

}

}else{

am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis() + Contants.getDifferMillis(differDay),sender);

}

}

}else{

isOpentime2 = "关";

Intent intent = new Intent(context, CallAlarm.class);

intent.putExtra("RESULT", "cancel");

PendingIntent sender=PendingIntent.getBroadcast(

context,1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am;

am =(AlarmManager)activity.getSystemService(context.ALARM_SERVICE);

am.cancel(sender);

Toast.makeText(context,R.string.alarm_delete2,

Toast.LENGTH_SHORT).show();

}

String[] temStr = new String[7];

temStr[0] = tmpS2;

temStr[1] = repeatString2;

temStr[2] = isOpentime2;

dbHelper.updateAlarmColock(2+"", temStr);

refreshDBHelper();

}

})

.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

refreshDBHelper();

}

})

.create().show();

}

cursor.close();

dbHelper.close();

}

private String format(int x)

{

String s=""+x;

if(s.length()==1) s="0"+s;

return s;

}

}

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

package com.cn.daming;

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;

public class AlarmClockAdapter extends BaseAdapter{

private LayoutInflater layoutInflater;

private Context context;

private List<String> alarm_ids;

private List<String> alarm_times;

private List<String> alarm_repeats;

private List<String> alarm_isopens;

private List<String> alarm_kinds;

ZuJian zuJian;

public AlarmClockAdapter(Context context,List<String> ids,List<String> times,List<String> repeats,

List<String> isopens,List<String> kinds) {

this.context = context;

this.alarm_ids = ids;

this.alarm_times = times;

this.alarm_repeats = repeats;

this.alarm_isopens = isopens;

this.alarm_kinds = kinds;

this.layoutInflater = LayoutInflater.from(context);

}

public int getCount() {

return alarm_times.size();

}

public Object getItem(int position) {

return alarm_times.get(position);

}

public long getItemId(int position) {

return position;

}

public View getView(int position, View convertView, ViewGroup parent) {

zuJian =new ZuJian();

if (convertView == null) {

convertView = layoutInflater.inflate(R.layout.alarm_clock, null);

zuJian.alarmTitle = (TextView) convertView.findViewById(R.id.alarm_title);

zuJian.alarmTimeView = (TextView) convertView.findViewById(R.id.alarm_time);

zuJian.repeatAlarmView = (TextView) convertView.findViewById(R.id.alarm_repeat_time);

zuJian.showISOpenView = (ImageView) convertView.findViewById(R.id.show_open_close);

convertView.setTag(zuJian);

} else {

zuJian = (ZuJian) convertView.getTag();

}

if (alarm_kinds.get(position) == null || alarm_kinds.get(position) == "") {

if(position == 0){

zuJian.alarmTitle.setText("大明闹钟一");

}

if(position == 1){

zuJian.alarmTitle.setText("大明闹钟二");

}

} else {

zuJian.alarmTitle.setText(alarm_kinds.get(position));

}

if (alarm_times.get(position) == null || alarm_times.get(position) == "") {

zuJian.alarmTimeView.setText("目前无设置");

} else {

zuJian.alarmTimeView.setText(alarm_times.get(position));

}

if (alarm_repeats.get(position) == null || alarm_repeats.get(position) == "") {

zuJian.repeatAlarmView.setText("目前无设置");

} else {

zuJian.repeatAlarmView.setText(alarm_repeats.get(position));

}

if (alarm_isopens.get(position) == null || alarm_isopens.get(position) == "") {

((ImageView) zuJian.showISOpenView).setBackgroundResource(R.drawable.alarm_dialog);

} else {

if(alarm_isopens.get(position).equals("开")){

((ImageView) zuJian.showISOpenView).setBackgroundResource(R.drawable.clock);

}

if(alarm_isopens.get(position).equals("关")){

((ImageView) zuJian.showISOpenView).setBackgroundResource(R.drawable.alarm_dialog);

}

}

return convertView;

}

final class ZuJian {

public TextView alarmTitle;

public TextView alarmTimeView;

public TextView repeatAlarmView;

public ImageView showISOpenView;

}

}

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

package com.cn.daming;

/* import class */

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

/*AlarmReceiver */

public class CallAlarm extends BroadcastReceiver

{

@Override

public void onReceive(final Context context, Intent intent)

{

String getStr = intent.getExtras().getString("RESULT");

Log.v("wangxianming", "RESULT = "+getStr);

Intent alaramIntent = new Intent(context, AlarmAgainSetting.class);

Bundle bundleRet = new Bundle();

bundleRet.putString("STR_RESULT", getStr);

alaramIntent.putExtras(bundleRet);

alaramIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(alaramIntent);

}

}

五、Contants。java类,工具类:

package com.cn.daming;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Calendar;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

import java.util.Random;

public class Contants {

//repeated day of week define

public static Map<String, Integer> mapWeek = new HashMap<String,Integer>();

public static void addMapWeek(){

mapWeek.put("周一", 1);

mapWeek.put("周二", 2);

mapWeek.put("周三", 3);

mapWeek.put("周四", 4);

mapWeek.put("周五", 5);

mapWeek.put("周六", 6);

mapWeek.put("周日", 7);

mapWeek.put("无重复", 0);

}

public static int getMapWeek(String str){

Contants.addMapWeek();

int dayOfMapWeek = 0;

if(str != null){

dayOfMapWeek = mapWeek.get(str);

}

return dayOfMapWeek;

}

public static String[] getDatetimeString(){

Date date = new Date();

String[] tempStr = new String[2];

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String str = format.format(date);

tempStr[0] = str.substring(0, 10);

tempStr[1] = str.substring(11, str.length());

return tempStr;

}

public static long getNowTimeMinuties()

{

return System.currentTimeMillis();

}

public static boolean differSetTimeAndNowTime(long setTime)

{

if(setTime >= getNowTimeMinuties()){

return true;

}else{

return false;

}

}

public static long getDifferMillis(int differDays)

{

return differDays * 24 * 60 * 60 * 1000;

}

//compare nowDay to nextDay

public static int compareDayNowToNext(int nowDay,int nextDay){

if(nextDay > nowDay){

return (nextDay-nowDay);

}else if(nextDay == nowDay){

return 0;

}else{

return (7-(nowDay-nextDay));

}

}

//turn the nowday to China`s day of Week

public static Map<String, Integer> nowWeek = new HashMap<String,Integer>();

public static void addNowWeek()

{

nowWeek.put("1", 7);

nowWeek.put("2", 1);

nowWeek.put("3", 2);

nowWeek.put("4", 3);

nowWeek.put("5", 4);

nowWeek.put("6", 5);

nowWeek.put("7", 6);

}

public static int getNowWeek()

{

Calendar nowCal = Calendar.getInstance();

Date nowDate = new Date(System.currentTimeMillis());

nowCal.setTime(nowDate);

int nowNum = nowCal.get(nowCal.DAY_OF_WEEK);

String nowNumStr = String.valueOf(nowNum);

Contants.addNowWeek();

int nowDayOfWeek = 0;

if(nowNumStr != null){

nowDayOfWeek = nowWeek.get(nowNumStr);

}

return nowDayOfWeek;

}

public static int getSetDay(String str)

{

if(str.equals("周一")){

return 1;

}

if(str.equals("周二")){

return 2;

}

if(str.equals("周三")){

return 3;

}

if(str.equals("周四")){

return 4;

}

if(str.equals("周五")){

return 5;

}

if(str.equals("周六")){

return 6;

}

if(str.equals("周日")){

return 7;

}

return 0;

}

public static int[] getDayOfNum(String[] str)

{

int[] dayOfInt = new int[str.length];

for(int i=0;i<str.length;i++){

dayOfInt[i] = getSetDay(str[i]);

}

return dayOfInt;

}

public static int getResultDifferDay(int[] in,int nowDay)

{

int result = 0;

for(int i=0;i<in.length;i++){

if(in[i] >= nowDay){

result = in[i];

break;

}

}

if(result == 0){

result = in[0];

}

return result;

}

public static int getResultDifferDay1(int[] in,int nowDay)

{

int result = 0;

for(int i=0;i<in.length;i++){

if(in[i] > nowDay){

result = in[i];

break;

}

}

if(result == 0){

result = in[0];

}

return result;

}

}

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

package com.cn.daming;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {

private final static String DATABASE_NAME = "alarm_db";

private final static int DATABASE_VERSION = 1;

private final static String ALARM_COLOCK_TABLE = "alarmcolock";

public final static String ALARM_ID = "_id";

public final static String ALARM_TIME = "alarmtime"; //alarm time

public final static String ALARM_REPEAT = "alarmrepeat";//alarm repeate is or not

public final static String ALARM_ISOPEN = "alarmisopen";//alarm open is 0r not

public final static String ALARM_KIND = "alarmkind"; //alarm is kind(1 is shuangseqiu ,2 is da le tou )

public final static String ALARM_SPARE1 = "alarmspare1";//spare1

public final static String ALARM_SPARE2 = "alarmspare2";//spare2

public final static String ALARM_SPARE3 = "alarmspare3";//spare3

public DataBaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

Stringsql = "create table "+ALARM_COLOCK_TABLE+" ("

+ALARM_ID+" integer primary key autoincrement, "

+ALARM_TIME+" text, "

+ALARM_REPEAT+" text, "

+ALARM_ISOPEN+" text, "

+ALARM_KIND+" text, "

+ALARM_SPARE1+" text, "

+ALARM_SPARE2+" text, "

+ALARM_SPARE3+" text )";

db.execSQL(sql);

}

@Override

public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

String sql = "drop table if exists "+ALARM_COLOCK_TABLE;

db.execSQL(sql);

}

//the action in AlarmColock table

public long insertAlarmColock(String[] strArray)

{

SQLiteDatabase db = this.getWritableDatabase();

ContentValues conv = new ContentValues();

conv.put(ALARM_TIME, strArray[0]);

conv.put(ALARM_REPEAT, strArray[1]);

conv.put(ALARM_ISOPEN, strArray[2]);

conv.put(ALARM_KIND, strArray[3]);

return db.insert(ALARM_COLOCK_TABLE, null, conv);

}

public Cursor selectAlarmColock(){

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(ALARM_COLOCK_TABLE, null, null, null, null, null, null);

return cursor;

}

public Cursor getAlarmColock(String id){

SQLiteDatabase db = this.getReadableDatabase();

String where = ALARM_ID+"=?";

String[] whereValues = {id};

Cursor cursor = db.query(ALARM_COLOCK_TABLE, null, where, whereValues, null, null, null);

return cursor;

}

public void deleteAlarmColock(String id){

SQLiteDatabase db = this.getWritableDatabase();

String where = ALARM_ID+"=?";

String[] whereValues = {id};

db.delete(ALARM_COLOCK_TABLE, where, whereValues);

}

public int updateAlarmColock(String id,String[] strArray){

SQLiteDatabase db = this.getWritableDatabase();

String where = ALARM_ID+"=?";

String[] whereValues = {id};

ContentValues cv = new ContentValues();

cv.put(ALARM_TIME, strArray[0]);

cv.put(ALARM_REPEAT, strArray[1]);

cv.put(ALARM_ISOPEN, strArray[2]);

cv.put(ALARM_KIND, strArray[3]);

Log.v("wangxianming", "cv : "+cv.get("ALARM_TIME")+cv.get("ALARM_REPEAT")+cv.get("ALARM_ISOPEN")+cv.get("ALARM_KIND"));

return db.update(ALARM_COLOCK_TABLE, cv, where, whereValues);

}

}

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

package com.cn.daming;

import android.app.Activity;

import android.app.AlarmManager;

import android.app.AlertDialog;

import android.app.PendingIntent;

import android.content.DialogInterface;

import android.content.Intent;

import android.database.Cursor;

import android.os.Bundle;

import android.widget.Toast;

public class AlarmAgainSetting extends Activity{

private DataBaseHelper dbHelper;

private Cursor cursor;

Intent getAlarmAgainSetting;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

getAlarmAgainSetting = AlarmAgainSetting.this.getIntent();

String getAlarmAgainSettingStr = getAlarmAgainSetting.getExtras().getString("STR_RESULT");

dbHelper = new DataBaseHelper(this);

if(getAlarmAgainSettingStr.equals("alarm1")){

Intent againIntent = new Intent(AlarmAgainSetting.this, CallAlarm.class);

againIntent.putExtra("RESULT", "alarm1");

PendingIntent sender=PendingIntent.getBroadcast(

AlarmAgainSetting.this,0, againIntent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am;

am = (AlarmManager)getSystemService(ALARM_SERVICE);

int nowDay = Contants.getNowWeek();

int setDay = 0;

cursor = dbHelper.selectAlarmColock();

String str1 = null;

int count1 = cursor.getCount();

if(count1 > 0){

for(int i=0;i<count1;i++){

if(i == 0){

cursor.moveToPosition(i);

str1 = cursor.getString(2);

}

}

}

if(!(str1.equals("目前无设置"))){

String[] setStr = str1.split(",");

int[] dayOfNum = Contants.getDayOfNum(setStr);

setDay = Contants.getResultDifferDay1(dayOfNum, nowDay);

int differDay = Contants.compareDayNowToNext(nowDay, setDay);

if(differDay == 0){

am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(7),sender);

}else{

am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(differDay),sender);

}

}

}

if(getAlarmAgainSettingStr.equals("alarm2")){

/* allAlarm.class */

Intent againIntent = new Intent(AlarmAgainSetting.this, CallAlarm.class);

againIntent.putExtra("RESULT", "alarm2");

/* PendingIntent */

PendingIntent sender=PendingIntent.getBroadcast(

AlarmAgainSetting.this,1, againIntent, PendingIntent.FLAG_UPDATE_CURRENT);

/* AlarmManager.RTC_WAKEUP

* */

AlarmManager am;

am = (AlarmManager)getSystemService(ALARM_SERVICE);

int nowDay = Contants.getNowWeek();

int setDay = 0;

cursor = dbHelper.selectAlarmColock();

String str2 = null;

int count2 = cursor.getCount();

if(count2 > 0){

for(int i=0;i<count2;i++){

if(i == 1){

cursor.moveToPosition(i);

str2 = cursor.getString(2);

}

}

}

if(!(str2.equals("目前无设置"))){

String[] setStr = str2.split(",");

int[] dayOfNum = Contants.getDayOfNum(setStr);

setDay = Contants.getResultDifferDay1(dayOfNum, nowDay);

int differDay = Contants.compareDayNowToNext(nowDay, setDay);

if(differDay == 0){

am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(7),sender);

}else{

am.set(AlarmManager.RTC_WAKEUP,Contants.getNowTimeMinuties() + Contants.getDifferMillis(differDay),sender);

}

}

}

cursor.close();

dbHelper.close();

Toast.makeText(this, R.string.alarm_time_come, Toast.LENGTH_SHORT).show();

new AlertDialog.Builder(this)

.setIcon(R.drawable.clock)

.setTitle("大明闹钟时间到了!!")

.setMessage("你使用大明闹钟时间到了!!!")

.setPositiveButton("确定",

new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int whichButton)

{

/*AlarmAlertActivity */

finish();

}

}).setNegativeButton("取消",

new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int whichButton)

{

/*AlarmAlertActivity */

finish();

}

}).show();

}

}

布局文件

一、main。xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/alarm_bg"

>

<LinearLayout android:id="@+id/box"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

</LinearLayout>

</LinearLayout>

二、alarm_clock.xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="320dip"

android:layout_height="wrap_content"

android:orientation="vertical">

<TextView

android:id="@+id/alarm_title"

android:layout_width="320dip"

android:layout_height="wrap_content"

android:textColor="#FF000000"

android:textSize="23px"

android:layout_margin="5px"

/>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="320dip"

android:layout_height="wrap_content"

android:orientation="horizontal"

>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="250dip"

android:layout_height="wrap_content"

android:orientation="horizontal">

<TextView

android:id="@+id/alarm_image"

android:scaleType="centerCrop"

android:layout_width="20dip"

android:layout_height="wrap_content"

android:layout_margin="8px"

/>

<LinearLayout

android:layout_width="222dip"

android:layout_height="wrap_content"

android:orientation="vertical"

>

<TextView

android:id="@+id/alarm_time"

android:layout_width="222dip"

android:layout_height="wrap_content"

android:paddingLeft="8dip"

android:layout_marginBottom="3dip"

android:textColor="#FF000000"

android:textSize="18px"

/>

<TextView

android:id="@+id/alarm_repeat_time"

android:layout_width="222dip"

android:layout_height="wrap_content"

android:paddingLeft="8dip"

android:layout_gravity="left"

android:textColor="#FF000000"

android:textSize="10px"

/>

</LinearLayout>

</LinearLayout>

<View

android:layout_width="2dip"

android:layout_height="match_parent"

android:textColor="#FF000000"

/>

<ImageView

android:id="@+id/show_open_close"

android:layout_width="38dip"

android:layout_height="wrap_content"

android:paddingTop="35dip"

android:scaleType="centerCrop"

android:layout_gravity="bottom"

android:gravity="bottom"

android:textColor="#FF000000"

/>

<TextView

android:layout_width="30dip"

android:layout_height="wrap_content"

/>

</LinearLayout>

</LinearLayout>

三、alarm_listview.xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:id="@+id/note_textview"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal|center_vertical"

android:gravity="center_horizontal|center_vertical"

android:paddingLeft="10dip"

android:text="@string/alarm_list"

android:textColor="#FF000000"

android:textSize="30dip"

android:textStyle="bold"

android:paddingTop="6dip"

android:paddingBottom="6dip"

/>

<ListView

android:id="@+id/alarm_list"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

</LinearLayout>

四、time_repeat_dialog.xml布局文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

>

<AbsoluteLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/layout1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/white"

>

<DigitalClock

android:id="@+id/dClock"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="40sp"

android:textColor="@drawable/blue"

android:layout_x="58px"

android:layout_y="5px">

</DigitalClock>

<TextView

android:id="@+id/isopen_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_isopen_text"

android:textSize="16px"

android:textColor="@drawable/black"

android:layout_x="10px"

android:layout_y="64px"

>

</TextView>

<CheckBox

android:id="@+id/isopen_check"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="20sp"

android:textColor="@drawable/black"

android:layout_x="160px"

android:layout_y="54px"

/>

<TextView

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_title1"

android:textSize="20sp"

android:textColor="@drawable/black"

android:layout_x="10px"

android:layout_y="104px"

>

</TextView>

<Button

android:id="@+id/mButton"

android:layout_width="108px"

android:layout_height="40px"

android:text="@string/str_button1"

android:textColor="@drawable/black"

android:textSize="18sp"

android:layout_x="160px"

android:layout_y="102px"

>

</Button>

<TextView

android:id="@+id/repeattext"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/str_default"

android:textSize="16sp"

android:textColor="@drawable/red"

android:layout_x="10px"

android:layout_y="145px"

>

</TextView>

<Button

android:id="@+id/repeatButton"

android:layout_width="108px"

android:layout_height="40px"

android:layout_x="160px"

android:layout_y="182px"

android:textColor="@drawable/black"

android:textSize="18sp"

android:text="@string/repeat_button"

/>

</AbsoluteLayout>

</LinearLayout>

value目录下的文件

一、arrays.xml文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string-array name="select_dialog_items">

<item>周一</item>

<item>周二</item>

<item>周三</item>

<item>周四</item>

<item>周五</item>

<item>周六</item>

<item>周日</item>

</string-array>

</resources>

二、color.xml文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

<drawable name="white">#FFFFFFFF</drawable>

<drawable name="black">#000000</drawable>

<drawable name="blue">#0000FF</drawable>

<drawable name="red">#FF0000</drawable>

<drawable name="dackgray">#666666</drawable>

<drawable name="yellow">#FFFFFF00</drawable>

</resources>

三、string.xml文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello">Hello World, MainActivity!</string>

<string name="app_name">AlarmApp</string>

<string name="alarm_list">大明闹钟</string>

<string name="not_dbcursor_values">数据库中没有值,请设置你的闹钟。。。。</string>

<string name="str_isopen_text">是否开启闹钟</string>

<string name="str_title1">设置闹钟</string>

<string name="str_button1">设置时间</string>

<string name="str_default">目前无设置</string>

<string name="repeat_button">设置重复</string>

<string name="alert_dialog_ok">确定</string>

<string name="alert_dialog_cancel">取消</string>

<string name="alarm_dialog_title">闹钟设置</string>

<string name="alert_dialog_multi_choice">重复闹钟设置</string>

<string name="alarm_time_come">您使用大明闹钟设置的闹钟时间到了!!!</string>

<string name="alarm_delete1">大明闹钟一删除成功!</string>

<string name="alarm_delete2">大明闹钟二删除成功!</string>

<string name="alarm_set2">大明闹钟二时间为:</string>

<string name="not_time_right">闹钟时间设置不对,请选择周几重复!</string>

</resources>

<strong><span style="font-size:24px;color:#660000;">注意:广播一定要在Manifest中注册</span></strong>

AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.cn.daming"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">

<!--

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港