我正在制作一个 android 应用程序......我一直在尝试为一天中的特定时间设置的每日通知编写代码。起初,我真的认为这会是一件容易的事,几乎 Play 商店中的每个应用程序都有一个定时通知。但是在一遍遍搜索之后,我发现的所有方法和 Youtube 教程都对我不起作用。问题可能出在我身上,但我不知道是什么。我所需要的只是一个简单、优雅、易于理解的方法(如果有的话)。任何帮助将不胜感激。
我所做的所有搜索都让我走到了这一步……但仍然没有任何运气:
此方法在我的 MainActivity 类中,仅在第一次启动应用程序时调用以设置闹钟...
private void alarmMethod() {
Intent myIntent = new Intent(this, NotifyService.class);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
Toast.makeText(MainActivity.this, "Start Alarm", Toast.LENGTH_LONG)
.show();
Run Code Online (Sandbox Code Playgroud)
这是我的 NotifyService 类:
package com.OHS.example; …Run Code Online (Sandbox Code Playgroud)