当前位置: 首页 > 面试题库 >

通知在重启后运行一次

令狐功
2023-03-14
问题内容

我想在重新启动后通知工作正常。我提醒开始,但一次。如果我随后更改了手机上的日期,则没有通知。只有再次运行该应用程序,它们才会运行。也就是说,在不启动应用程序的情况下重新启动电话后,启动电话时,通知仅显示一次。

我在 MainActivity中 设置了通知时间:

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, c1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

MyAlarmService (onCreate):

mManager =(NotificationManager)this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

   Notification notification = new Notification(R.drawable.ic_launcher,"Title", System.currentTimeMillis());

   intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

   PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
   notification.flags |= Notification.FLAG_AUTO_CANCEL;
   notification.setLatestEventInfo(this.getApplicationContext(), "Title", "Description", pendingNotificationIntent);

   mManager.notify(0, notification);

MyReceiver:

Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);

AndroidManifest(MyReceiver和服务):

<service android:name=".MyAlarmService"
                 android:enabled="true"/>
        <receiver android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            android:label="MyReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>

问题答案:

重新启动后,通知(AlarmManager)丢失。看来您需要在重启时重置Alarmanager setRepeating()

请检查此线程

重新启动后重复警报管理器



 类似资料:
  • 定期任务配置: 在Logcat中,我得到以下内容: 附加所有相关细节: 编辑一:经过几天的分析,我想出了以下几点: 这是特定于设备的问题。例如,在nexus设备上不会发生。 这是一个更大问题的一部分。显示此行为的设备在使用、和时也不能正常工作。 一个解决方案是此解决方案。但是,这个解决方案至少存在2个问题。(1)当您终止应用程序时,权限将被重置。这意味着每次你打开应用程序后,手动权限是给予。(2)

  • 我试图实现推送通知与反应本机与此插件反应本机推送通知。我成功的是在应用程序运行时收到通知(在前台),但我想做的是在应用程序关闭时收到通知(后台),不运行,当我收到通知进入应用程序时。 我的密码 我正在使用firebase函数发送通知 AndroidManifest。xml 正如我之前所说的,只有当应用程序运行时,当我使用firebase http功能发送时,我才能成功获得通知。我现在只在Andro

  • 我在我的Android和iOS应用程序上都使用firebase进行推送通知,一切都很好。如果我重新启动设备,推送通知确实会出现,但要过一段时间(大约1-2分钟)。另一方面,WhatsApp几乎立即发出推送通知。我在想我怎样才能获得这种表现。我在android上读到过,我可以在启动时启动后台服务,但这有什么好处呢? 设备重启后的Android推送通知 在后台服务中,我唯一能想到的是从启动开始订阅推送

  • 问题内容: 我做出了一个管理命令,该命令从csv文件填充了我的一个模型。 我需要经常进行此更新,并且csv文件有成千上万的行。 有时可能需要10分钟以上才能完成填充。 我想添加一个功能,使我可以直接通过网站上传csv文件,并且在文件上传后,django应该运行该命令或至少运行其中的逻辑,然后填充数据库。 我将如何处理?我希望能够在上传文件后离开页面,并在任务完成后收到一封电子邮件。 问题答案: 你

  • 我有一个RecyclerView,它从API加载一些数据,包括一个图像url和一些数据,我使用networkImageView延迟加载图像。 下面是适配器的实现: 问题是,当我们在recyclerView中刷新时,它会在开始时短暂闪烁,这看起来很奇怪。 我只是使用了GridView/ListView,它就像我预期的那样工作。没有闪电战。 在我的片段创建的视图中配置RecycleView: 有人面临

  • 我有一个使用Spring kafka库的Spring启动应用程序的消费者。我想为每个消费者线程设置租户上下文,即在创建每个线程时调用一个方法(创建时每个线程只调用一次)。目前,我已将其添加到listner中,其中对方法有@KafkaListner注释,但它每次轮询并处理每条记录时都会调用它。我想在消费者线程启动时调用此方法一次。如果我们有任何这样的事情,请在这里帮助我。