本文介绍了如何每天设置重复通知 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 时钟?
发布评论