TimePicker,SharedPreferences实现android小闹钟


简述:

使用TimePickerDialog来实现设置闹钟

分为一次性闹钟和周期性闹钟

使用SharedPreferences来储存闹钟的设置信息

 

AlarmClockActivity布局:

View Code

  1 <?xml version=”1.0” encoding=”utf-8”?>
2 <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android
3 android:layout_width=”fill_parent”
4 android:layout_height=”fill_parent”
5 android:orientation=”vertical”>
6
7 <TextView
8 android:id=”@+id/timeId”
9 android:layout_width=”fill_parent”
10 android:layout_height=”wrap_content”
11 android:textSize=”33dp”
12 android:gravity=”center_horizontal”
13 android:layout_marginTop=”10dp”
14 android:text=”00:00 AM” />
15
16 <TableLayout
17 android:id=”@+id/tableLayout1”
18 android:layout_width=”match_parent”
19 android:layout_height=”wrap_content”
20 android:layout_marginTop=”10dp”>
21
22 <TableRow
23 android:id=”@+id/tableRow1”
24 android:layout_width=”wrap_content”
25 android:layout_height=”wrap_content” >
26
27 <TextView
28 android:layout_width=”wrap_content”
29 android:layout_height=”wrap_content”
30 android:layout_marginLeft=”15dp”
31 android:layout_weight=”1.0”
32 android:textSize=”20sp”
33 android:text=”只响一次的闹钟” />
34
35 <Button
36 android:id=”@+id/onceSetId”
37 android:layout_width=”wrap_content”
38 android:layout_height=”wrap_content”
39 android:layout_marginRight=”10dp”
40 android:layout_weight=”1.0”
41 android:text=”设置闹钟” />
42 </TableRow>
43
44 <TableRow
45 android:id=”@+id/tableRow2”
46 android:layout_width=”wrap_content”
47 android:layout_height=”wrap_content” >
48
49 <TextView
50 android:id=”@+id/onceTextId”
51 android:layout_width=”wrap_content”
52 android:layout_height=”wrap_content”
53 android:layout_marginLeft=”15dp”
54 android:layout_weight=”1.0”
55 android:textSize=”20sp”
56 android:text=”暂无设置” />
57
58 <Button
59 android:id=”@+id/onceDeleId”
60 android:layout_width=”wrap_content”
61 android:layout_height=”wrap_content”
62 android:layout_marginRight=”10dp”
63 android:layout_weight=”1.0”
64 android:text=”删除闹钟” />
65 </TableRow>
66
67 <TableRow
68 android:id=”@+id/tableRow3”
69 android:layout_width=”wrap_content”
70 android:layout_height=”wrap_content”
71 android:layout_marginTop=”25dp”>
72
73 <TextView
74 android:layout_width=”wrap_content”
75 android:layout_height=”wrap_content”
76 android:layout_marginLeft=”15dp”
77 android:layout_weight=”1.0”
78 android:textSize=”20sp”
79 android:text=”重复响起的闹钟” />
80
81 <Button
82 android:id=”@+id/timesSetId”
83 android:layout_width=”wrap_content”
84 android:layout_height=”wrap_content”
85 android:layout_marginRight=”10dp”
86 android:layout_weight=”1.0”
87 android:text=”设置闹钟” />
88 </TableRow>
89
90 <TableRow
91 android:id=”@+id/tableRow4”
92 android:layout_width=”wrap_content”
93 android:layout_height=”wrap_content” >
94
95 <TextView
96 android:id=”@+id/timesTextId”
97 android:layout_width=”wrap_content”
98 android:layout_height=”wrap_content”
99 android:layout_marginLeft=”15dp”
100 android:layout_weight=”1.0”
101 android:textSize=”20sp”
102 android:text=”暂无设置” />
103
104 <Button
105 android:id=”@+id/timesDeleId”
106 android:layout_width=”wrap_content”
107 android:layout_height=”wrap_content”
108 android:layout_marginRight=”10dp”
109 android:layout_weight=”1.0”
110 android:text=”删除闹钟” />
111 </TableRow>
112 </TableLayout>
113
114 </LinearLayout>


 

AlarmClockActivity.java:

View Code

  1 package com.tiantian.test;
