如何每天设置重复通知 8 时钟?

编程入门 行业动态 更新时间:2024-10-19 12:33:32
本文介绍了如何每天设置重复通知 8 时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要每天收到 8 时钟重复通知.我已经为它修复了一些编码,但我看到了一个错误.下面是一些精选.如何设置八点闹钟

I need to get 8 clock repeating notification every day. I have fixed some coding for it and I see an error. Below are some picks. How do I set an 8 clock alarm

报警活动

public class AlarmActivity extends AppCompatActivity { private AlarmManager alarmManager; private PendingIntent pendingIntent; private int REQUEST_CODE = 45645; private String id = "myname"; private long timeInMilliSeconds = 30; static String APP_TAG = "classname"; private BroadcastReceiver broadcastReceiver; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); initAlarm(); } private void initAlarm() { //creating intent Intent intent = new Intent(id); boolean alarmRunning = PendingIntent.getBroadcast( this, REQUEST_CODE, intent, PendingIntent.FLAG_NO_CREATE ) != null; //setting broadcast broadcastReceiver = new MyReceiver(); registerReceiver( broadcastReceiver, new IntentFilter(id) ); //setting alarm long ensurePositiveTime = Math.max(timeInMilliSeconds, 0L); pendingIntent = PendingIntent.getBroadcast( this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT ); //Check if alarm is already running if (!alarmRunning) { alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ensurePositiveTime, pendingIntent ); } else { updateAlarm(); Log.e("Alarm", "Alarm already running.!"); } } private void updateAlarm() { //calculating alarm time and creating pending intent Intent intent = new Intent(id); long ensurePositiveTime = Math.max(timeInMilliSeconds, 0L); pendingIntent = PendingIntent.getBroadcast( this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT ); //removing previously running alarm alarmManager.cancel(pendingIntent); unregisterReceiver(broadcastReceiver); //setting broadcast broadcastReceiver = new MyReceiver(); registerReceiver( broadcastReceiver, new IntentFilter(id) ); //Check if alarm is already running alarmManager.set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ensurePositiveTime, pendingIntent ); Log.e("Alarm", "Alarm updated..!"); } /** * Use this to cancel alarm */ private void cancelAlarm() { if (pendingIntent != null) { alarmManager.cancel(pendingIntent); } if (broadcastReceiver != null) { unregisterReceiver(broadcastReceiver); broadcastReceiver = null; } Log.e("Alarm", "Alarm has been canceled..!"); }

}

我的接收器

class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { WakeLocker.acquire(context); createNotification(context); WakeLocker.release(); } /*** * It creates notification * @param context */ private void createNotification(Context context) { String channelId = "fcm_default_channel"; String channelName = "notification"; Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId) .setSmallIcon(R.mipmap.ic_launcher) .setContentText("I am alarm from my activity") .setContentTitle(context.getString(R.string.app_name)) .setAutoCancel(true) .setSound(defaultSoundUri) .setDefaults(NotificationCompat.DEFAULT_ALL) .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( channelId, channelName, NotificationManager.IMPORTANCE_HIGH ); notificationBuilder.setChannelId(channelId); mNotificationManager.createNotificationChannel(channel); } Notification notification = notificationBuilder.build(); mNotificationManager.notify(0, notification); } }

唤醒锁

public abstract class WakeLocker { private static PowerManager.WakeLock wakeLock; public static void acquire(Context c) { if (wakeLock != null) wakeLock.release(); PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, AlarmActivity.APP_TAG); wakeLock.acquire(); } public static void release() { if (wakeLock != null){ wakeLock.release(); } wakeLock = null; } }

如何设置8时钟闹钟模式?如果你知道,我请求你的帮助.在此编码中,我只收到一次警报.

How do I set the 8 clock alarm model? If you know, I ask for your help. In this coding, I get an alarm only once.

推荐答案

只需更改init alarm和update alarm的代码

这将在每天早上 8 点触发警报

This will trigger alarm everyday at 8 AM

private void initAlarm() { //creating intent Intent intent = new Intent(id); boolean alarmRunning = PendingIntent.getBroadcast( this, REQUEST_CODE, intent, PendingIntent.FLAG_NO_CREATE ) != null; //setting broadcast broadcastReceiver = new MyReceiver(); registerReceiver( broadcastReceiver, new IntentFilter(id) ); //setting alarm pendingIntent = PendingIntent.getBroadcast( this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT ); //Check if alarm is already running if (!alarmRunning) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 0); long startUpTime = calendar.getTimeInMillis(); alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent ); } else { updateAlarm(); Log.e("Alarm", "Alarm already running.!"); } }

在 updateAlarm() 中

private void updateAlarm() { //calculating alarm time and creating pending intent Intent intent = new Intent(id); pendingIntent = PendingIntent.getBroadcast( this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT ); //removing previously running alarm alarmManager.cancel(pendingIntent); unregisterReceiver(broadcastReceiver); //setting broadcast broadcastReceiver = new MyReceiver(); registerReceiver( broadcastReceiver, new IntentFilter(id) ); //Check if alarm is already running Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 8); calendar.set(Calendar.MINUTE, 0); long startUpTime = calendar.getTimeInMillis(); alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent ); Log.e("Alarm", "Alarm updated..!"); }

更多推荐

如何每天设置重复通知 8 时钟?

本文发布于:2023-11-27 04:11:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时钟   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!