闹钟核心:AlarmManager(闹钟管理者)
闹钟:一次性闹钟,周期闹钟
1、布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android28_alarm_notification.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getAlarmOne"
android:text="一次性闹钟" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getAlarmCycle"
android:text="周期闹钟" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消周期闹钟"
android:onClick="cancelAlarmCycle"
/>
</LinearLayout>
2、 一次性闹钟(实现闹钟步骤)
//1.获取闹钟管理者
//2.弹出一个时间对话框
//3.确定选择的时间
//4.设置闹钟
//5.到设置的闹钟时间一到,执行响应
Java代码:
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取闹钟管理者
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
}
//一次性闹钟
public void getAlarmOne(View view){
//获取当前系统时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
//1.弹出一个时间对话框
TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//2.确定选择的时间
//3.设置闹钟
Calendar calendarA=Calendar.getInstance();
calendarA.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendarA.set(Calendar.MINUTE,minute);
//4.到设置的闹钟时间一到,执行响应
Intent intent=new Intent();
intent.setAction("com.example.android28_alarm_notification.RING");
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendarA.getTimeInMillis(),pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
3、写一个类继承BroadcastReceiver接受广播(收到广播后跳转到Activity)
if ("com.example.android28_alarm_notification.RING".equals(intent.getAction())){
Log.i("test","有广播来啦");
//跳转到Activity
Intent intent1=new Intent(context,RingActivity.class);
//给Intent设置标志位Flag
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//Intent的组成部分:ComponementName extra Action Data Categlay(分组) Flag
context.startActivity(intent1);
}
4、跳转过来之后显示的Activity和执行放音乐的操作
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring);
//播放音乐
mediaPlayer = MediaPlayer.create(this, R.raw.cheng);
mediaPlayer.start();
}
//关闭闹钟
public void close(View view){
mediaPlayer.stop();
finish();
}
5、在清单文件中配置静态广播
<receiver android:name=".MyReceived">
<intent-filter>
<action android:name="com.example.android28_alarm_notification.RING" />
</intent-filter>
</receiver>
6、周期闹钟
//周期闹钟
public void getAlarmCycle(View view){
//获取当前系统时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
//1.弹出一个时间对话框
TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//2.确定选择的时间
//3.设置闹钟
Calendar calendarA=Calendar.getInstance();
calendarA.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendarA.set(Calendar.MINUTE,minute);
//4.到设置的闹钟时间一到,执行响应
Intent intent=new Intent();
intent.setAction("com.example.android28_alarm_notification.RING");
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x102,intent,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendarA.getTimeInMillis(),5000, pendingIntent);
}
},hour,minute,true);
timePickerDialog.show();
}
//取消周期闹钟
public void cancelAlarmCycle(View view){
alarmManager.cancel(pendingIntent);
}