android闹钟延时,android闹钟定时启动延时或者直接不启动
自己寫的android鬧鐘功能,需要實現timepicker選擇完成后將選擇的時間設定為鬧鐘的啟動時間,但是不管怎么改總是沒法定時啟動
alertDialog = new AlertDialog.Builder(context)
.setView(view)
.setCustomTitle(viewTitle)
.setNegativeButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
time = new StringBuilder();
time.append(hour < 10 ? "0" + hour : hour).append(":")
.append(minute < 10 ? "0" + minute : minute);
button.setText(time);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
intent = new Intent(context, AlarmNotifyReceiver.class);
pi = PendingIntent.getBroadcast(context, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
manager = (AlarmManager) context
.getSystemService(context.ALARM_SERVICE);
manager.setWindow(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
1000 * 60 * 60 * 24, pi);
Toast.makeText(context, "鬧鐘設置為" + time,
Toast.LENGTH_SHORT).show();
dialog.cancel();
}
})
.setPositiveButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertDialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
int divierId = alertDialog.getContext().getResources()
.getIdentifier("android:id/titleDivider", null, null);
View divider = alertDialog.findViewById(divierId);
divider.setBackgroundColor(context.getResources().getColor(
R.color.theme_background)); // earlier defined color-int
}
});
alertDialog.show();
接下來是接收廣播的類
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "啟動鬧鐘", Toast.LENGTH_SHORT).show();
notice = MediaPlayer.create(context,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
notice.setLooping(true);
try {
notice.setAudioStreamType(AudioManager.STREAM_ALARM);
notice.prepare();
notice.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Builder builder = new AlertDialog.Builder(context);
builder.setTitle("日常提醒鬧鐘").setMessage("請執行操作")
.setPositiveButton("好噠~~", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
notice.stop();
}
});
AlertDialog dialog=builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dialog.show();
}
寫了Service沒,要在注冊廣播
是不是時間設置錯了,你的TimePicker設置setOnTimeChangedListener來監聽時間的更新了沒
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android闹钟延时,android闹钟定时启动延时或者直接不启动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android接口一般定义格式,Andr
- 下一篇: Spring Cloud Alibaba