您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > Android开发闹钟(详细开发步骤)
一、MainActivity中的代码:packagecom.cn.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();@OverridepublicvoidonCreate(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);editor.commit();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,目前无设置);editor.commit();}});}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);editor.commit();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(目前无设置);//SharedPrefe
本文标题:Android开发闹钟(详细开发步骤)
链接地址:https://www.777doc.com/doc-4376470 .html