闹钟可以设置一次性的闹钟和定时循环的闹钟,下面分别分享给大家
设置闹钟时间的activity_alarm1.xml文件:
里面有设置一次闹钟按钮、周期闹钟按钮,取消周期闹钟按钮
<?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:id="@+id/activity_alarm1" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.zking.a24provider.Alarm1Activity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置闹钟(一次)" android:onClick="alertAlarm"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置闹钟(周期)" android:onClick="alertAlarmCycle"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消周期闹钟" android:onClick="cancleCycle"/> </LinearLayout>
AlarmActivity文件代码:这里需要广播接收者来跳转Activity,周期闹钟需要点击取消周期闹钟才可停止
package com.zking.a24provider; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TimePicker; import java.util.Calendar; public class Alarm1Activity extends AppCompatActivity { private AlarmManager alarmManager; private PendingIntent pendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alarm1); //获取闹钟管理者 alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); }
//设置闹钟一次 public void alertAlarm(View view){ //弹出时间对话框 // 获取当前系统时间设置在对话框中 Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //得到闹钟距离设置时间的毫秒数 Calendar c=Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY,hourOfDay); c.set(Calendar.MINUTE,minute); Intent intent=new Intent(); intent.setAction("com.zking.a24provider.RING"); //将来时态的跳转 pendingIntent = PendingIntent.getBroadcast(Alarm1Activity.this,0x101,intent,0); //设置一次闹钟 alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pendingIntent); //时间一到,发送广播(闹钟响了) // 广播接收者中(跳转Activity) // 唤醒页面,跳转Activity并播放音乐 } },hour,minute,true); timePickerDialog.show(); }
//设置闹钟 周期 public void alertAlarmCycle(View view){ //弹出时间对话框 // 获取当前系统时间设置在对话框中 Calendar calendar=Calendar.getInstance(); int hour=calendar.get(Calendar.HOUR_OF_DAY); int minute=calendar.get(Calendar.MINUTE); TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { //得到闹钟距离设置时间的毫秒数 Calendar c=Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY,hourOfDay); c.set(Calendar.MINUTE,minute); Intent intent=new Intent(); intent.setAction("com.zking.a24provider.RING"); //将来时态的跳转 PendingIntent pendingIntent=PendingIntent.getBroadcast(Alarm1Activity.this,0x101,intent,0); //设置周期闹钟 // 设置5秒后继续响,一般都是设置24小时后响,看个人需求 alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000,pendingIntent); //时间一到,发送广播(闹钟响了) // 广播接收者中(跳转Activity) // 唤醒页面,跳转Activity并播放音乐 } },hour,minute,true); timePickerDialog.show(); }
//取消周期闹钟 public void cancleCycle(View view){ alarmManager.cancel(pendingIntent); } }
闹钟响起后跳转到的页面activity_ring.xml:
里面添加一些提示内容,还有停止闹钟按钮
<?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:id="@+id/activity_ring" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.zking.a24provider.RingActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="起床啦!"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:onClick="stop"/> </LinearLayout>
闹钟响起后的RingActivity:主要是用来播放音乐和停止闹钟package com.zking.a24provider; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class RingActivity extends AppCompatActivity { private MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ring);//这里的音乐文件是放在res下的raw里 mediaPlayer = MediaPlayer.create(this, R.raw.love); mediaPlayer.start(); } //停止音乐 public void stop(View view){ mediaPlayer.stop(); finish(); } }
广播接收者文件RingReceived:需要在清单文件里配置
<!--配置广播接收者--> <receiver android:name=".RingReceived"> <intent-filter> <action android:name="com.zking.a24provider.RING"></action> </intent-filter> </receiver>
package com.zking.a24provider; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class RingReceived extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if("com.zking.a24provider.RING".equals(intent.getAction())){ Log.i("text","闹钟响了"); //跳转到Activity Intent intent1=new Intent(context,RingActivity.class); //需要给intent设置标志位,类似于把要跳转的activity放到activity栈中 intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent1); } } }
喜欢就点个赞吧! ^_^