android闹钟Word文档下载推荐.docx
《android闹钟Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《android闹钟Word文档下载推荐.docx(24页珍藏版)》请在冰豆网上搜索。
程序欢迎界面:
点击设置闹钟界面:
点击闹钟设置中的设置后的界面:
闹钟时间到了弹出dialog:
设置重复想起闹钟后的界面:
点击返回键弹出的提示:
下面请看代码:
一、MainActivity中的代码:
package.daming;
importjava.util.Calendar;
importandroid.app.Activity;
importandroid.app.AlarmManager;
importandroid.app.AlertDialog;
importandroid.app.PendingIntent;
importandroid.app.TimePickerDialog;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.content.SharedPreferences;
importandroid.os.Bundle;
importandroid.view.KeyEvent;
importandroid.view.LayoutInflater;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.EditText;
importandroid.widget.TextView;
importandroid.widget.TimePicker;
importandroid.widget.Toast;
publicclassMainActivityextendsActivity{
TextViewsetTime1;
TextViewsetTime2;
TextViewsetTime3;
ButtonmButton1;
ButtonmButton2;
ButtonmButton3;
ButtonmButton4;
ButtonmButton5;
ButtonmButton6;
Stringtime1String=null;
Stringtime2String=null;
Stringtime3String=null;
StringdefalutString="
目前无设置"
;
AlertDialogbuilder=null;
Calendarc=Calendar.getInstance();
@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得活动的Preferences对象
SharedPreferencessettings=getPreferences(Activity.MODE_PRIVATE);
time1String=settings.getString("
TIME1"
defalutString);
time2String=settings.getString("
TIME2"
time3String=settings.getString("
TIME3"
InitButton1();
InitButton2();
InitButton3();
InitButton4();
InitButton5();
InitButton6();
setTime1.setText(time1String);
setTime3.setText(time2String);
setTime2.setText(time3String);
}
publicvoidInitButton1()
setTime1=(TextView)findViewById(R.id.setTime1);
mButton1=(Button)findViewById(R.id.mButton1);
mButton1.setOnClickListener(newView.OnClickListener()
publicvoidonClick(Viewv)
c.setTimeInMillis(System.currentTimeMillis());
intmHour=c.get(Calendar.HOUR_OF_DAY);
intmMinute=c.get(Calendar.MINUTE);
newTimePickerDialog(MainActivity.this,
newTimePickerDialog.OnTimeSetListener()
{
publicvoidonTimeSet(TimePickerview,inthourOfDay,
intminute)
c.set(Calendar.HOUR_OF_DAY,hourOfDay);
c.set(Calendar.MINUTE,minute);
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intentintent=newIntent(MainActivity.this,CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
MainActivity.this,0,intent,0);
AlarmManageram;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),
sender
);
StringtmpS=format(hourOfDay)+"
:
"
+format(minute);
setTime1.setText(tmpS);
//SharedPreferences保存数据,并提交
SharedPreferencestime1Share=getPreferences(0);
SharedPreferences.Editoreditor=time1Share.edit();
editor.putString("
tmpS);
mit();
Toast.makeText(MainActivity.this,"
设置大明闹钟时间为"
+tmpS,
Toast.LENGTH_SHORT)
.show();
}
},mHour,mMinute,true).show();
});
publicvoidInitButton2()
mButton2=(Button)findViewById(R.id.mButton2);
mButton2.setOnClickListener(newView.OnClickListener()
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
大明闹钟时间删除"
Toast.LENGTH_SHORT).show();
setTime1.setText("
);
"
publicvoidInitButton3()
setTime3=(TextView)findViewById(R.id.setTime5);
mButton3=(Button)findViewById(R.id.mButton5);
mButton3.setOnClickListener(newView.OnClickListener()
MainActivity.this,1,intent,0);
setTime3.setText(tmpS);
SharedPreferencestime2Share=getPreferences
(1);
SharedPreferences.Editoreditor=time2Share.edit();
publicvoidInitButton4()
mButton4=(Button)findViewById(R.id.mButton6);
mButton4.setOnClickListener(newView.OnClickListener()
setTime3.setText("
publicvoidInitButton5()
setTime2=(TextView)findViewById(R.id.setTime2);
LayoutInflaterfactory=LayoutInflater.from(this);
finalViewsetView=factory.inflate(R.layout.timeset,null);
finalTimePickertPicker=(TimePicker)setView
.findViewById(R.id.tPicker);
tPicker.setIs24HourView(true);
finalAlertDialogdi=newAlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.clock)
.setTitle("
设置"
)
.setView(setView)
.setPositiveButton("
确定"
newDialogInterface.OnClickListener()
publicvoidonClick(DialogInterfacedialog,intwhich)
EditTexted=(EditText)setView.findViewById(R.id.mEdit);
inttimes=Integer.parseInt(ed.getText().toString())
*1000;
c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour());
c.set(Calendar.MINUTE,tPicker.getCurrentMinute());
Intentintent=newIntent(MainActivity.this,
CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
am.setRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),times,sender);
StringtmpS=format(tPicker.getCurrentHour())+"
+
format(tPicker.getCurrentMinute());
StringsubStr="
+tmpS+
"
开始,重复间隔为"
+times/1000+"
秒"
setTime2.setText("
//SharedPreferences保存数据,并提交
SharedPreferencestime3Share=getPreferences
(2);
SharedPreferences.Editoreditor=time3Share.edit();
subStr);
设置大明闹钟为"
})
.setNegativeButton("
取消"
}).create();
mButton5=(Button)findViewById(R.id.mButton3);
mButton5.setOnClickListener(newView.OnClickListener()
tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
tPicker.setCurrentMinute(c.get(Calendar.MINUTE));
di.show();
publicvoidInitButton6()
mButton6=(Button)findViewById(R.id.mButton4);
mButton6.setOnClickListener(newView.OnClickListener()
闹钟时间删除"
publicbooleanonKeyUp(intkeyCode,KeyEventevent){
if(keyCod