/** * 顯示時間選擇dialog */ private void showTimePickerDialog() { Calendar calendar = Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); final TimePickerDialog timePickerDialog = new TimePickerDialog(mContext,setting,hour,minute,true); timePickerDialog.setIcon(R.drawable.ic_launcher); timePickerDialog.show(); }
</pre><pre code_snippet_id="599288" snippet_file_name="blog_20150206_3_6899549" name="code" class="java">//当点击TimePickerDialog控件的设置按钮时,调用该方法 TimePickerDialog.OnTimeSetListener setting = new TimePickerDialog.OnTimeSetListener(){ @Override public void onTimeSet(TimePicker view, int hour, int minute){ Intent intent = new Intent(mContext, AlarmReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(mContext, 0, intent, 0); long systemTime = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 这里时区需要设置一下,不然会有8个小时的时间差 calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm"); String timeStr = sdfTime.format(calendar.getTime())+""; remind_time_text.setText(timeStr);//设置显示文字 如:20:08
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd"); Date currentDate = new Date(); // 选择的每天定时时间 long selectTime = calendar.getTimeInMillis(); //保存设置提醒的日期 SharedPreferences.setOpenAppDate(mContext, sdfDate.format(currentDate)); // 进行闹铃注册,设置每天的提醒时间 AlarmManager manager = (AlarmManager)getSystemService(ALARM_SERVICE); manager.setRepeating(AlarmManager.RTC_WAKEUP, selectTime, 24*60*60*1000, sender); Toast.makeText(mContext, "设置成功! ", Toast.LENGTH_LONG).show(); } };
//接收闹铃提醒时的广播
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { <span style="white-space:pre"> </span>Log.e("AlarmReceiver","闹铃响了, 可以做点事情了~~");
</pre><pre code_snippet_id="599288" snippet_file_name="blog_20150206_9_199033" name="code" class="java"><span style="white-space:pre"> </span>}
</pre><pre code_snippet_id="599288" snippet_file_name="blog_20150206_11_7718869" name="code" class="java">}
清单文件中加入
<receiver android:name="com.xxxx.android.AlarmReceiver" android:process=":remote">
时间: 2024-10-25 04:40:58