Android开发闹钟详细开发步骤.docx
《Android开发闹钟详细开发步骤.docx》由会员分享,可在线阅读,更多相关《Android开发闹钟详细开发步骤.docx(26页珍藏版)》请在冰豆网上搜索。
Android开发闹钟详细开发步骤
一、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",defalutString);
time3String=settings.getString("TIME3",defalutString);
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.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);
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("TIME1",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()
{
publicvoidonClick(Viewv)
{
Intentintent=newIntent(MainActivity.this,CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
MainActivity.this,0,intent,0);
AlarmManageram;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
Toast.makeText(MainActivity.this,"大明闹钟时间删除",
Toast.LENGTH_SHORT).show();
setTime1.setText("目前无设置");
SharedPreferencestime1Share=getPreferences(0);
SharedPreferences.Editoreditor=time1Share.edit();
editor.putString("TIME1","目前无设置");
mit();
}
});
}
publicvoidInitButton3()
{
setTime3=(TextView)findViewById(R.id.setTime5);
mButton3=(Button)findViewById(R.id.mButton5);
mButton3.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.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);
Intentintent=newIntent(MainActivity.this,CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
MainActivity.this,1,intent,0);
AlarmManageram;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),
sender
);
StringtmpS=format(hourOfDay)+":
"+format(minute);
setTime3.setText(tmpS);
//SharedPreferences保存数据,并提交
SharedPreferencestime2Share=getPreferences
(1);
SharedPreferences.Editoreditor=time2Share.edit();
editor.putString("TIME2",tmpS);
mit();
Toast.makeText(MainActivity.this,"设置大明闹钟时间为"+tmpS,
Toast.LENGTH_SHORT)
.show();
}
},mHour,mMinute,true).show();
}
});
}
publicvoidInitButton4()
{
mButton4=(Button)findViewById(R.id.mButton6);
mButton4.setOnClickListener(newView.OnClickListener()
{
publicvoidonClick(Viewv)
{
Intentintent=newIntent(MainActivity.this,CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
MainActivity.this,0,intent,0);
AlarmManageram;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
Toast.makeText(MainActivity.this,"大明闹钟时间删除",
Toast.LENGTH_SHORT).show();
setTime3.setText("目前无设置");
//SharedPreferences保存数据,并提交
SharedPreferencestime2Share=getPreferences
(1);
SharedPreferences.Editoreditor=time2Share.edit();
editor.putString("TIME2","目前无设置");
mit();
}
});
}
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.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY,tPicker.getCurrentHour());
c.set(Calendar.MINUTE,tPicker.getCurrentMinute());
c.set(Calendar.SECOND,0);
c.set(Calendar.MILLISECOND,0);
Intentintent=newIntent(MainActivity.this,
CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
MainActivity.this,1,intent,0);
AlarmManageram;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,
c.getTimeInMillis(),times,sender);
StringtmpS=format(tPicker.getCurrentHour())+":
"+
format(tPicker.getCurrentMinute());
StringsubStr="设置大明闹钟时间为"+tmpS+
"开始,重复间隔为"+times/1000+"秒";
setTime2.setText("设置大明闹钟时间为"+tmpS+
"开始,重复间隔为"+times/1000+"秒");
//SharedPreferences保存数据,并提交
SharedPreferencestime3Share=getPreferences
(2);
SharedPreferences.Editoreditor=time3Share.edit();
editor.putString("TIME3",subStr);
mit();
Toast.makeText(MainActivity.this,"设置大明闹钟为"+tmpS+
"开始,重复间隔为"+times/1000+"秒",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消",
newDialogInterface.OnClickListener()
{
publicvoidonClick(DialogInterfacedialog,intwhich)
{
}
}).create();
mButton5=(Button)findViewById(R.id.mButton3);
mButton5.setOnClickListener(newView.OnClickListener()
{
publicvoidonClick(Viewv)
{
c.setTimeInMillis(System.currentTimeMillis());
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()
{
publicvoidonClick(Viewv)
{
Intentintent=newIntent(MainActivity.this,CallAlarm.class);
PendingIntentsender=PendingIntent.getBroadcast(
MainActivity.this,1,intent,0);
AlarmManageram;
am=(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(sender);
Toast.makeText(MainActivity.this,"闹钟时间删除",
Toast.LENGTH_SHORT).show();
setTime2.setText("目前无设置");
//SharedPreferences保存数据,并提交
SharedPreferencestime3Share=getPreferences
(2);
SharedPreferences.Editoreditor=time3Share.edit();
editor.putString("TIME3","目前无设置");
mit();
}
});
}
@Override
publicbooleanonKeyUp(intkeyCode,KeyEventevent){
if(keyCode==KeyEvent.KEYCODE_BACK){
builder=newAlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.clock)
.setTitle("温馨提示:
")
.setMessage("您是否要退出大明闹钟程序")
.setPositiveButton("确定",
newDialogInterface.OnClickListener(){
publicvoidonClick(DialogInterfacedialog,
intwhichButton){
MainActivity.this.finish();
}
})
.setNegativeButton("取消",
newDialogInterface.OnClickListener(){
publicvoidonClick(DialogInterfacedialog,
intwhichButton){
builder.dismiss();
}
}).show();
}
returntrue;
}
privateStringformat(intx)
{
Strings=""+x;
if(s.length()==1)s="0"+s;
returns;
}
}
二、CallAlarm中的代码:
package.daming;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.BroadcastReceiver;
importandroid.os.Bundle;
publicclassCallAlarmextendsBroadcastReceiver
{
@Override
publicvoidon