private static String midStr1 = "";
private static String midStr2 = "";
public static String getStrDate(Calendar calendar){
int year;
int month;
int day;
int hour;
int minute;
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
if (minute<10){
midStr2 = "0";
}
if (hour<10){
midStr1 = "0";
}
return year + "年" + month + "月" + day + "日 " +midStr1 + hour + ":"+ midStr2 + minute;
}
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int chooseY, int chooseM, int chooseD) {
//设置储存数据的年月日
CalendarLab.get(getActivity()).setCalendarYMD(chooseY,chooseM,chooseD);
mYMDText.setText(chooseY + "年"+chooseM + "月" +chooseD);
}
},year,month,day);
datePickerDialog.show();
TimePickerDialog timePickerDialog = new TimePickerDialog(getActivity(),
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int chooseH, int chooseM) {
//这里的chooseH和chooseM就是选中的小时和分钟
CalendarLab.get(getActivity()).setCalendarHM(chooseH,chooseM);
String s1 = "";
String s2 = "";
if (chooseH<10){
s1 = "0";
}
if (chooseM<10){
s2 = "0";
}
mHMText.setText(s1 + chooseH + ":" + s2 + chooseM);
}
},hour,minute,true); //最后一个参数表示是否使用24小时制
timePickerDialog.show();
Intent intent = new Intent(getActivity(), AlarmService.class);
intent.putExtra("calendar", CalendarLab.get(getActivity()).getCalendar());
intent.putExtra("noteId", noteId);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startService(intent);
//发送一条启动闹铃图标的广播
Intent intentIcon = new Intent("com.gaozhidong.android.Color");
intentIcon.putExtra("noteId", noteId);
getActivity().sendBroadcast(intentIcon);
public class AlarmService extends Service {
private static final String TAG = "test";
private Calendar mCalendar;
private AlarmManager mAlarmManager;
public AlarmService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mCalendar = (Calendar) intent.getSerializableExtra("calendar");
int noteId = intent.getIntExtra("noteId", 0);
//设置广播
Intent intent2 = new Intent();
intent2.setAction("com.g.android.RING");
intent2.putExtra("noteId", noteId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent2, 0);
//根据不同的版本使用不同的设置方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mAlarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
} else {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent);
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e(TAG, "onDestroy: 服务被杀死");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.g.android.RING")){
Intent intent2 = new Intent(context,ContentActivity.class);
int noteId = intent.getIntExtra("noteId",1);
NoteBody noteBody = NotesLab.get(context).queryNote(noteId);
//发送通知
PendingIntent pi = PendingIntent.getActivity(context,0,intent2,0);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(noteBody.getTime())
.setContentText(noteBody.getText())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.mipmap.ic_launcher))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setAutoCancel(true)
.setContentIntent(pi);
Notification notification = builder.build();
manager.notify(1,notification);
//发送一条清空闹铃图标的广播
NotesLab.get(context).updateFlag(noteId,0);
Intent intent1 = new Intent("com.g.android.NoColor");
intent1.putExtra("noteId",noteId);
context.sendBroadcast(intent1);
}
}
}
整个流程:
选择时间 -> 确定后启动服务 -> 服务开启后设置AlarmManager -> AlarmManager设置到点发送一条广播 -> 广播接收器接收到广播后发送一条通知 -> 通知发出铃声、震动、文字或图片通知