利用AlarmManager实现闹钟设置
//设置本地闹钟,actiongString:闹钟标识
setLocAlarm(int week, String actionString) { Calendar currentCalendar = Calendar.getInstance();
currentCalendar.setTime(new Date());
int nowWeek = currentCalendar.get(Calendar.DAY_OF_WEEK);
int nowHour = currentCalendar.get(Calendar.HOUR_OF_DAY);
int nowMinute = currentCalendar.get(Calendar.MINUTE); int setHour = mTimePicker.getCurrentHour();
int setMinute = mTimePicker.getCurrentMinute(); Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, week);//星期
calendar.set(Calendar.HOUR_OF_DAY, setHour);//时
calendar.set(Calendar.MINUTE, setMinute);//分
calendar.set(Calendar.SECOND, 0);//秒
//代表设置的的是过去的时间加上7*24*60*60秒
if (week < nowWeek || (week == nowWeek && setHour < nowHour) || (week == nowWeek && setHour == nowHour && setMinute < nowMinute)) {
calendar.add(Calendar.SECOND, 7 * 24 * 60 * 60);
}
Intent intent = new Intent(getActivity(), AlarmService.class);
intent.setAction(actionString); PendingIntent pendingIntent = PendingIntent.getService(getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//指定唤醒service
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
//设置准确时间也可以利用manager.setRepeating();设置重复闹钟(时间不准)
manager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); }
2.取消闹钟
//根据表示取消闹钟
void cancelAlarm(String actionString) {
Intent intent = new Intent(getActivity(), AlarmService.class);
intent.setAction(actionString);
PendingIntent pendingIntent = PendingIntent.getService(getActivity(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
manager.cancel(pendingIntent);
}
3.在service中处理闹钟
public class AlarmService extends Service {
public AlarmService() {
} @Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
LogUtil.d("sss","收到了。。action=="+intent.getAction());
// 如果是重复的闹钟就根据当前时间重新设置下
if (needRepeat) {
resetAlarm(intent.getAction());
} Intent intent1 = new Intent();
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//打开闹钟提示activity
intent1.setClass(this, AlarmTipActivity.class); //若果是在前台收到的闹钟消息
if (LHTool.isForeceground(this)) {
intent1.putExtra("fromhead",true);
}else {
intent1.putExtra("fromhead",false);
}
startActivity(intent1);
return super.onStartCommand(intent,flags,startId); }
//重新设置下周当前时间的闹钟
private void resetAlarm(String actionStr){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.SECOND, 7 * 24 * 60 * 60);
Intent intent = new Intent(this, AlarmService.class);
intent.setAction(actionStr); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
manager.setExact(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent); } }
4.在acitivity中进行相关提示操作
public class AlarmTipActivity extends AppCompatActivity { private MediaPlayer mMediaPlayer; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_tip);
AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create();
builder.setMessage("Alarm Clock!");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { dialog.cancel();
Intent intent = getIntent();
//如果不是从前台过来的闹钟就启动MainActivity
if (!intent.getBooleanExtra("fromhead", false)) {
Intent intent1 = new Intent();
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent1.setClass(AlarmTipActivity.this, MainActivity.class);
startActivity(intent1);
}
finish();
}
});
//点击空白处不可取消alert
builder.setCancelable(false);
builder.show();
wakeUpScreen();
}
@Override
protected void onStart() {
super.onStart();
//确保唤醒后也能播放音乐
playMusic();
} private void playMusic() { mMediaPlayer = MediaPlayer.create(this, LHSharePreferenceTool.getAlarmRing(this));
mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.start(); }
/*唤醒屏幕*/
private void wakeUpScreen() {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON|WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
|WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
} @Override
protected void onStop() {
super.onStop();
if (mMediaPlayer != null) { mMediaPlayer.release();
mMediaPlayer = null;
}
}
}
相关权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>