2
3 import java.util.Calendar;
4
5 import android.app.Activity;
6 import android.app.AlarmManager;
7 import android.app.PendingIntent;
8 import android.app.TimePickerDialog;
9 import android.app.TimePickerDialog.OnTimeSetListener;
10 import android.content.Intent;
11 import android.content.SharedPreferences;
12 import android.content.SharedPreferences.Editor;
13 import android.os.Bundle;
14 import android.os.Handler;
15 import android.text.format.Time;
16 import android.util.Log;
17 import android.view.View;
18 import android.view.View.OnClickListener;
19 import android.widget.Button;
20 import android.widget.TextView;
21 import android.widget.TimePicker;
22
23 public class AlarmClockActivity extends Activity {
24 /* Called when the activity is first created. /
25 private TextView timeTV;
26 static TextView onceTV;
27 private TextView timesTV;
28 private Button onceSetBT;
29 private Button onceDeleBT;
30 private Button timesSetBT;
31 private Button timesDelBT;
32 private Calendar c;
33
34 private Handler mHandler;
35 private SharedPreferences prefs;
36 private Editor editor;
37
38 private final static String ONCE_ALARM = “com.tiantian.action.ONCE_ALARM”;
39 private final static String TIMES_ALARM = “com.tiantian.action.TIMES_ALARM”;
40 @Override
41 public void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.main);
44 timeTV = (TextView) findViewById(R.id.timeId);
45 onceTV = (TextView) findViewById(R.id.onceTextId);
46 timesTV = (TextView) findViewById(R.id.timesTextId);
47 mHandler = new Handler();
48 mHandler.post(timeThread);
49
50 prefs = getSharedPreferences(“alarmClock”, MODE_PRIVATE);
51 editor = prefs.edit();
52 if(prefs != null){
53 onceTV.setText(prefs.getString(“_onceTV”, “暂无设置”));
54 timesTV.setText(prefs.getString(“_timesTV”, “暂无设置”));
55 }
56
57 onceSetBT = (Button) findViewById(R.id.onceSetId);
58 onceSetBT.setOnClickListener(new ButtonListener());
59 onceDeleBT = (Button) findViewById(R.id.onceDeleId);
60 onceDeleBT.setOnClickListener(new ButtonListener());
61 timesSetBT = (Button) findViewById(R.id.timesSetId);
62 timesSetBT.setOnClickListener(new ButtonListener());
63 timesDelBT = (Button) findViewById(R.id.timesDeleId);
64 timesDelBT.setOnClickListener(new ButtonListener());
65
66 }
67
68 class ButtonListener implements OnClickListener{
69
70 @Override
71 public void onClick(View v) {
72 // TODO Auto-generated method stub
73 switch(v.getId()){
74 case R.id.onceSetId:
75 c = Calendar.getInstance();
76 int hour = c.get(Calendar.HOUR_OF_DAY);
77 int minute = c.get(Calendar.MINUTE);
78 new TimePickerDialog(AlarmClockActivity.this, new OnTimeSetListener() {
79
80 @Override
81 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
82 // TODO Auto-generated method stub
83 onceTV.setText(“已设置为:” + changeTime(hourOfDay) + “:” + changeTime(minute));
84 c = timePicker(hourOfDay, minute);
85 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
86 Intent intent = new Intent(AlarmClockActivity.this, CallAlarm.class);
87 intent.setAction(ONCE_ALARM);
88 PendingIntent senderPI = PendingIntent.getBroadcast(AlarmClockActivity.this, 0, intent, 0);
89 am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), senderPI);
90 }
91 }, hour, minute, true).show();
92 break;
93 case R.id.onceDeleId:
94 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
95 Intent intent = new Intent(AlarmClockActivity.this, CallAlarm.class);
96 PendingIntent senderPI = PendingIntent.getBroadcast(AlarmClockActivity.this, 0, intent, 0);
97 am.cancel(senderPI);
98 onceTV.setText(“暂无设置”);
99 break;
100 case R.id.timesSetId:
101 c = Calendar.getInstance();
102 int hour1 = c.get(Calendar.HOUR_OF_DAY);
103 int minute1 = c.get(Calendar.MINUTE);
104 new TimePickerDialog(AlarmClockActivity.this, new OnTimeSetListener() {
105
106 @Override
107 public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
108 // TODO Auto-generated method stub
109 timesTV.setText(“已设置为:” + changeTime(hourOfDay) + “:” + changeTime(minute));
110 c = timePicker(hourOfDay, minute);
111 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
112 Intent intent = new Intent(AlarmClockActivity.this, CallAlarm.class);
113 intent.setAction(TIMES_ALARM);
114 PendingIntent senderPI = PendingIntent.getBroadcast(AlarmClockActivity.this, 1, intent, 0);
115 am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 246060*1000, senderPI);
116 }
117 }, hour1, minute1, true).show();
118 break;
119 case R.id.timesDeleId:
120 AlarmManager am1 = (AlarmManager) getSystemService(ALARM_SERVICE);
121 Intent intent1 = new Intent(AlarmClockActivity.this, CallAlarm.class);
122 PendingIntent senderPI1 = PendingIntent.getBroadcast(AlarmClockActivity.this, 1, intent1, 0);
123 am1.cancel(senderPI1);
124 timesTV.setText(“暂无设置”);
125 break;
126 }
127
128 }
129
130 }
131
132 private Calendar timePicker(int hourOfDay, int minute){
133 c.setTimeInMillis(System.currentTimeMillis());
134 c.set(Calendar.HOUR_OF_DAY, hourOfDay);
135 c.set(Calendar.MINUTE, minute);
136 c.set(Calendar.SECOND, 0);
137 c.set(Calendar.MILLISECOND, 0);
138 //避免设置时间比当前时间小时 马上响应的情况发生
139 if(c.getTimeInMillis() < Calendar.getInstance().getTimeInMillis()){
140 c.set(Calendar.DAY_OF_MONTH, Calendar.DAY_OF_MONTH + 1);
141 }
142 return c;
143 }
144
145
146 @Override
147 protected void onDestroy() {
148 // TODO Auto-generated method stub
149 super.onDestroy();
150 editor.putString(“_onceTV”, onceTV.getText().toString());
151 editor.putString(“_timesTV”, timesTV.getText().toString());
152 editor.commit();
153 }
154
155
156 Runnable timeThread = new Runnable() {
157
158 @Override
159 public void run() {
160 // TODO Auto-generated method stub
161 timeTV.setText(changeTime(Calendar.getInstance().get(Calendar.HOUR))
162 + “:” +changeTime(Calendar.getInstance().get(Calendar.MINUTE))
163 + “ “ + changeAMPM(Calendar.getInstance().get(Calendar.AM_PM)));
164 mHandler.postDelayed(timeThread, 100);
165 }
166 };
167
168 private String changeTime(int a){
169 String num = null;
170 if(a < 10){
171 num = “0” + a;
172 }else if(a > 9){
173 num = “” + a;
174 }
175 return num;
176 }
177 private String changeAMPM(int ap){
178 String apStr = null;
179 if(ap == 0){
180 apStr = “AM”;
181 }else if(ap == 1){
182 apStr = “PM”;
183 }
184 return apStr;
185 }
186
187 }


CallAlarm.java:

View Code

 1 package com.tiantian.test;
2
3 import android.content.BroadcastReceiver;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.util.Log;
7
8 public class CallAlarm extends BroadcastReceiver{
9 private final static String ONCE_ALARM = “com.tiantian.action.ONCE_ALARM”;
10 private final static String TIMES_ALARM = “com.tiantian.action.TIMES_ALARM”;
11 @Override
12 public void onReceive(Context context, Intent intent) {
13 // TODO Auto-generated method stub
14 Log.v(“CAT”, “I’m in BroadcastReceiver”);
15 Intent wakeUpIntent = new Intent(context, WakeUp.class);
16 wakeUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
17 if(intent.getAction().equals(ONCE_ALARM)){
18 wakeUpIntent.setAction(ONCE_ALARM);
19 }else if(intent.getAction().endsWith(TIMES_ALARM)){
20 wakeUpIntent.setAction(TIMES_ALARM);
21 }
22 context.startActivity(wakeUpIntent);
23 }
24
25 }


 

WakeUp.java

View Code

 1 package com.tiantian.test;
2
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.content.DialogInterface;
6 import android.content.DialogInterface.OnClickListener;
7 import android.content.SharedPreferences;
8 import android.content.SharedPreferences.Editor;
9 import android.media.MediaPlayer;
10 import android.os.Bundle;
11 import android.os.Environment;
12 import android.view.View;
13 import android.widget.Button;
14
15 public class WakeUp extends Activity{
16 private Button okButton;
17 private SharedPreferences prefs;
18 private Editor editor;
19 private final static String ONCE_ALARM = “com.tiantian.action.ONCE_ALARM”;
20
21 private MediaPlayer mediaPlayer;
22 @Override
23 protected void onCreate(Bundle savedInstanceState) {
24 super.onCreate(savedInstanceState);
25 setContentView(R.layout.wake_up);
26 prefs = getSharedPreferences(“alarmClock”, MODE_PRIVATE);
27 mediaPlayer = new MediaPlayer();
28 try {
29 //mediaPlayer.setDataSource(Environment.getExternalStorageDirectory() + “/mp3/trhxn.mp3”);
30 mediaPlayer = MediaPlayer.create(this, R.raw.conan);
31 //mediaPlayer.prepare();
32 mediaPlayer.setLooping(true);
33 mediaPlayer.start();
34 } catch (Exception e) {
35 e.printStackTrace();
36 }
37
38 okButton = (Button) findViewById(R.id.okButtonId);
39 okButton.setOnClickListener(new View.OnClickListener() {
40
41 @Override
42 public void onClick(View v) {
43 // TODO Auto-generated method stub
44 if(getIntent().getAction().equals(ONCE_ALARM)){
45 editor = prefs.edit();
46 editor.putString(“_onceTV”, “暂无设置”);
47 editor.commit();
48 AlarmClockActivity.onceTV.setText(“暂无设置”);
49 }
50 mediaPlayer.stop();
51 mediaPlayer.release();
52 finish();
53 }
54 });
55
56
57
58 }
59
60
61 }




来源博客:Wang Jie's Blog
本文链接:https://blog.wangjiegulu.com/2012/03/03/TimePicker-SharedPreferences实现android小闹钟/
版权声明:本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处。