在日常生活中,人们都离不开手机了,闹钟也是一个不可或缺的工具,手机有系统自带的闹钟,但这种的闹钟关闭得却非常地容易,而我们想要的是一个真正有效用性的闹钟,所以就写了个一个自定义的闹钟,以满足各种需求.
制作闹钟前要先获取闹钟的管理者
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
public void setAlarm(View view){
//获取当前的时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
//弹出时间选择框
TimePickerDialog timePickerDialog= new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//用选的时间设置闹钟时间
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
//设置一个广播,
Intent intent=new Intent();
intent.setAction("com.zking.g150820_android28_alarm_notification.RING");
//将来时态的跳转
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x100,intent,0);
//设置闹钟alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
}
},hour,minute,true);
//展示
timePickerDialog.show();
}
public void setAlarmCycle(View view){
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog= new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
Intent intent=new Intent();
intent.setAction("com.zking.g150820_android28_alarm_notification.RING");
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x100,intent,0);
//重复闹钟与一次性闹钟的不同之处:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),5000,pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
手机消息的推送在几乎所有的软件上都是有的,利用Notification则可以实现消息推送,
先说一下Notification: Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。
public void sendNotification(View view){
//实例化通知管理器
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
//利用RemoteViews则可以实现自定义的推送界面
RemoteViews remoteViews=new RemoteViews("com.example.android_alarm_notification",R.layout.layout1);
builder.setCustomContentView(remoteViews);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
//维护通知的消失
builder.setAutoCancel(true);
//设置图标
builder.setSmallIcon(android.R.drawable.ic_media_play);
builder.setContentIntent(PendingIntent.getActivity(this,0x100,new Intent(this,Main2Activity.class),0));
Notification notification=builder.build();
//发送通知
notificationManager.notify(0x200,notification);
}