1.涉及到的技术点
- 使用 MediaPlayer播放音频
- 使用AnalogClock 显示当前时钟
- 使用DatePickerDialog 日期选择框,设置提醒日期
- 使用TimePickerDialog时间选择框,设置提醒时间
2. 闹铃设置页面实现
- 编写设置闹铃的布局页面 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<AnalogClock
android:id="@+id/analogClock"
android:layout_width="131dp"
android:layout_height="107dp"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/btn_time"
android:layout_width="219dp"
android:layout_height="54dp"
android:layout_marginTop="20dp"
android:text="选择时间" />
<TextView
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="日期" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="时间" />
<Button
android:id="@+id/btn_date"
android:layout_width="219dp"
android:layout_height="51dp"
android:layout_marginTop="20dp"
android:text="选择日期" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/sw_set"
android:layout_width="wrap_content"
android:layout_height="51dp"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/tv_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="时间" />
</androidx.appcompat.widget.LinearLayoutCompat>
布局效果图
- 当点击【选择时间】按钮的时候,弹出时间选择对话框
bt_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new TimePickerDialog(MainActivity.this,mOnTimeSetListener,hour,minute,true).show();
}
});
- mOnTimeSetListener 的回调监听
//时间选择回调监听器
TimePickerDialog.OnTimeSetListener mOnTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay;
MainActivity.this.minute = minute;
tv_time.setText(hourOfDay + "时" + minute + "分");
}
};
实现效果如下
- 当点击【选择日期】按钮的时候,弹出日期选择对话框
bt_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this,mOnDateSetListener,year,month,day).show();
}
});
- mOnDateSetListener 回调监听
//日期选择回调监听器
DatePickerDialog.OnDateSetListener mOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int month, int day) {
MainActivity.this.year = year;
MainActivity.this.month = month;
MainActivity.this.day = day;
tv_date.setText(year + "年" + (month + 1) + "月" + day + "日");
}
};
这里注意:month 月份要加1 ,在我们习惯中,默认从1开始,外国人喜欢从0开始(因为计算机是英文编程,存在一些差异~)
实现效果如下
- 滑动开关,设置闹铃
sw_set.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
setAlarm();
}else {
cancelAlarm();
}
}
});
setAlarm方法
/**
* 设置闹铃
*/
private void setAlarm() {
ca = Calendar.getInstance();
ca.set(year, month, day, hour, minute, 0);
//跳转到闹铃页面
Intent intent = new Intent(MainActivity.this, ALMActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, ca.getTimeInMillis(), pi);
Toast.makeText(this, "闹钟已设置", Toast.LENGTH_SHORT).show();
tv_status.setText("闹钟时间:" + year + "年" + (month + 1) + "月" + day + "日" + hour + "时" + minute + "分");
}
cancelAlarm 方法
/**
* 取消闹铃
* */
private void cancelAlarm() {
Intent intent = new Intent(MainActivity.this, ALMActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi);
Toast.makeText(this, "闹钟已取消", Toast.LENGTH_SHORT).show();
tv_status.setText("not set");
}
- 闹铃设置页面整个代码实现过程
public class MainActivity extends AppCompatActivity {
Button bt_date, bt_time;
TextView tv_date, tv_time, tv_status;
SwitchCompat sw_set;
int year, month, day, hour, minute;
Calendar ca = Calendar.getInstance();
//日期选择回调监听器
DatePickerDialog.OnDateSetListener mOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int month, int day) {
MainActivity.this.year = year;
MainActivity.this.month = month;
MainActivity.this.day = day;
tv_date.setText(year + "年" + (month + 1) + "月" + day + "日");
}
};
//时间选择回调监听器
TimePickerDialog.OnTimeSetListener mOnTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay;
MainActivity.this.minute = minute;
tv_time.setText(hourOfDay + "时" + minute + "分");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
bt_date =findViewById(R.id.btn_date);
bt_time =findViewById(R.id.btn_time);
sw_set =findViewById(R.id.sw_set);
tv_date =findViewById(R.id.tv_date);
tv_time =findViewById(R.id.tv_time);
tv_status =findViewById(R.id.tv_status);
//获取当前时间
year =ca.get(Calendar.YEAR);
month =ca.get(Calendar.MONTH);
day =ca.get(Calendar.DAY_OF_MONTH);
hour =ca.get(Calendar.HOUR_OF_DAY);
minute =ca.get(Calendar.MINUTE);
bt_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this,mOnDateSetListener,year,month,day).show();
}
});
bt_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new TimePickerDialog(MainActivity.this,mOnTimeSetListener,hour,minute,true).show();
}
});
sw_set.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
setAlarm();
}else {
cancelAlarm();
}
}
});
}
/**
* 设置闹铃
*/
private void setAlarm() {
ca = Calendar.getInstance();
ca.set(year, month, day, hour, minute, 0);
Intent intent = new Intent(MainActivity.this, ALMActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, ca.getTimeInMillis(), pi);
Toast.makeText(this, "闹钟已设置", Toast.LENGTH_SHORT).show();
tv_status.setText("闹钟时间:" + year + "年" + (month + 1) + "月" + day + "日" + hour + "时" + minute + "分");
}
/**
* 取消闹铃
* */
private void cancelAlarm() {
Intent intent = new Intent(MainActivity.this, ALMActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pi);
Toast.makeText(this, "闹钟已取消", Toast.LENGTH_SHORT).show();
tv_status.setText("not set");
}
}
3. 响铃页面(ALMActivity)实现
当我们在闹铃设置界面设置闹铃时间+日期之后,当时间达到闹铃设置的时间,就跳转到闹铃页面
- 响铃页面布局activity_almactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ALMActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="30dp"
android:text="闹铃启动啦!!!"
android:textSize="30sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
这里布局很简单,就简单显示一句话。闹铃页面重点是在于播放音频,达到提醒用户的效果
- ALMActivity.java 页面代码实现
public class ALMActivity extends AppCompatActivity {
MediaPlayer mMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_almactivity);
try {
// 初始化 MediaPlayer
mMediaPlayer = new MediaPlayer();
// 设置数据源
mMediaPlayer = MediaPlayer.create(this, R.raw.music);
// 获取 AudioManager 服务
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
//这行代码检查 STREAM_ALARM 音频流的音量是否不为0。如果音量为0,表示用户可能已经将闹钟声音静音了,此时不会播放音频
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
// 设置音频循环播放
mMediaPlayer.setLooping(true);
// 开始播放音频
mMediaPlayer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
//弹出对话框
new AlertDialog.Builder(this).setTitle("闹钟提醒").setMessage("这是闹铃提醒的内容")
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//释放 MediaPlayer 资源
mMediaPlayer.release();
mMediaPlayer= null;
finish();
}
}).show();
}
}
在这里使用MediaPlayer来播放音频,代码都写了非常想起的注释,就不单独讲啦
这里注意:音频文件放在res->raw
下,默认raw是没有这个文件夹的,新建文件夹就好啦,如下图所示