下班闹钟是先设置每天的下班时间,然后设置闹钟,到点提醒下班;护眼闹钟是设置从当前时间开始,每45分钟提醒一次,让眼睛休息一下。提醒默认振动和响铃还有灯光。
public class RemindActivity extends AppCompatActivity { private final static String TAG = RemindActivity.class.getSimpleName(); private TimePicker timePicker; private Button mButton1,mButton2,mButtonCancel,mButtonCancel1; private int mHour = -1; private int mMinute = -1; public static final long DAY = 1000L*60*60*24; Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_remind); mContext = RemindActivity.this; //获取当前时间 Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); if(mHour == -1&& mMinute == -1){ mHour = calendar.get(Calendar.HOUR_OF_DAY); mMinute = calendar.get(Calendar.MINUTE); } timePicker = (TimePicker) findViewById(R.id.timePicker); timePicker.setCurrentHour(mHour); timePicker.setCurrentMinute(mMinute); timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) { mHour = hourOfDay; mMinute = minute; } }); mButton1 = (Button) findViewById(R.id.normal_button); //设置下班闹铃 mButton1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(RemindActivity.this, AlarmReceiver.class); intent.setAction("com.example.commuting.action.outwork"); PendingIntent pendingIntent = PendingIntent.getBroadcast(RemindActivity.this,0,intent,0); long firstTime = SystemClock.elapsedRealtime();//开机之后到现在的运行时间 long systemTime = System.currentTimeMillis(); //设置闹铃时间 Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(System.currentTimeMillis()); calendar1.setTimeZone(TimeZone.getTimeZone("GMT+8")); calendar1.set(Calendar.MINUTE,mMinute); calendar1.set(Calendar.HOUR_OF_DAY,mHour); calendar1.set(Calendar.SECOND,0); calendar1.set(Calendar.MILLISECOND,0); //选择的每天的定时时间即下班时间 long selectTime = calendar1.getTimeInMillis(); //如果当前时间大于设置的时间,那么从第二天的设定时间开始 if(systemTime > selectTime){ Toast.makeText(mContext,"设置的时间小于当前时间",Toast.LENGTH_SHORT).show(); calendar1.add(Calendar.DAY_OF_MONTH,1); selectTime = calendar1.getTimeInMillis(); } //计算现在时间到设置时间的时间差 long diffTime1 = selectTime - systemTime; firstTime += diffTime1; //进行闹铃注册 AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE); manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,5*60*1000,pendingIntent); Toast.makeText(mContext,"设置下班提醒成功!",Toast.LENGTH_SHORT).show(); } }); //重复闹铃 mButton2 = (Button) findViewById(R.id.repeating_button); mButton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(mContext,AlarmReceiver.class); intent.setAction("com.example.commuting.action.eyes"); PendingIntent sender = PendingIntent.getBroadcast(mContext,0,intent,0); long firstTime = SystemClock.elapsedRealtime();//开机之后到现在的运行时间 long systemTime = System.currentTimeMillis(); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(System.currentTimeMillis()); calendar2.setTimeZone(TimeZone.getTimeZone("GMT+8")); calendar2.set(Calendar.MINUTE,mMinute); calendar2.set(Calendar.HOUR_OF_DAY,mHour); calendar2.set(Calendar.SECOND,0); calendar2.set(Calendar.MILLISECOND,0); //选择的每天定时时间 long selectTime = calendar2.getTimeInMillis(); //如果当前时间大于设置的时间,那么从第二天的设定时间开始 if(systemTime > selectTime){ Toast.makeText(mContext,"设置的时间小于当前时间",Toast.LENGTH_SHORT).show(); calendar2.add(Calendar.DAY_OF_MONTH,1); selectTime = calendar2.getTimeInMillis(); } //计算现在时间到设置时间的时间差 long difTime = selectTime - systemTime; //不管将不将此句注释掉,都是每隔一分钟闹铃响一次。 firstTime += difTime; //进行闹铃注册 AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE); // //第一个参数int type指定定时服务的类型,该参数接受如下值: // /*ELAPSED_REALTIME: 在指定的延时过后,发送广播,但不唤醒设备(闹钟在睡眠状态下不可用)。如果在系统休眠时闹钟触发,它将不会被传递,直到下一次设备唤醒。 // *ELAPSED_REALTIME_WAKEUP: 在指定的延时过后,发送广播,并唤醒设备(即使关机也会执行operation所对应的组件) // * RTC:在指定的时刻,发送广播,但不唤醒设备,如果在系统休眠时闹钟触发,它将不会被传递,直到下一次设备唤醒(闹钟在睡眠状态下不可用)。 // * RTC_WAKEUP:在指定的时刻,发送广播,并唤醒设备,即使系统关机也会执行 operation所对应的组件 // * */ //AlarmManager.ELAPSED_REALTIME_WAKEUP真实时间流逝闹钟,当闹钟发躰时唤醒手机休眠; //第二个参数表示闹钟首次执行时间 // /* // * 第三个参数设为60*1000以下时,都是一分钟提醒一次。 // * 第三个参数intervalTime为闹钟间隔,内置的几个变量如下: // * INTERVAL_DAY: 设置闹钟,间隔一天 // * INTERVAL_HALF_DAY: 设置闹钟,间隔半天 // * INTERVAL_FIFTEEN_MINUTES:设置闹钟,间隔15分钟 // * INTERVAL_HALF_HOUR: 设置闹钟,间隔半个小时 // * INTERVAL_HOUR: 设置闹钟,间隔一个小时 // * */ manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstTime,45*60*1000,sender); Log.i(TAG,"time ="+difTime+",selectTime="+selectTime+",systemTime="+systemTime+",firstTime="+firstTime); Toast.makeText(mContext,"设置护眼闹铃成功!",Toast.LENGTH_SHORT).show(); } }); //取消护眼闹铃 mButtonCancel = (Button) findViewById(R.id.cancel_button); mButtonCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(mContext,AlarmReceiver.class); intent.setAction("com.example.commuting.action.eyes"); PendingIntent sender = PendingIntent.getBroadcast(mContext,0,intent,0); //取消闹铃 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(sender); Toast.makeText(mContext,"取消护眼闹铃成功!",Toast.LENGTH_SHORT).show(); } }); //取消下班闹铃 mButtonCancel1 = (Button) findViewById(R.id.cancel_button1); mButtonCancel1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(mContext,AlarmReceiver.class); intent.setAction("com.example.commuting.action.outwork"); PendingIntent sender = PendingIntent.getBroadcast(mContext,0,intent,0); //取消闹铃 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.cancel(sender); Toast.makeText(mContext,"取消下班闹铃成功!",Toast.LENGTH_SHORT).show(); } }); } }
广播事件及处理:
public class AlarmReceiver extends BroadcastReceiver { String soundPath = Environment.getExternalStorageDirectory()+"/"; Context mContext; @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "闹铃响了, 可以做点事情了~~", Toast.LENGTH_LONG).show(); Log.d("AlarmRecever","闹铃响了"); mContext = context; //=====================状态栏提示 start ================= //设置通知内容并开启 NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); List<String> list = getSDCardMusic(context); //下班闹铃的响应 if(intent.getAction().equals("com.example.commuting.action.outwork")){ Notification notification1 = new Notification.Builder(context) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("下班时间到了") .setContentText("下班时间到了,记得打卡") .setWhen(System.currentTimeMillis()) .setContentIntent(getDefaultIntent(PendingIntent.FLAG_UPDATE_CURRENT)) .build(); if(list != null && list.size() !=0){ notification1.sound = Uri.parse(list.get(0)); }else{ notification1.defaults |= Notification.DEFAULT_SOUND; } //设置声音循环播放 notification1.flags |= Notification.FLAG_INSISTENT; notification1.defaults |= Notification.DEFAULT_VIBRATE; notification1.defaults |= Notification.DEFAULT_LIGHTS; manager.notify(2,notification1); } //护眼闹铃的响应 else if(intent.getAction().equals("com.example.commuting.action.eyes")){ Notification notification = new Notification.Builder(context) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("用电脑的时间过长!") .setContentText("用电脑时间有点长,眼睛需要好好休息一下。") .setWhen(System.currentTimeMillis()) //当用Notification.FLAG_AUTO_CANCEL时,点击通知栏后,通知栏自动消失(不是通知栏消息消失) //用PendingIntent.FLAG_UPDATE_CURRENT)时,点击通知栏消息后,消息消失 .setContentIntent(getDefaultIntent(PendingIntent.FLAG_UPDATE_CURRENT)) .build(); /* * 添加声音可以使用默认声音,也可以自定义声音,也可以用系统声音 * */ if(list != null && list.size() != 0){ notification.sound = Uri.parse(list.get(0)); Log.d("AlarmReceiver","musicName="+list.get(0)); }else{ notification.defaults |= Notification.DEFAULT_SOUND; } //设置声音循环播放 notification.flags |= Notification.FLAG_INSISTENT; /* * 添加振动,可以添加默认的振动defaults,也可以添加自定义振动vibrate * 另外还需要注意一点:使用振动器时需要权限,如下: * <uses-permission android:name="android.permission.VIBRATE"></uses-permission> * */ notification.defaults |= Notification.DEFAULT_VIBRATE; /* * 设置灯光 * */ notification.defaults |= Notification.DEFAULT_LIGHTS; // notification.contentIntent = PendingIntent.getActivity() //不起作用 notification.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(1,notification);// 这个notification 的 id 设为1023 } //=====================状态栏提示 end ================= } //获取设备SD卡中的歌曲路径 public List<String> getSDCardMusic(Context context){ String musicName; int isMusic; Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); List<String> musics = new ArrayList<>(); for(int i=0;i<cursor.getCount();i++){ cursor.moveToNext(); musicName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); // Log.d("AlarmReceiver","musicName="+musicName); isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); if(isMusic != 0){ musics.add(musicName); } } return musics; } public PendingIntent getDefaultIntent(int flags){ PendingIntent pendingIntent = PendingIntent.getActivity(mContext,1,new Intent(),flags); return pendingIntent; } }布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:gravity="center_horizontal" tools:context="com.example.commutingtime.RemindActivity"> <TimePicker android:id="@+id/timePicker" android:layout_centerHorizontal="true" android:timePickerMode="clock" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TimePicker> <LinearLayout android:id="@+id/ll_ring" android:gravity="center_horizontal" android:layout_below="@id/timePicker" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/normal_button" android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置下班提醒" /> <Button android:id="@+id/repeating_button" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置护眼闹铃" /> </LinearLayout> <LinearLayout android:layout_marginTop="20dp" android:gravity="center_horizontal" android:layout_below="@+id/ll_ring" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/cancel_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消下班闹铃" /> <Button android:id="@+id/cancel_button" android:layout_marginLeft="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消护眼闹铃" /> </LinearLayout> </RelativeLayout>