如何在特定时间在android上发出通知?
我想在特定时间通知我的应用程序。比如说,即使应用程序关闭,我每天都必须在早上7点发出通知 我该怎么做?有教程吗?如何在特定时间在android上发出通知?,android,android-notifications,Android,Android Notifications,我想在特定时间通知我的应用程序。比如说,即使应用程序关闭,我每天都必须在早上7点发出通知 我该怎么做?有教程吗? 请提及该链接。您可以使用AlarmManager在指定时间设置报警 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (!prefs.getBoolean("firstTime", false)) { Intent alarmIntent = new Inten
请提及该链接。您可以使用
AlarmManager
在指定时间设置报警
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false)) {
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.apply();
}
我使用SharedReferences
检查这不是第一次运行应用程序,如果是,则设置该警报,否则不执行任何操作,而不是在每次启动应用程序时重置警报。使用
广播接收器
在警报发生时收听
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// show toast
Toast.makeText(context, "Alarm running", Toast.LENGTH_SHORT).show();
}
}
使用另一个接收器收听设备启动,以便重置警报
public class DeviceBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// on device boot compelete, reset the alarm
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
}
将权限添加到清单中
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
然后注册你的接收者
<receiver android:name=".DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" />
首先,您需要使用广播接收器。因为一个广播接收器只启动了很短的时间 在处理广播时,应用程序会得到一组固定的时间(目前为10秒)来完成它的工作。如果没有在这段时间内完成,应用程序将被视为行为异常,其进程将立即进入后台状态,以便在需要时为内存而终止 使用意图服务是一种更好的做法。这里有一个示例,说明如何使用意图服务 这是广播接收器类
public class MyReceiver extends BroadcastReceiver {
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, MyNewIntentService.class);
context.startService(intent1);
}
}
并在舱单上登记
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false" >
</receiver>
<service
android:name=".MyNewIntentService"
android:exported="false" >
</service>
并在舱单上登记
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false" >
</receiver>
<service
android:name=".MyNewIntentService"
android:exported="false" >
</service>
我希望这将对您有所帮助。由于后台服务限制(),来自公认答案的解决方案将无法在Android 8 Oreo(api级别26)及更高版本上正常工作,并且在应用程序处于后台时将导致类似的异常:
java.lang.IllegalStateException: Not allowed to start service Intent xxx: app is in background
可能的解决方法之一是使用:
JobIntentService
而不是IntentService
扩展您的Service
,并使用onHandleWork
方法而不是onhandlecontent
android:permission=“android.permission.BIND_JOB_SERVICE”
添加到AndroidManifest.xml
中的服务中
- 从系统获取报警服务
- 创建挂起的意图,传入广播接收器类的名称
- 制作日历对象并将其时间设置为上午8点
- 检查当前时间是否超过8。如果是,则再增加一天
- 调用AlarmManager类的set repeating方法
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmIntent = new Intent(context of current file, AlarmReceiver1.class);
AlarmReceiver1 = broadcast receiver
pendingIntent = PendingIntent.getBroadcast( Menu.this, 0, alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
alarmIntent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
alarmManager.cancel(pendingIntent);
Calendar alarmStartTime = Calendar.getInstance();
Calendar now = Calendar.getInstance();
alarmStartTime.set(Calendar.HOUR_OF_DAY, 8);
alarmStartTime.set(Calendar.MINUTE, 00);
alarmStartTime.set(Calendar.SECOND, 0);
if (now.after(alarmStartTime)) {
Log.d("Hey","Added a day");
alarmStartTime.add(Calendar.DATE, 1);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
alarmStartTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Alarm","Alarms set for everyday 8 am.");
来上广播接收机课。您需要在清单中注册广播接收器。这将导致您接收时钟事件。
覆盖此广播接收器的onReceive方法,并在其中自行发出通知,或创建单独的通知生成服务,并在其中生成和显示通知
清单代码段:
public class AlarmReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationService1.class);
service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
context.startService(service1);
}
public class NotificationService1 extends IntentService{
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
Context context = this.getApplicationContext();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, Activity to be opened after clicking on the
notif);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif","Notifications sent.");
}
}
广播接收器代码段:
public class AlarmReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationService1.class);
service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
context.startService(service1);
}
public class NotificationService1 extends IntentService{
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
Context context = this.getApplicationContext();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, Activity to be opened after clicking on the
notif);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif","Notifications sent.");
}
}
通知建筑服务代码段:
public class AlarmReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationService1.class);
service1.setData((Uri.parse("custom://"+System.currentTimeMillis())));
context.startService(service1);
}
public class NotificationService1 extends IntentService{
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
Context context = this.getApplicationContext();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, Activity to be opened after clicking on the
notif);
Bundle bundle = new Bundle();
bundle.putString("test", "test");
mIntent.putExtras(bundle);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Resources res = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("ticker value")
.setAutoCancel(true)
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Notif title")
.setContentText("Text").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif","Notifications sent.");
}
}
使用NotifyMe Android库进行简单通知。当您希望弹出通知时,可以非常轻松地设置延迟或时间。通知将通过系统重新启动弹出 使用Jitpack.io下载库 将此添加到应用程序的build.gradle文件中
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
将其添加到项目的build.gradle中的依赖项中
dependencies {
implementation 'com.github.jakebonk:NotifyMe:1.0.1'
}
范例
创建NotifyMe生成器对象
NotifyMe.Builder notifyMe = new NotifyMe.Builder(getApplicationContext());
然后设置所需的字段
notifyMe.title(String title);
notifyMe.content(String content);
notifyMe.color(Int red,Int green,Int blue,Int alpha);//Color of notification header
notifyMe.led_color(Int red,Int green,Int blue,Int alpha);//Color of LED when
notification pops up
notifyMe.time(Calendar time);//The time to popup notification
notifyMe.delay(Int delay);//Delay in ms
notifyMe.large_icon(Int resource);//Icon resource by ID
notifyMe.rrule("FREQ=MINUTELY;INTERVAL=5;COUNT=2")//RRULE for frequency of
//notification
notifyMe.addAction(Intent intent,String text); //The action will call the intent when
//pressed
设置完所有需要的字段后,只需调用build()
这是我的解决方案,在安卓10上测试。还与所有以前版本的android兼容 MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
....
reminderNotification();
}
public void reminderNotification()
{
NotificationUtils _notificationUtils = new NotificationUtils(this);
long _currentTime = System.currentTimeMillis();
long tenSeconds = 1000 * 10;
long _triggerReminder = _currentTime + tenSeconds; //triggers a reminder after 10 seconds.
_notificationUtils.setReminder(_triggerReminder);
}
public class NotificationUtils extends ContextWrapper
{
private NotificationManager _notificationManager;
private Context _context;
public NotificationUtils(Context base)
{
super(base);
_context = base;
createChannel();
}
public NotificationCompat.Builder setNotification(String title, String body)
{
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
private void createChannel()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, TIMELINE_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(channel);
}
}
public NotificationManager getManager()
{
if(_notificationManager == null)
{
_notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return _notificationManager;
}
public void setReminder(long timeInMillis)
{
Intent _intent = new Intent(_context, ReminderBroadcast.class);
PendingIntent _pendingIntent = PendingIntent.getBroadcast(_context, 0, _intent, 0);
AlarmManager _alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
_alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, _pendingIntent);
}
}
public class ReminderBroadcast extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
NotificationUtils _notificationUtils = new NotificationUtils(context);
NotificationCompat.Builder _builder = _notificationUtils.setNotification("Testing", "Testing notification system");
_notificationUtils.getManager().notify(101, _builder.build());
}
}
NotificationUtils.class
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
....
reminderNotification();
}
public void reminderNotification()
{
NotificationUtils _notificationUtils = new NotificationUtils(this);
long _currentTime = System.currentTimeMillis();
long tenSeconds = 1000 * 10;
long _triggerReminder = _currentTime + tenSeconds; //triggers a reminder after 10 seconds.
_notificationUtils.setReminder(_triggerReminder);
}
public class NotificationUtils extends ContextWrapper
{
private NotificationManager _notificationManager;
private Context _context;
public NotificationUtils(Context base)
{
super(base);
_context = base;
createChannel();
}
public NotificationCompat.Builder setNotification(String title, String body)
{
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
private void createChannel()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, TIMELINE_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(channel);
}
}
public NotificationManager getManager()
{
if(_notificationManager == null)
{
_notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return _notificationManager;
}
public void setReminder(long timeInMillis)
{
Intent _intent = new Intent(_context, ReminderBroadcast.class);
PendingIntent _pendingIntent = PendingIntent.getBroadcast(_context, 0, _intent, 0);
AlarmManager _alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
_alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, _pendingIntent);
}
}
public class ReminderBroadcast extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
NotificationUtils _notificationUtils = new NotificationUtils(context);
NotificationCompat.Builder _builder = _notificationUtils.setNotification("Testing", "Testing notification system");
_notificationUtils.getManager().notify(101, _builder.build());
}
}
提醒广播类
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
....
reminderNotification();
}
public void reminderNotification()
{
NotificationUtils _notificationUtils = new NotificationUtils(this);
long _currentTime = System.currentTimeMillis();
long tenSeconds = 1000 * 10;
long _triggerReminder = _currentTime + tenSeconds; //triggers a reminder after 10 seconds.
_notificationUtils.setReminder(_triggerReminder);
}
public class NotificationUtils extends ContextWrapper
{
private NotificationManager _notificationManager;
private Context _context;
public NotificationUtils(Context base)
{
super(base);
_context = base;
createChannel();
}
public NotificationCompat.Builder setNotification(String title, String body)
{
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.noti_icon)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}
private void createChannel()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, TIMELINE_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(channel);
}
}
public NotificationManager getManager()
{
if(_notificationManager == null)
{
_notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return _notificationManager;
}
public void setReminder(long timeInMillis)
{
Intent _intent = new Intent(_context, ReminderBroadcast.class);
PendingIntent _pendingIntent = PendingIntent.getBroadcast(_context, 0, _intent, 0);
AlarmManager _alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
_alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, _pendingIntent);
}
}
public class ReminderBroadcast extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
NotificationUtils _notificationUtils = new NotificationUtils(context);
NotificationCompat.Builder _builder = _notificationUtils.setNotification("Testing", "Testing notification system");
_notificationUtils.getManager().notify(101, _builder.build());
}
}
AndroidManifest.xml
<application>
...
<receiver android:name=".custom.ReminderBroadcast"/>
</application>
...
注意:CHANNEL\u ID
和TIMELINE\u CHANNEL\u NAME
已在另一个类上创建
比如说,
CHANNEL\u ID=“通知频道”
TIMELINE\u CHANNEL\u NAME=“TIMELINE notification”
如果对我的代码和bug有任何误解,请随时发表评论。我会尽快回复。对我不起作用,尝试手动设置时间,但仍然无效。使用广播的原因是什么?不认为这是必要的/有用的。你可以直接用一个挂起的帐篷与你的意向服务对话。它在Pie版本中有效吗?似乎没有这样做:(@NarendraSingh,这是因为您需要android O及更高版本的通知通道。查看是否在打开应用程序时也发送通知?您可以在
onCreate()中添加通知代码)
启动程序活动的方法。感谢您的回复。SharedReference
第二天将如何工作?因为布尔值将为False。欢迎使用。SharedReference
在应用程序首次启动时仅工作一次以设置警报,然后将布尔值设置为true,即应用程序在或者,为了避免反复重置警报。如果您的意图神奇地消失,您可能需要放置一个标志:。此外,如果您的可序列化的对象未包装在包中,则它们可能会变为空值。
:。当使用接受答案中给出的代码并根据您的说明修改它时,此方法不起作用。这不是调用ingBroadCastReceiver
。我该如何解决这个问题?如果没有看到您的代码,很难说。如果您的接收器有问题,它发生在启动服务之前,我的意思是,无论您使用我的建议还是从接受方的答案。您是否已在清单中注册了接收器?为什么不一起删除服务?Settin通知应该足够快,以便在广播接收器中完成。