设置闹钟
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
SPUtil.setNotificationTime(Tools.getNotificationTime(hourOfDay, minute));
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
calendar1.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar1.set(Calendar.MINUTE, minute);
calendar1.set(Calendar.SECOND, 0);
calendar1.set(Calendar.MILLISECOND, 0);
am = (AlarmManager) getSystemService(ALARM_SERVICE);
start_alarm = new Intent(MessageActivity.this, ToastActivity.class);
sender = PendingIntent.getActivity(MessageActivity.this, 1, start_alarm, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), repeat_time, sender);
String s =Tools.changeTimeStyle(calendar1.get(Calendar.HOUR_OF_DAY),calendar1.get(Calendar.MINUTE));
showTimeTxt.setText(s);
SPUtil.setNotificationTime(s);
can_cancle = true;
}
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), true).show();
ToastActivity
public class ToastActivity extends AppCompatActivity {
private Uri ringUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
private final MediaPlayer mp = new MediaPlayer();
private Vibrator vibrator;
private PowerManager.WakeLock mWakelock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//hide title
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
startMedia();//播放音乐
startVibrator();//震动
showAlarmDialog();
}
@Override
protected void onResume() {
super.onResume();
if (mWakelock == null) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass()
.getCanonicalName());
mWakelock.acquire();
}
}
@Override
protected void onPause() {
super.onPause();
if (mWakelock != null && mWakelock.isHeld()) {
mWakelock.release();
mWakelock = null;
}
}
private void showAlarmDialog() {
new AlertDialog.Builder(this).setTitle("闹钟响了")
.setMessage(Tools.getSharedWeather(MyApplication.getCityData(SPUtil.getGPSCity())))
.setPositiveButton("关闭闹钟", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mp.stop();
vibrator.cancel();
ToastActivity.this.finish();
}
}).show();
}
private void startVibrator() {
long[] pattern = {500, 1000, 500, 1000};//停止 开始 停止 开始
vibrator.vibrate(pattern, 0);
}
private void startMedia() {
try {
mp.setDataSource(this, ringUri);
mp.prepare();
mp.start();
} catch (IOException e) {
e.printStackTrace();
}
}
添加唤醒锁屏权限: