




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、一、MainActivity中的代碼:package .daming;import java.util.Calendar;import android.app.Activity;import android.app.AlarmManager;import android.app.AlertDialog;import android.app.PendingIntent;import android.app.TimePickerDialog;import android.content.DialogInterface;import android.content.Intent;import and
2、roid.content.SharedPreferences;import android.os.Bundle;import android.view.KeyEvent;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.TimePicker
3、;import android.widget.Toast;public class MainActivity extends Activity TextView setTime1; TextView setTime2; TextView setTime3; Button mButton1; Button mButton2; Button mButton3; Button mButton4; Button mButton5; Button mButton6; String time1String = null; String time2String = null; String time3Str
4、ing = null; String defalutString = 目前無設置; AlertDialog builder = null; Calendar c=Calendar.getInstance(); Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); /取得活動的Preferences對象 SharedPreferences settings = getPreferences(Activit
5、y.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(time1Str
6、ing); setTime3.setText(time2String); setTime2.setText(time3String); public void InitButton1() setTime1=(TextView) findViewById(R.id.setTime1); mButton1=(Button)findViewById(R.id.mButton1); mButton1.setOnClickListener(new View.OnClickListener() public void onClick(View v) c.setTimeInMillis(System.cur
7、rentTimeMillis(); int mHour=c.get(Calendar.HOUR_OF_DAY); int mMinute=c.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() public void onTimeSet(TimePicker view,int hourOfDay, int minute) c.setTimeInMillis(System.currentTimeMillis(); c.set(Calendar.
8、HOUR_OF_DAY,hourOfDay); c.set(Calendar.MINUTE,minute); c.set(Calendar.SECOND,0); c.set(Calendar.MILLISECOND,0); Intent intent = new Intent(MainActivity.this, CallAlarm.class); PendingIntent sender=PendingIntent.getBroadcast( MainActivity.this,0, intent, 0); AlarmManager am; am = (AlarmManager)getSys
9、temService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender ); String tmpS=format(hourOfDay)+:+format(minute); setTime1.setText(tmpS); /SharedPreferences保存數據,并提交 SharedPreferences time1Share = getPreferences(0); SharedPreferences.Editor editor = time1Share.edit(); editor.p
10、utString(TIME1, tmpS); mit(); Toast.makeText(MainActivity.this,設置大明鬧鐘時間為+tmpS, Toast.LENGTH_SHORT) .show(); ,mHour,mMinute,true).show(); ); public void InitButton2() mButton2=(Button) findViewById(R.id.mButton2); mButton2.setOnClickListener(new View.OnClickListener() public void onClick(View v) Inte
11、nt intent = new Intent(MainActivity.this, CallAlarm.class); PendingIntent sender=PendingIntent.getBroadcast( MainActivity.this,0, intent, 0); AlarmManager am; am =(AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(sender); Toast.makeText(MainActivity.this,大明鬧鐘時間刪除, Toast.LENGTH_SHORT).show();
12、setTime1.setText(目前無設置); SharedPreferences time1Share = getPreferences(0); SharedPreferences.Editor editor = time1Share.edit(); editor.putString(TIME1, 目前無設置); mit(); ); public void InitButton3() setTime3=(TextView) findViewById(R.id.setTime5); mButton3=(Button)findViewById(R.id.mButton5); mButton3.
13、setOnClickListener(new View.OnClickListener() public void onClick(View v) c.setTimeInMillis(System.currentTimeMillis(); int mHour=c.get(Calendar.HOUR_OF_DAY); int mMinute=c.get(Calendar.MINUTE); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() public void onTimeSet(Ti
14、mePicker 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); Intent intent = new Intent(MainActivity.this, CallAlarm.class); PendingIntent sender=P
15、endingIntent.getBroadcast( MainActivity.this,1, intent, 0); AlarmManager am; am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sender ); String tmpS=format(hourOfDay)+:+format(minute); setTime3.setText(tmpS); /SharedPreferences保存數據,并提交 SharedPre
16、ferences time2Share = getPreferences(1); SharedPreferences.Editor editor = time2Share.edit(); editor.putString(TIME2, tmpS); mit(); Toast.makeText(MainActivity.this,設置大明鬧鐘時間為+tmpS, Toast.LENGTH_SHORT) .show(); ,mHour,mMinute,true).show(); ); public void InitButton4() mButton4=(Button) findViewById(R
17、.id.mButton6); mButton4.setOnClickListener(new View.OnClickListener() public void onClick(View v) Intent intent = new Intent(MainActivity.this, CallAlarm.class); PendingIntent sender=PendingIntent.getBroadcast( MainActivity.this,0, intent, 0); AlarmManager am; am =(AlarmManager)getSystemService(ALAR
18、M_SERVICE); am.cancel(sender); Toast.makeText(MainActivity.this,大明鬧鐘時間刪除, Toast.LENGTH_SHORT).show(); setTime3.setText(目前無設置); /SharedPreferences保存數據,并提交 SharedPreferences time2Share = getPreferences(1); SharedPreferences.Editor editor = time2Share.edit(); editor.putString(TIME2, 目前無設置); mit(); ); p
19、ublic void InitButton5() setTime2=(TextView) findViewById(R.id.setTime2); LayoutInflater factory = LayoutInflater.from(this); final View setView = factory.inflate(R.layout.timeset,null); final TimePicker tPicker=(TimePicker)setView .findViewById(R.id.tPicker); tPicker.setIs24HourView(true); final Al
20、ertDialog di=new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.clock) .setTitle(設置) .setView(setView) .setPositiveButton(確定, new DialogInterface.OnClickListener() public void onClick(DialogInterface dialog, int which) EditText ed=(EditText)setView.findViewById(R.id.mEdit); int times=Int
21、eger.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); Intent intent = new Intent(MainActivity.this, Cal
22、lAlarm.class); PendingIntent sender = PendingIntent.getBroadcast( MainActivity.this,1, intent, 0); AlarmManager am; am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),times,sender); String tmpS=format(tPicker.getCurrentHour()+:+ format(tP
23、icker.getCurrentMinute(); String subStr = 設置大明鬧鐘時間為+tmpS+ 開始,重復間隔為+times/1000+秒; setTime2.setText(設置大明鬧鐘時間為+tmpS+ 開始,重復間隔為+times/1000+秒); /SharedPreferences保存數據,并提交 SharedPreferences time3Share = getPreferences(2); SharedPreferences.Editor editor = time3Share.edit(); editor.putString(TIME3, subStr);
24、 mit(); Toast.makeText(MainActivity.this,設置大明鬧鐘為+tmpS+ 開始,重復間隔為+times/1000+秒, Toast.LENGTH_SHORT).show(); ) .setNegativeButton(取消, new DialogInterface.OnClickListener() public void onClick(DialogInterface dialog, int which) ).create(); mButton5=(Button) findViewById(R.id.mButton3); mButton5.setOnCli
25、ckListener(new View.OnClickListener() public void onClick(View v) c.setTimeInMillis(System.currentTimeMillis(); tPicker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY); tPicker.setCurrentMinute(c.get(Calendar.MINUTE); di.show(); ); public void InitButton6() mButton6=(Button) findViewById(R.id.mButton4);
26、mButton6.setOnClickListener(new View.OnClickListener() public void onClick(View v) Intent intent = new Intent(MainActivity.this, CallAlarm.class); PendingIntent sender = PendingIntent.getBroadcast( MainActivity.this,1, intent, 0); AlarmManager am; am = (AlarmManager)getSystemService(ALARM_SERVICE);
27、am.cancel(sender); Toast.makeText(MainActivity.this,鬧鐘時間刪除, Toast.LENGTH_SHORT).show(); setTime2.setText(目前無設置); /SharedPreferences保存數據,并提交 SharedPreferences time3Share = getPreferences(2); SharedPreferences.Editor editor = time3Share.edit(); editor.putString(TIME3, 目前無設置); mit(); ); Override public
28、 boolean onKeyUp(int keyCode, KeyEvent event) if(keyCode = KeyEvent.KEYCODE_BACK) builder = new AlertDialog.Builder(MainActivity.this) .setIcon(R.drawable.clock) .setTitle(溫馨提示:) .setMessage(您是否要退出大明鬧鐘程序!) .setPositiveButton(確定, new DialogInterface.OnClickListener() public void onClick(DialogInterfa
29、ce dialog, int whichButton) MainActivity.this.finish(); ) .setNegativeButton(取消, new DialogInterface.OnClickListener() public void onClick(DialogInterface dialog, int whichButton) builder.dismiss(); ).show(); return true; private String format(int x) String s=+x; if(s.length()=1) s=0+s; return s; 二、CallAlarm中的代碼:package .daming;import android.content.Context;import android.content.Intent;import android.content.BroadcastReceiver;import android.os.Bundle;public class CallAlarm extends BroadcastReceiver Override public void onReceive(Context context, Intent intent) Intent i = new Intent(context
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 居家清潔皮膚管理制度
- 幼兒園收費及管理制度
- 公司現金支出管理制度
- 學校學生用電管理制度
- 小型現場安全管理制度
- 商戶請假遲到管理制度
- 工地工資考勤管理制度
- 外出審計外勤管理制度
- 醫療集團應急管理制度
- 培訓機構水電管理制度
- 山東臨沂歷年中考作文題與審題指導(2005-2023)
- 浙江省溫州市2023-2024學年高一下學期期末考試語文試卷(含答案)
- 2025-2030中國助聽器、植入物和診斷設備行業市場發展趨勢與前景展望戰略研究報告
- 建筑工地安全月教育課件
- 米哈游的創新創業之路
- 速度輪滑講解課件
- 代加工物料管理制度
- 2025屆湖北省武漢華中師大一附中高三最后一模化學試題含解析
- 2025屆湖北省武漢華中師大一附中5月高考適應性考試英語試題試卷含解析
- 水土保持方案編制經費預算書
- 《上市公司社會責任報告披露要求》
評論
0/150
提交評論