相信大家都有早上起不来的时候,是否想过自己做一个关不掉的闹钟呢?当然自己做的 自己可以关,也可以有不能关的
其实做一个闹钟并不难,首先要得到闹钟管理器AlarmManager 然后就是设置闹钟的时间了,
当你点击设置的时候要获取系统当前的时间
Calendar calendar=Calendar.getInstance();
int hour=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
然后要弹出时间对话框TimePickerDialog,再设置闹钟的时间,时间一到,发送广播(闹钟响了),广播接受者中跳转Activity,然后再那个Activity中播放音乐
另外还可以添加通知管理器NotifitionManager在通知栏可以显示消息
大致思路就是这样接下来就看代码吧
主要布局MainActivity
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置闹钟(一次)"
android:id="@+id/setAlarm"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置闹钟(周期)"
android:id="@+id/setAlarmCycle"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消周期"
android:id="@+id/cancelCycle"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知"
android:id="@+id/sendNotification"
/>
MainActivity的主要代码
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
private Button setAlarm;
private Button setAlarmCycle;
private Button cancelCycle;
private Button sendNotification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取闹钟管理者
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setAlarm = (Button) findViewById(R.id.setAlarm);
setAlarmCycle = (Button) findViewById(R.id.setAlarmCycle);
cancelCycle = (Button) findViewById(R.id.cancelCycle);
sendNotification = (Button) findViewById(R.id.sendNotification);
setAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(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 timePicker, int i, int i1) {
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,i);
c.set(Calendar.MINUTE,i1);
Intent intent=new Intent();
intent.setAction("com.zking.g150820_android28_alarm_notification.RING");
//将来时态的跳转 ang eng ing ong
PendingIntent pendingIntent=PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
//时间一到,发送广播(闹钟响了)
//广播接受者中(跳转Activity)
// 跳转Activity,在这个Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
});
setAlarmCycle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(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 timePicker, int i, int i1) {
Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,i);
c.set(Calendar.MINUTE,i1);
Intent intent=new Intent();
intent.setAction("com.zking.g150820_android28_alarm_notification.RING");
//将来时态的跳转 ang eng ing ong
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0x101,intent,0);
//设置闹钟
// alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
//设置周期闹钟
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);
//时间一到,发送广播(闹钟响了)
//广播接受者中(跳转Activity)
// 跳转Activity,在这个Activity中播放音乐
}
},hour,minute,true);
timePickerDialog.show();
}
});
cancelCycle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alarmManager.cancel(pendingIntent);
}
});
sendNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//实例化通知管理器
NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//实例化通知
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("温馨提示");
builder.setContentText("天气转变大注意适当增减衣服");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setAutoCancel(true);
builder.setSmallIcon(android.R.drawable.ic_media_play);
builder.setContentIntent(PendingIntent.getActivity(MainActivity.this,0x102,new Intent(MainActivity.this,RingActivity.class),0));
Notification notification=builder.build();
//发送通知
notificationManager.notify(0x101,notification);
}
});
}
}
响铃时的布局文件
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="懒pig,该起床了!!!"
android:textSize="30sp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
android:id="@+id/stop"
/>
广播接受者的代码
public class RingReceived extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if("com.zking.g150820_android28_alarm_notification.RING".equals(intent.getAction())){
Log.i("test","闹钟响了");
//跳转到Activity
Intent intent1=new Intent(context,RingActivity.class);
//给Intent设置标志位
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
响铃的Activity
public class RingActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private Button stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ring);
stop = (Button) findViewById(R.id.stop);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mediaPlayer.stop();
finish();
}
});
mediaPlayer = MediaPlayer.create(this, R.raw.love);
mediaPlayer.start();
}
}
既然有 清单文件别忘了配置哦
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".RingActivity"></activity> <receiver android:name=".RingReceived"> <intent-filter> <action android:name="com.example.clerknaozhong.RING"></action> </intent-filter> </receiver>
那么这样一个简单的闹钟就完成了