android 开源闹钟,Android闹钟 - huiyun-yoyo的个人页面 - OSCHINA - 中文开源技术交流社区...

设置闹钟

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();

}

}

添加唤醒锁屏权限:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值